github.com/wallyworld/juju@v0.0.0-20161013125918-6cf1bc9d917a/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/testing" 13 "github.com/juju/juju/watcher" 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.Assert(ok, jc.IsTrue) 59 c.Assert(actual.Changed, gc.HasLen, len(changed)) 60 // Because the versions can change, we only need to make sure 61 // the keys match, not the contents (UnitSettings == txnRevno). 62 for k, settings := range actual.Changed { 63 _, ok := changedNames[k] 64 c.Assert(ok, jc.IsTrue) 65 oldVer, ok := c.settingsVersions[k] 66 if !ok { 67 // This is the first time we see this unit, so 68 // save the settings version for later. 69 c.settingsVersions[k] = settings.Version 70 } else { 71 // Already seen; make sure the version increased. 72 if settings.Version <= oldVer { 73 c.Fatalf("expected unit settings version > %d (got %d)", oldVer, settings.Version) 74 } 75 } 76 } 77 c.Assert(actual.Departed, jc.SameContents, departed) 78 case <-timeout: 79 c.Fatalf("watcher did not send change") 80 } 81 } 82 83 // AssertStops Kills the watcher and asserts (1) that Wait completes without 84 // error before a long time has passed; and (2) that Changes remains open but 85 // no values are being sent. 86 func (c RelationUnitsWatcherC) AssertStops() { 87 c.Watcher.Kill() 88 wait := make(chan error) 89 go func() { 90 c.PreAssert() 91 wait <- c.Watcher.Wait() 92 }() 93 select { 94 case <-time.After(testing.LongWait): 95 c.Fatalf("watcher never stopped") 96 case err := <-wait: 97 c.Assert(err, jc.ErrorIsNil) 98 } 99 100 c.PreAssert() 101 select { 102 case change, ok := <-c.Watcher.Changes(): 103 c.Fatalf("watcher sent unexpected change: (%#v, %v)", change, ok) 104 default: 105 } 106 }