11 "github.com/go-kit/kit/endpoint"
12 "github.com/go-kit/kit/sd"
15 func TestRoundRobin(t *testing.T) {
17 counts = []int{0, 0, 0}
18 endpoints = []endpoint.Endpoint{
19 func(context.Context, interface{}) (interface{}, error) { counts[0]++; return struct{}{}, nil },
20 func(context.Context, interface{}) (interface{}, error) { counts[1]++; return struct{}{}, nil },
21 func(context.Context, interface{}) (interface{}, error) { counts[2]++; return struct{}{}, nil },
25 endpointer := sd.FixedEndpointer(endpoints)
26 balancer := NewRoundRobin(endpointer)
28 for i, want := range [][]int{
37 endpoint, err := balancer.Endpoint()
41 endpoint(context.Background(), struct{}{})
42 if have := counts; !reflect.DeepEqual(want, have) {
43 t.Fatalf("%d: want %v, have %v", i, want, have)
48 func TestRoundRobinNoEndpoints(t *testing.T) {
49 endpointer := sd.FixedEndpointer{}
50 balancer := NewRoundRobin(endpointer)
51 _, err := balancer.Endpoint()
52 if want, have := ErrNoEndpoints, err; want != have {
53 t.Errorf("want %v, have %v", want, have)
57 func TestRoundRobinNoRace(t *testing.T) {
58 balancer := NewRoundRobin(sd.FixedEndpointer([]endpoint.Endpoint{
68 done = make(chan struct{})
75 for i := 0; i < n; i++ {
83 _, _ = balancer.Endpoint()
84 atomic.AddUint64(&count, 1)
90 time.Sleep(time.Second)
94 t.Logf("made %d calls", atomic.LoadUint64(&count))