Removed extraneous test file.
Morgan Hein
7 years ago
0 | package lb_test | |
1 | ||
2 | import ( | |
3 | "errors" | |
4 | "testing" | |
5 | "time" | |
6 | ||
7 | "golang.org/x/net/context" | |
8 | ||
9 | "github.com/go-kit/kit/endpoint" | |
10 | "github.com/go-kit/kit/sd" | |
11 | loadbalancer "github.com/go-kit/kit/sd/lb" | |
12 | ) | |
13 | ||
14 | func TestRetryMaxTotalFail_WCB(t *testing.T) { | |
15 | var ( | |
16 | cb = func(count int, msg string) (bool, *string) { return true, nil } | |
17 | endpoints = sd.FixedSubscriber{} // no endpoints | |
18 | lb = loadbalancer.NewRoundRobin(endpoints) | |
19 | retry = loadbalancer.RetryWithCallback(999, time.Second, lb, cb) // lots of retries | |
20 | ctx = context.Background() | |
21 | ) | |
22 | if _, err := retry(ctx, struct{}{}); err == nil { | |
23 | t.Errorf("expected error, got none") // should fail | |
24 | } | |
25 | } | |
26 | ||
27 | func TestRetryMaxPartialFail_WCB(t *testing.T) { | |
28 | var ( | |
29 | cb = func(count int, msg string) (bool, *string) { return true, nil } | |
30 | endpoints = []endpoint.Endpoint{ | |
31 | func(context.Context, interface{}) (interface{}, error) { return nil, errors.New("error one") }, | |
32 | func(context.Context, interface{}) (interface{}, error) { return nil, errors.New("error two") }, | |
33 | func(context.Context, interface{}) (interface{}, error) { return struct{}{}, nil /* OK */ }, | |
34 | } | |
35 | subscriber = sd.FixedSubscriber{ | |
36 | 0: endpoints[0], | |
37 | 1: endpoints[1], | |
38 | 2: endpoints[2], | |
39 | } | |
40 | retries = len(endpoints) - 1 // not quite enough retries | |
41 | lb = loadbalancer.NewRoundRobin(subscriber) | |
42 | ctx = context.Background() | |
43 | ) | |
44 | if _, err := loadbalancer.RetryWithCallback(retries, time.Second, lb, cb)(ctx, struct{}{}); err == nil { | |
45 | t.Errorf("expected error, got none") | |
46 | } | |
47 | } | |
48 | ||
49 | func TestRetryMaxSuccess_WCB(t *testing.T) { | |
50 | var ( | |
51 | cb = func(count int, msg string) (bool, *string) { return true, nil } | |
52 | endpoints = []endpoint.Endpoint{ | |
53 | func(context.Context, interface{}) (interface{}, error) { return nil, errors.New("error one") }, | |
54 | func(context.Context, interface{}) (interface{}, error) { return nil, errors.New("error two") }, | |
55 | func(context.Context, interface{}) (interface{}, error) { return struct{}{}, nil /* OK */ }, | |
56 | } | |
57 | subscriber = sd.FixedSubscriber{ | |
58 | 0: endpoints[0], | |
59 | 1: endpoints[1], | |
60 | 2: endpoints[2], | |
61 | } | |
62 | retries = len(endpoints) // exactly enough retries | |
63 | lb = loadbalancer.NewRoundRobin(subscriber) | |
64 | ctx = context.Background() | |
65 | ) | |
66 | if _, err := loadbalancer.RetryWithCallback(retries, time.Second, lb, cb)(ctx, struct{}{}); err != nil { | |
67 | t.Error(err) | |
68 | } | |
69 | } | |
70 | ||
71 | func TestRetryTimeout_WCB(t *testing.T) { | |
72 | var ( | |
73 | cb = func(count int, msg string) (bool, *string) { return true, nil } | |
74 | step = make(chan struct{}) | |
75 | e = func(context.Context, interface{}) (interface{}, error) { <-step; return struct{}{}, nil } | |
76 | timeout = time.Millisecond | |
77 | retry = loadbalancer.RetryWithCallback(999, timeout, loadbalancer.NewRoundRobin(sd.FixedSubscriber{0: e}), cb) | |
78 | errs = make(chan error, 1) | |
79 | invoke = func() { _, err := retry(context.Background(), struct{}{}); errs <- err } | |
80 | ) | |
81 | ||
82 | go func() { step <- struct{}{} }() // queue up a flush of the endpoint | |
83 | invoke() // invoke the endpoint and trigger the flush | |
84 | if err := <-errs; err != nil { // that should succeed | |
85 | t.Error(err) | |
86 | } | |
87 | ||
88 | go func() { time.Sleep(10 * timeout); step <- struct{}{} }() // a delayed flush | |
89 | invoke() // invoke the endpoint | |
90 | if err := <-errs; err != context.DeadlineExceeded { // that should not succeed | |
91 | t.Errorf("wanted %v, got none", context.DeadlineExceeded) | |
92 | } | |
93 | } | |
94 | ||
95 | func AbortEarlyCustomMessage_WCB(t *testing.T) { | |
96 | var ( | |
97 | cb = func(count int, msg string) (bool, *string) { | |
98 | ret := "Aborting early" | |
99 | return false, &ret | |
100 | } | |
101 | endpoints = sd.FixedSubscriber{} // no endpoints | |
102 | lb = loadbalancer.NewRoundRobin(endpoints) | |
103 | retry = loadbalancer.RetryWithCallback(999, time.Second, lb, cb) // lots of retries | |
104 | ctx = context.Background() | |
105 | ) | |
106 | _, err := retry(ctx, struct{}{}) | |
107 | if err == nil { | |
108 | t.Errorf("expected error, got none") // should fail | |
109 | } | |
110 | if err.Error() != "Aborting early" { | |
111 | t.Errorf("expected custom error message, got %v", err) | |
112 | } | |
113 | } | |
114 | ||
115 | func AbortEarlyOnNTries_WCB(t *testing.T) { | |
116 | var ( | |
117 | cb = func(count int, msg string) (bool, *string) { | |
118 | if (count >= 4) { | |
119 | t.Errorf("expected retries to abort at 3 but continued to %v", count) | |
120 | } | |
121 | if (count == 3) { | |
122 | return false, nil | |
123 | } | |
124 | return true, nil | |
125 | } | |
126 | endpoints = sd.FixedSubscriber{} // no endpoints | |
127 | lb = loadbalancer.NewRoundRobin(endpoints) | |
128 | retry = loadbalancer.RetryWithCallback(999, time.Second, lb, cb) // lots of retries | |
129 | ctx = context.Background() | |
130 | ) | |
131 | if _, err := retry(ctx, struct{}{}); err == nil { | |
132 | t.Errorf("expected error, got none") // should fail | |
133 | } | |
134 | } |