github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/core/watcher/watchertest/relationunits.go (about) 1 // Copyright 2015 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package watchertest 5 6 import ( 7 "time" 8 9 jc "github.com/juju/testing/checkers" 10 gc "gopkg.in/check.v1" 11 12 "github.com/juju/juju/core/watcher" 13 "github.com/juju/juju/testing" 14 ) 15 16 // NewRelationUnitsWatcherC returns a RelationUnitsWatcherC that 17 // checks for aggressive event coalescence. 18 func NewRelationUnitsWatcherC(c *gc.C, w watcher.RelationUnitsWatcher, preAssert func()) RelationUnitsWatcherC { 19 if preAssert == nil { 20 preAssert = func() {} 21 } 22 return RelationUnitsWatcherC{ 23 C: c, 24 PreAssert: preAssert, 25 Watcher: w, 26 settingsVersions: make(map[string]int64), 27 } 28 } 29 30 type RelationUnitsWatcherC struct { 31 *gc.C 32 Watcher watcher.RelationUnitsWatcher 33 PreAssert func() 34 settingsVersions map[string]int64 35 } 36 37 func (c RelationUnitsWatcherC) AssertNoChange() { 38 c.PreAssert() 39 select { 40 case actual, ok := <-c.Watcher.Changes(): 41 c.Fatalf("watcher sent unexpected change: (%#v, %v)", actual, ok) 42 case <-time.After(testing.ShortWait): 43 } 44 } 45 46 // AssertChange asserts the given changes was reported by the watcher, 47 // but does not assume there are no following changes. 48 func (c RelationUnitsWatcherC) AssertChange(changed []string, departed []string) { 49 // Get all items in changed in a map for easy lookup. 50 changedNames := make(map[string]bool) 51 for _, name := range changed { 52 changedNames[name] = true 53 } 54 c.PreAssert() 55 timeout := time.After(testing.LongWait) 56 select { 57 case actual, ok := <-c.Watcher.Changes(): 58 c.Logf("got change %v", actual) 59 c.Assert(ok, jc.IsTrue) 60 c.Assert(actual.Changed, gc.HasLen, len(changed)) 61 // Because the versions can change, we only need to make sure 62 // the keys match, not the contents (UnitSettings == txnRevno). 63 for k, settings := range actual.Changed { 64 _, ok := changedNames[k] 65 c.Assert(ok, jc.IsTrue) 66 oldVer, ok := c.settingsVersions[k] 67 if !ok { 68 // This is the first time we see this unit, so 69 // save the settings version for later. 70 c.settingsVersions[k] = settings.Version 71 } else { 72 // Already seen; make sure the version increased. 73 if settings.Version <= oldVer { 74 c.Fatalf("expected unit settings version > %d (got %d)", oldVer, settings.Version) 75 } 76 } 77 } 78 c.Assert(actual.Departed, jc.SameContents, departed) 79 case <-timeout: 80 c.Fatalf("watcher did not send change") 81 } 82 } 83 84 // AssertStops Kills the watcher and asserts (1) that Wait completes without 85 // error before a long time has passed; and (2) that Changes remains open but 86 // no values are being sent. 87 func (c RelationUnitsWatcherC) AssertStops() { 88 c.Watcher.Kill() 89 wait := make(chan error) 90 go func() { 91 c.PreAssert() 92 wait <- c.Watcher.Wait() 93 }() 94 select { 95 case <-time.After(testing.LongWait): 96 c.Fatalf("watcher never stopped") 97 case err := <-wait: 98 c.Assert(err, jc.ErrorIsNil) 99 } 100 101 c.PreAssert() 102 select { 103 case change, ok := <-c.Watcher.Changes(): 104 c.Fatalf("watcher sent unexpected change: (%#v, %v)", change, ok) 105 default: 106 } 107 }