9 "github.com/go-kit/kit/log"
10 "github.com/go-kit/kit/sd"
13 var _ sd.Instancer = (*Instancer)(nil) // API check
15 func TestRefresh(t *testing.T) {
16 name := "some.service.internal"
18 ticker := time.NewTicker(time.Second)
20 tickc := make(chan time.Time)
24 records := []*net.SRV{}
25 lookup := func(service, proto, name string) (string, []*net.SRV, error) {
26 t.Logf("lookup(%q, %q, %q)", service, proto, name)
27 atomic.AddUint64(&lookups, 1)
28 return "cname", records, nil
31 instancer := NewInstancerDetailed(name, ticker, lookup, log.NewNopLogger())
32 defer instancer.Stop()
34 // First lookup, empty
35 state := instancer.cache.State()
39 if want, have := 0, len(state.Instances); want != have {
40 t.Errorf("want %d, have %d", want, have)
42 if want, have := uint64(1), atomic.LoadUint64(&lookups); want != have {
43 t.Errorf("want %d, have %d", want, have)
46 // Load some records and lookup again
48 {Target: "1.0.0.1", Port: 1001},
49 {Target: "1.0.0.2", Port: 1002},
50 {Target: "1.0.0.3", Port: 1003},
54 // There is a race condition where the instancer.State call below
55 // invokes the cache before it is updated by the tick above.
56 // TODO(pb): solve by running the read through the loop goroutine.
57 time.Sleep(100 * time.Millisecond)
59 state = instancer.cache.State()
63 if want, have := 3, len(state.Instances); want != have {
64 t.Errorf("want %d, have %d", want, have)
66 if want, have := uint64(2), atomic.LoadUint64(&lookups); want != have {
67 t.Errorf("want %d, have %d", want, have)
71 type nopCloser struct{}
73 func (nopCloser) Close() error { return nil }