github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/state/watcher/watchertest/strings.go (about)

     1  // Copyright 2017 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package watchertest
     5  
     6  import "gopkg.in/tomb.v2"
     7  
     8  // StringsWatcher is an implementation of state.StringsWatcher that can
     9  // be manipulated, for testing.
    10  type StringsWatcher struct {
    11  	T tomb.Tomb
    12  	C chan []string
    13  }
    14  
    15  // NewStringsWatcher returns a new StringsWatcher that returns the given
    16  // channel in its "Changes" method. NewStringsWatcher takes ownership of
    17  // the channel, closing it when it is stopped.
    18  func NewStringsWatcher(ch chan []string) *StringsWatcher {
    19  	w := &StringsWatcher{C: ch}
    20  	w.T.Go(func() error {
    21  		defer close(ch)
    22  		<-w.T.Dying()
    23  		return nil
    24  	})
    25  	return w
    26  }
    27  
    28  // Changes is part of the state.StringsWatcher interface.
    29  func (w *StringsWatcher) Changes() <-chan []string {
    30  	return w.C
    31  }
    32  
    33  // Err is part of the state.StringsWatcher interface.
    34  func (w *StringsWatcher) Err() error {
    35  	return w.T.Err()
    36  }
    37  
    38  // Stop is part of the state.StringsWatcher interface.
    39  func (w *StringsWatcher) Stop() error {
    40  	w.Kill()
    41  	return w.Wait()
    42  }
    43  
    44  // Kill is part of the state.StringsWatcher interface.
    45  func (w *StringsWatcher) Kill() {
    46  	w.T.Kill(nil)
    47  }
    48  
    49  // Wait is part of the state.StringsWatcher interface.
    50  func (w *StringsWatcher) Wait() error {
    51  	return w.T.Wait()
    52  }