8 "github.com/go-kit/kit/endpoint"
9 "github.com/go-kit/kit/sd"
12 func TestRandom(t *testing.T) {
15 endpoints = make([]endpoint.Endpoint, n)
16 counts = make([]int, n)
20 tolerance = want / 100 // 1%
23 for i := 0; i < n; i++ {
25 endpoints[i] = func(context.Context, interface{}) (interface{}, error) { counts[i0]++; return struct{}{}, nil }
28 endpointer := sd.FixedEndpointer(endpoints)
29 balancer := NewRandom(endpointer, seed)
31 for i := 0; i < iterations; i++ {
32 endpoint, _ := balancer.Endpoint()
33 endpoint(context.Background(), struct{}{})
36 for i, have := range counts {
37 delta := int(math.Abs(float64(want - have)))
38 if delta > tolerance {
39 t.Errorf("%d: want %d, have %d, delta %d > %d tolerance", i, want, have, delta, tolerance)
44 func TestRandomNoEndpoints(t *testing.T) {
45 endpointer := sd.FixedEndpointer{}
46 balancer := NewRandom(endpointer, 1415926)
47 _, err := balancer.Endpoint()
48 if want, have := ErrNoEndpoints, err; want != have {
49 t.Errorf("want %v, have %v", want, have)