-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtxn_test.go
57 lines (51 loc) · 1.43 KB
/
txn_test.go
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
package txn
import (
"context"
"errors"
"testing"
)
func TestPing(t *testing.T) {
// Test 1: Basic functionality
t.Run("basic functionality", func(t *testing.T) {
pingFunc := func(ctx context.Context) error {
return nil
}
cnt, err := Ping(5, nil, pingFunc)
if cnt != 1 || err != nil {
t.Errorf("Expected cnt=1 and err=nil, got cnt=%d and err=%v", cnt, err)
}
})
// Test 2: Retry mechanism
t.Run("retry mechanism", func(t *testing.T) {
retry := 0
pingFunc := func(ctx context.Context) error {
retry++
if retry == 2 {
return nil
}
return errors.New("failed")
}
cnt, err := Ping(2, nil, pingFunc)
if cnt != 2 || err != nil {
t.Errorf("Expected cnt=2 and err=nil, got cnt=%d and err=%v", cnt, err)
}
})
// Test 3: Reach retry limit
t.Run("reach retry limit", func(t *testing.T) {
pingFunc := func(ctx context.Context) error {
return errors.New("failed")
}
cnt, err := Ping(2, nil, pingFunc)
if cnt != 3 || err == nil || err.Error() != "reached retry limit (2), last error: failed" {
t.Errorf("Expected cnt=3 and specific error, got cnt=%d and err=%v", cnt, err)
}
})
// Test 4: Invalid parameters
t.Run("invalid parameters", func(t *testing.T) {
cnt, err := Ping(2, nil, nil)
if cnt != 3 || err == nil ||
err.Error() != "reached retry limit (2), last error: nil argument\n[txn.Ping ping]" {
t.Errorf("Expected cnt=3 and specific error, got cnt=%d and err=%v", cnt, err)
}
})
}