func discoverdUnregisterFunc(c *C, hb discoverd.Heartbeater, sc *cache.ServiceCache) func() { return func() { done := make(chan struct{}) started := make(chan struct{}) go func() { events := make(chan *discoverd.Event) stream := sc.Watch(events, false) defer stream.Close() close(started) for event := range events { if event.Kind == discoverd.EventKindDown && event.Instance.Addr == hb.Addr() { close(done) return } } }() <-started c.Assert(hb.Close(), IsNil) select { case <-done: case <-time.After(10 * time.Second): c.Fatal("timed out waiting for discoverd unregister") } } }
func newService(name string, sc *cache.ServiceCache, wm *WatchManager, trackBackends bool) *service { s := &service{ name: name, sc: sc, wm: wm, } if trackBackends { events := make(chan *discoverd.Event) s.stream = sc.Watch(events, true) s.reqs = make(map[string]int64) s.cond = sync.NewCond(&sync.Mutex{}) go s.watchBackends(events) } return s }
func discoverdSetLeader(c *C, dc discoverdClient, sc *cache.ServiceCache, name, id string) { done := make(chan struct{}) go func() { events := make(chan *discoverd.Event) stream := sc.Watch(events, true) defer stream.Close() for event := range events { if event.Kind == discoverd.EventKindLeader && event.Instance.ID == id { close(done) return } } }() err := dc.Service(name).SetLeader(id) c.Assert(err, IsNil) select { case <-done: case <-time.After(10 * time.Second): c.Fatal("timed out waiting for discoverd leader change") } }
func discoverdRegister(c *C, dc discoverdClient, sc *cache.ServiceCache, name, addr string) func() { done := make(chan struct{}) go func() { events := make(chan *discoverd.Event) stream := sc.Watch(events, true) defer stream.Close() for event := range events { if event.Kind == discoverd.EventKindUp && event.Instance.Addr == addr { close(done) return } } }() hb, err := dc.AddServiceAndRegister(name, addr) c.Assert(err, IsNil) select { case <-done: case <-time.After(10 * time.Second): c.Fatal("timed out waiting for discoverd registration") } return discoverdUnregisterFunc(c, hb, sc) }