Explorar o código

Add test of panic behaviour for 100% statement coverage

Evan Huus %!s(int64=11) %!d(string=hai) anos
pai
achega
ba556f4cd0
Modificáronse 1 ficheiros con 31 adicións e 0 borrados
  1. 31 0
      breaker/breaker_test.go

+ 31 - 0
breaker/breaker_test.go

@@ -8,6 +8,10 @@ import (
 
 var errSomeError = errors.New("errSomeError")
 
+func alwaysPanics() error {
+	panic("foo")
+}
+
 func returnsError() error {
 	return errSomeError
 }
@@ -34,6 +38,33 @@ func TestBreakerErrorExpiry(t *testing.T) {
 	}
 }
 
+func TestBreakerPanicsCountAsErrors(t *testing.T) {
+	breaker := New(3, 2, 1*time.Second)
+
+	// three errors opens the breaker
+	for i := 0; i < 3; i++ {
+		func() {
+			defer func() {
+				val := recover()
+				if val.(string) != "foo" {
+					t.Error("incorrect panic")
+				}
+			}()
+			if err := breaker.Run(alwaysPanics); err != nil {
+				t.Error(err)
+			}
+			t.Error("shouldn't get here")
+		}()
+	}
+
+	// breaker is open
+	for i := 0; i < 5; i++ {
+		if err := breaker.Run(returnsError); err != ErrBreakerOpen {
+			t.Error(err)
+		}
+	}
+}
+
 func TestBreakerStateTransitions(t *testing.T) {
 	breaker := New(3, 2, 1*time.Second)