func (l *Listener) run(errorSink daemon.ErrorSink) { log.Debugf("Initialising state") changes := make(chan data.ServiceChange) l.store.WatchServices(nil, changes, errorSink, store.QueryServiceOptions{WithInstances: true}) // Send initial state of each service store.ForeachServiceInstance(l.store, func(name string, _ data.Service) error { log.Debugf("Initialising state for service %s", name) l.send(name) return nil }, nil) for { change := <-changes if change.ServiceDeleted { l.updates <- model.ServiceUpdate{ Service: model.Service{Name: change.Name}, Delete: true, } } else { log.Debugf("Updating state for service %s", change.Name) l.send(change.Name) } } }
func testInstances(s store.Store, t *testing.T) { require.Nil(t, s.AddService("svc", testService)) require.Nil(t, s.AddInstance("svc", "inst", testInst)) instances := func() map[string]data.Instance { insts := make(map[string]data.Instance) require.Nil(t, store.ForeachInstance(s, "svc", func(_, n string, inst data.Instance) error { insts[n] = inst return nil })) return insts } require.Equal(t, map[string]data.Instance{"inst": testInst}, instances()) serviceInstances := func() map[string]data.Instance { insts := make(map[string]data.Instance) require.Nil(t, store.ForeachServiceInstance(s, nil, func(sn string, in string, inst data.Instance) error { insts[sn+" "+in] = inst return nil })) return insts } require.Equal(t, map[string]data.Instance{"svc inst": testInst}, serviceInstances()) require.Nil(t, s.RemoveInstance("svc", "inst")) require.Equal(t, map[string]data.Instance{}, instances()) require.Equal(t, map[string]data.Instance{}, serviceInstances()) }
func allInstances(st store.Store) []data.Instance { res := make([]data.Instance, 0) store.ForeachServiceInstance(st, nil, func(_, _ string, inst data.Instance) error { res = append(res, inst) return nil }) return res }