-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathex02_classification_test.go
More file actions
47 lines (36 loc) · 1.04 KB
/
ex02_classification_test.go
File metadata and controls
47 lines (36 loc) · 1.04 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
package errorsadv
import (
"errors"
"testing"
)
// A mock struct that implements the behavior pattern.
type NetworkErr struct{ tmp bool }
func (e NetworkErr) Error() string { return "network error" }
func (e NetworkErr) Temporary() bool { return e.tmp }
type FatalErr struct{}
func (e FatalErr) Error() string { return "fatal error" }
func TestExecuteWithRetryClassification(t *testing.T) {
// Test 1: Fatal Error (Should exit instantly attempt 1)
attempts := 0
fatalOp := func() error {
attempts++
return FatalErr{}
}
err := ExecuteWithRetry(fatalOp)
if !errors.Is(err, FatalErr{}) {
t.Fatalf("FAILED: Expected to return the fatal error untouched, got %v", err)
}
if attempts > 1 {
t.Fatalf("FAILED: It retried a fatal error! Attempts: %d", attempts)
}
// Test 2: Temporary Error (Should retry 3 times)
attempts = 0
tempOp := func() error {
attempts++
return NetworkErr{tmp: true}
}
_ = ExecuteWithRetry(tempOp)
if attempts != 3 {
t.Fatalf("FAILED: It failed to retry a temporary error! Attempts: %d", attempts)
}
}