-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrors_test.go
More file actions
117 lines (100 loc) · 2.82 KB
/
errors_test.go
File metadata and controls
117 lines (100 loc) · 2.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package gomian
import (
"errors"
"testing"
)
func TestErrCircuitOpen(t *testing.T) {
// Test that ErrCircuitOpen is defined
if ErrCircuitOpen == nil {
t.Error("ErrCircuitOpen should not be nil")
}
}
func TestCircuitError(t *testing.T) {
// Test creating a new circuit error
name := "TestBreaker"
message := "test error"
err := &CircuitError{
Name: name,
Err: errors.New(message),
}
// Check error fields
circuitErr := err
if circuitErr.Name != name {
t.Errorf("CircuitError.Name should be %s, got %s", name, circuitErr.Name)
}
// Check error message
expected := "circuit breaker 'TestBreaker': test error"
if err.Error() != expected {
t.Errorf("Error message should be '%s', got '%s'", expected, err.Error())
}
}
func TestCircuitErrorWithWrappedError(t *testing.T) {
// Test creating a circuit error with a wrapped error
name := "TestBreaker"
wrappedErr := errors.New("wrapped error")
err := &CircuitError{
Name: name,
Err: wrappedErr,
}
// Check error fields
circuitErr := err
if circuitErr.Name != name {
t.Errorf("CircuitError.Name should be %s, got %s", name, circuitErr.Name)
}
// Check error message
expected := "circuit breaker 'TestBreaker': wrapped error"
if err.Error() != expected {
t.Errorf("Error message should be '%s', got '%s'", expected, err.Error())
}
// Check wrapped error
if !errors.Is(err, wrappedErr) {
t.Error("CircuitError should wrap the provided error")
}
}
func TestIsCircuitOpen(t *testing.T) {
// Test with nil error
if IsCircuitOpen(nil) {
t.Error("IsCircuitOpen should return false for nil error")
}
// Test with non-circuit error
if IsCircuitOpen(errors.New("regular error")) {
t.Error("IsCircuitOpen should return false for non-circuit errors")
}
// Test with circuit open error
if !IsCircuitOpen(ErrCircuitOpen) {
t.Error("IsCircuitOpen should return true for ErrCircuitOpen")
}
// Test with wrapped circuit open error
circuitErr := &CircuitError{
Name: "test",
Err: ErrCircuitOpen,
}
if !IsCircuitOpen(circuitErr) {
t.Error("IsCircuitOpen should return true for wrapped ErrCircuitOpen")
}
// Test with other error
otherErr := errors.New("other error")
if IsCircuitOpen(otherErr) {
t.Error("IsCircuitOpen should return false for other errors")
}
}
func TestUnwrap(t *testing.T) {
// Test unwrapping a CircuitError with a wrapped error
wrappedErr := errors.New("wrapped error")
err := &CircuitError{
Name: "TestBreaker",
Err: wrappedErr,
}
// Check that errors.Unwrap returns the wrapped error
if errors.Unwrap(err) != wrappedErr {
t.Error("errors.Unwrap should return the wrapped error")
}
// Test unwrapping a CircuitError with a nil error
err = &CircuitError{
Name: "TestBreaker",
Err: nil,
}
if errors.Unwrap(err) != nil {
t.Error("errors.Unwrap should return nil for CircuitError with nil error")
}
}