1 package circuitbreaker_test
12 "github.com/go-kit/kit/endpoint"
15 func testFailingEndpoint(
17 breaker endpoint.Middleware,
19 shouldPass func(int) bool,
20 requestDelay time.Duration,
21 openCircuitError string,
23 _, file, line, _ := runtime.Caller(1)
24 caller := fmt.Sprintf("%s:%d", filepath.Base(file), line)
26 // Create a mock endpoint and wrap it with the breaker.
28 var e endpoint.Endpoint
32 // Prime the endpoint with successful requests.
33 for i := 0; i < primeWith; i++ {
34 if _, err := e(context.Background(), struct{}{}); err != nil {
35 t.Fatalf("%s: during priming, got error: %v", caller, err)
37 time.Sleep(requestDelay)
40 // Switch the endpoint to start throwing errors.
41 m.err = errors.New("tragedy+disaster")
44 // The first several should be allowed through and yield our error.
45 for i := 0; shouldPass(i); i++ {
46 if _, err := e(context.Background(), struct{}{}); err != m.err {
47 t.Fatalf("%s: want %v, have %v", caller, m.err, err)
49 time.Sleep(requestDelay)
53 // But the rest should be blocked by an open circuit.
54 for i := 0; i < 10; i++ {
55 if _, err := e(context.Background(), struct{}{}); err.Error() != openCircuitError {
56 t.Fatalf("%s: want %q, have %q", caller, openCircuitError, err.Error())
58 time.Sleep(requestDelay)
61 // Make sure none of those got through.
62 if want, have := through, m.through; want != have {
63 t.Errorf("%s: want %d, have %d", caller, want, have)
72 func (m *mock) endpoint(context.Context, interface{}) (interface{}, error) {
74 return struct{}{}, m.err