9 "github.com/go-kit/kit/endpoint"
10 "github.com/go-kit/kit/log"
13 func TestEndpointCache(t *testing.T) {
17 c = map[string]io.Closer{"a": ca, "b": cb}
18 f = func(instance string) (endpoint.Endpoint, io.Closer, error) { return endpoint.Nop, c[instance], nil }
19 cache = newEndpointCache(f, log.NewNopLogger(), endpointerOptions{})
23 cache.Update(Event{Instances: []string{"a", "b"}})
26 t.Errorf("endpoint a closed, not good")
28 t.Errorf("endpoint b closed, not good")
29 case <-time.After(time.Millisecond):
30 t.Logf("no closures yet, good")
32 assertEndpointsLen(t, cache, 2)
34 // Duplicate, should be no-op
35 cache.Update(Event{Instances: []string{"a", "b"}})
38 t.Errorf("endpoint a closed, not good")
40 t.Errorf("endpoint b closed, not good")
41 case <-time.After(time.Millisecond):
42 t.Logf("no closures yet, good")
44 assertEndpointsLen(t, cache, 2)
46 // Error, should continue returning old endpoints
47 cache.Update(Event{Err: errors.New("sd error")})
50 t.Errorf("endpoint a closed, not good")
52 t.Errorf("endpoint b closed, not good")
53 case <-time.After(time.Millisecond):
54 t.Logf("no closures yet, good")
56 assertEndpointsLen(t, cache, 2)
59 go cache.Update(Event{Instances: []string{"a"}})
62 t.Errorf("endpoint a closed, not good")
64 t.Logf("endpoint b closed, good")
65 case <-time.After(time.Second):
66 t.Errorf("didn't close the deleted instance in time")
68 assertEndpointsLen(t, cache, 1)
71 go cache.Update(Event{Instances: []string{}})
73 // case <-cb: will succeed, as it's closed
75 t.Logf("endpoint a closed, good")
76 case <-time.After(time.Second):
77 t.Errorf("didn't close the deleted instance in time")
79 assertEndpointsLen(t, cache, 0)
82 func TestEndpointCacheErrorAndTimeout(t *testing.T) {
86 c = map[string]io.Closer{"a": ca, "b": cb}
87 f = func(instance string) (endpoint.Endpoint, io.Closer, error) { return endpoint.Nop, c[instance], nil }
88 timeOut = 100 * time.Millisecond
89 cache = newEndpointCache(f, log.NewNopLogger(), endpointerOptions{
90 invalidateOnError: true,
91 invalidateTimeout: timeOut,
96 cache.timeNow = func() time.Time { return timeNow }
99 cache.Update(Event{Instances: []string{"a"}})
102 t.Errorf("endpoint a closed, not good")
103 case <-time.After(time.Millisecond):
104 t.Logf("no closures yet, good")
106 assertEndpointsLen(t, cache, 1)
108 // Send error, keep time still.
109 cache.Update(Event{Err: errors.New("sd error")})
112 t.Errorf("endpoint a closed, not good")
113 case <-time.After(time.Millisecond):
114 t.Logf("no closures yet, good")
116 assertEndpointsLen(t, cache, 1)
118 // Move the time, but less than the timeout
119 timeNow = timeNow.Add(timeOut / 2)
120 assertEndpointsLen(t, cache, 1)
123 t.Errorf("endpoint a closed, not good")
124 case <-time.After(time.Millisecond):
125 t.Logf("no closures yet, good")
128 // Move the time past the timeout
129 timeNow = timeNow.Add(timeOut)
130 assertEndpointsError(t, cache, "sd error")
133 t.Logf("endpoint a closed, good")
134 case <-time.After(time.Millisecond):
135 t.Errorf("didn't close the deleted instance in time")
138 // Send another error
139 cache.Update(Event{Err: errors.New("another sd error")})
140 assertEndpointsError(t, cache, "sd error") // expect original error
143 func TestBadFactory(t *testing.T) {
144 cache := newEndpointCache(func(string) (endpoint.Endpoint, io.Closer, error) {
145 return nil, nil, errors.New("bad factory")
146 }, log.NewNopLogger(), endpointerOptions{})
148 cache.Update(Event{Instances: []string{"foo:1234", "bar:5678"}})
149 assertEndpointsLen(t, cache, 0)
152 func assertEndpointsLen(t *testing.T, cache *endpointCache, l int) {
153 endpoints, err := cache.Endpoints()
155 t.Errorf("unexpected error %v", err)
158 if want, have := l, len(endpoints); want != have {
159 t.Errorf("want %d, have %d", want, have)
163 func assertEndpointsError(t *testing.T, cache *endpointCache, wantErr string) {
164 endpoints, err := cache.Endpoints()
166 t.Errorf("expecting error, not good")
169 if want, have := wantErr, err.Error(); want != have {
170 t.Errorf("want %s, have %s", want, have)
173 if want, have := 0, len(endpoints); want != have {
174 t.Errorf("want %d, have %d", want, have)
178 type closer chan struct{}
180 func (c closer) Close() error { close(c); return nil }