OSDN Git Service

delete miner
[bytom/vapor.git] / vendor / github.com / go-kit / kit / sd / dnssrv / instancer_test.go
1 package dnssrv
2
3 import (
4         "net"
5         "sync/atomic"
6         "testing"
7         "time"
8
9         "github.com/go-kit/kit/log"
10         "github.com/go-kit/kit/sd"
11 )
12
13 var _ sd.Instancer = (*Instancer)(nil) // API check
14
15 func TestRefresh(t *testing.T) {
16         name := "some.service.internal"
17
18         ticker := time.NewTicker(time.Second)
19         ticker.Stop()
20         tickc := make(chan time.Time)
21         ticker.C = tickc
22
23         var lookups uint64
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
29         }
30
31         instancer := NewInstancerDetailed(name, ticker, lookup, log.NewNopLogger())
32         defer instancer.Stop()
33
34         // First lookup, empty
35         state := instancer.cache.State()
36         if state.Err != nil {
37                 t.Error(state.Err)
38         }
39         if want, have := 0, len(state.Instances); want != have {
40                 t.Errorf("want %d, have %d", want, have)
41         }
42         if want, have := uint64(1), atomic.LoadUint64(&lookups); want != have {
43                 t.Errorf("want %d, have %d", want, have)
44         }
45
46         // Load some records and lookup again
47         records = []*net.SRV{
48                 {Target: "1.0.0.1", Port: 1001},
49                 {Target: "1.0.0.2", Port: 1002},
50                 {Target: "1.0.0.3", Port: 1003},
51         }
52         tickc <- time.Now()
53
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)
58
59         state = instancer.cache.State()
60         if state.Err != nil {
61                 t.Error(state.Err)
62         }
63         if want, have := 3, len(state.Instances); want != have {
64                 t.Errorf("want %d, have %d", want, have)
65         }
66         if want, have := uint64(2), atomic.LoadUint64(&lookups); want != have {
67                 t.Errorf("want %d, have %d", want, have)
68         }
69 }
70
71 type nopCloser struct{}
72
73 func (nopCloser) Close() error { return nil }