github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/state/watcher/watchertest/notify.go (about) 1 // Copyright 2018 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package watchertest 5 6 import ( 7 tomb "gopkg.in/tomb.v2" 8 ) 9 10 type NotifyWatcher struct { 11 tomb tomb.Tomb 12 ch <-chan struct{} 13 } 14 15 func NewNotifyWatcher(ch <-chan struct{}) *NotifyWatcher { 16 w := &NotifyWatcher{ch: ch} 17 w.tomb.Go(func() error { 18 <-w.tomb.Dying() 19 return tomb.ErrDying 20 }) 21 return w 22 } 23 24 func (w *NotifyWatcher) Changes() <-chan struct{} { 25 return w.ch 26 } 27 28 func (w *NotifyWatcher) Stop() error { 29 w.Kill() 30 return w.Wait() 31 } 32 33 func (w *NotifyWatcher) Kill() { 34 w.tomb.Kill(nil) 35 } 36 37 // KillErr can be used to kill the worker with 38 // an error, to simulate a failing watcher. 39 func (w *NotifyWatcher) KillErr(err error) { 40 w.tomb.Kill(err) 41 } 42 43 func (w *NotifyWatcher) Err() error { 44 return w.tomb.Err() 45 } 46 47 func (w *NotifyWatcher) Wait() error { 48 return w.tomb.Wait() 49 }