Package list golang-github-go-kit-kit / 32eda3f
circuitbreaker: improve test reporting Peter Bourgon 6 years ago
1 changed file(s) with 10 addition(s) and 4 deletion(s). Raw diff Collapse all Expand all
11
22 import (
33 "errors"
4 "fmt"
5 "path/filepath"
6 "runtime"
47 "testing"
58
69 "golang.org/x/net/context"
912 )
1013
1114 func testFailingEndpoint(t *testing.T, breaker endpoint.Middleware, primeWith int, shouldPass func(int) bool, openCircuitError string) {
15 _, file, line, _ := runtime.Caller(1)
16 caller := fmt.Sprintf("%s:%d", filepath.Base(file), line)
17
1218 // Create a mock endpoint and wrap it with the breaker.
1319 m := mock{}
1420 var e endpoint.Endpoint
1824 // Prime the endpoint with successful requests.
1925 for i := 0; i < primeWith; i++ {
2026 if _, err := e(context.Background(), struct{}{}); err != nil {
21 t.Fatalf("during priming, got error: %v", err)
27 t.Fatalf("%s: during priming, got error: %v", caller, err)
2228 }
2329 }
2430
2935 // The first several should be allowed through and yield our error.
3036 for i := 0; shouldPass(i); i++ {
3137 if _, err := e(context.Background(), struct{}{}); err != m.err {
32 t.Fatalf("want %v, have %v", m.err, err)
38 t.Fatalf("%s: want %v, have %v", caller, m.err, err)
3339 }
3440 }
3541 thru := m.thru
3743 // But the rest should be blocked by an open circuit.
3844 for i := 0; i < 10; i++ {
3945 if _, err := e(context.Background(), struct{}{}); err.Error() != openCircuitError {
40 t.Fatalf("want %q, have %q", openCircuitError, err.Error())
46 t.Fatalf("%s: want %q, have %q", caller, openCircuitError, err.Error())
4147 }
4248 }
4349
4450 // Make sure none of those got through.
4551 if want, have := thru, m.thru; want != have {
46 t.Errorf("want %d, have %d", want, have)
52 t.Errorf("%s: want %d, have %d", caller, want, have)
4753 }
4854 }
4955