MCPcopy Index your code
hub / github.com/go-kit/kit / TestDefaultEndpointer

Function TestDefaultEndpointer

sd/endpointer_test.go:14–68  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

12)
13
14func TestDefaultEndpointer(t *testing.T) {
15 var (
16 ca = make(closer)
17 cb = make(closer)
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
21 }
22 instancer = &mockInstancer{instance.NewCache()}
23 )
24 // set initial state
25 instancer.Update(sd.Event{Instances: []string{"a", "b"}})
26
27 endpointer := sd.NewEndpointer(instancer, f, log.NewNopLogger(), sd.InvalidateOnError(time.Minute))
28
29 var (
30 endpoints []endpoint.Endpoint
31 err error
32 )
33 if !within(time.Second, func() bool {
34 endpoints, err = endpointer.Endpoints()
35 return err == nil && len(endpoints) == 2
36 }) {
37 t.Errorf("wanted 2 endpoints, got %d (%v)", len(endpoints), err)
38 }
39
40 instancer.Update(sd.Event{Instances: []string{}})
41
42 select {
43 case <-ca:
44 t.Logf("endpoint a closed, good")
45 case <-time.After(time.Millisecond):
46 t.Errorf("didn't close the deleted instance in time")
47 }
48
49 select {
50 case <-cb:
51 t.Logf("endpoint b closed, good")
52 case <-time.After(time.Millisecond):
53 t.Errorf("didn't close the deleted instance in time")
54 }
55
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)
60 }
61
62 endpointer.Close()
63
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.
68}
69
70type mockInstancer struct{ *instance.Cache }
71

Callers

nothing calls this directly

Calls 7

EndpointsMethod · 0.95
NewCacheFunction · 0.92
NewEndpointerFunction · 0.92
InvalidateOnErrorFunction · 0.92
withinFunction · 0.70
UpdateMethod · 0.45
CloseMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…