func (stringSetSuite) TestUninitialized(c *gc.C) { var uninitialized set.Strings c.Assert(uninitialized.Size(), gc.Equals, 0) c.Assert(uninitialized.IsEmpty(), gc.Equals, true) // You can get values and sorted values from an unitialized set. AssertValues(c, uninitialized) // All contains checks are false c.Assert(uninitialized.Contains("foo"), gc.Equals, false) // Remove works on an uninitialized Strings uninitialized.Remove("foo") var other set.Strings // Union returns a new set that is empty but initialized. c.Assert(uninitialized.Union(other), gc.DeepEquals, set.NewStrings()) c.Assert(uninitialized.Intersection(other), gc.DeepEquals, set.NewStrings()) c.Assert(uninitialized.Difference(other), gc.DeepEquals, set.NewStrings()) other = set.NewStrings("foo", "bar") c.Assert(uninitialized.Union(other), gc.DeepEquals, other) c.Assert(uninitialized.Intersection(other), gc.DeepEquals, set.NewStrings()) c.Assert(uninitialized.Difference(other), gc.DeepEquals, set.NewStrings()) c.Assert(other.Union(uninitialized), gc.DeepEquals, other) c.Assert(other.Intersection(uninitialized), gc.DeepEquals, set.NewStrings()) c.Assert(other.Difference(uninitialized), gc.DeepEquals, other) // Once something is added, the set becomes initialized. uninitialized.Add("foo") AssertValues(c, uninitialized, "foo") }
func (w *actionWatcher) loop() error { var ( changes set.Strings in <-chan watcher.Change = w.source out chan<- []string = w.sink ) w.st.watcher.WatchCollectionWithFilter(w.st.actions.Name, w.source, w.filterFn) defer w.st.watcher.UnwatchCollection(w.st.actions.Name, w.source) initial, err := w.initial() if err != nil { return err } changes = initial for { select { case <-w.tomb.Dying(): return tomb.ErrDying case <-w.st.watcher.Dead(): return stateWatcherDeadError(w.st.watcher.Err()) case ch := <-in: updates, ok := collect(ch, in, w.tomb.Dying()) if !ok { return tomb.ErrDying } if err := w.merge(changes, initial, updates); err != nil { return err } if !changes.IsEmpty() { out = w.sink } else { out = nil } case out <- changes.Values(): changes = set.NewStrings() out = nil } } }