8 "github.com/go-kit/kit/endpoint"
9 "github.com/go-kit/kit/log"
10 "github.com/go-kit/kit/sd"
11 "github.com/go-kit/kit/sd/internal/instance"
14 func TestDefaultEndpointer(t *testing.T) {
18 c = map[string]io.Closer{"a": ca, "b": cb}
19 f = func(instance string) (endpoint.Endpoint, io.Closer, error) {
20 return endpoint.Nop, c[instance], nil
22 instancer = &mockInstancer{instance.NewCache()}
25 instancer.Update(sd.Event{Instances: []string{"a", "b"}})
27 endpointer := sd.NewEndpointer(instancer, f, log.NewNopLogger(), sd.InvalidateOnError(time.Minute))
30 endpoints []endpoint.Endpoint
33 if !within(time.Second, func() bool {
34 endpoints, err = endpointer.Endpoints()
35 return err == nil && len(endpoints) == 2
37 t.Errorf("wanted 2 endpoints, got %d (%v)", len(endpoints), err)
40 instancer.Update(sd.Event{Instances: []string{}})
44 t.Logf("endpoint a closed, good")
45 case <-time.After(time.Millisecond):
46 t.Errorf("didn't close the deleted instance in time")
51 t.Logf("endpoint b closed, good")
52 case <-time.After(time.Millisecond):
53 t.Errorf("didn't close the deleted instance in time")
56 if endpoints, err := endpointer.Endpoints(); err != nil {
57 t.Errorf("unepected error %v", err)
58 } else if want, have := 0, len(endpoints); want != have {
59 t.Errorf("want %d, have %d", want, have)
64 instancer.Update(sd.Event{Instances: []string{"a"}})
65 // TODO verify that on Close the endpointer fully disconnects from the instancer.
66 // Unfortunately, because we use instance.Cache, this test cannot be in the sd package,
67 // and therefore does not have access to the endpointer's private members.
70 type mockInstancer struct{ *instance.Cache }
72 type closer chan struct{}
74 func (c closer) Close() error { close(c); return nil }
76 func within(d time.Duration, f func() bool) bool {
77 deadline := time.Now().Add(d)
78 for time.Now().Before(deadline) {