@@ -2,31 +2,18 @@ package healthcheck
2
2
3
3
import (
4
4
"context"
5
- "fmt"
6
- "os"
7
- "sync"
8
5
"testing"
9
6
"time"
10
7
11
8
log "github.com/sirupsen/logrus"
12
9
)
13
10
14
- // Mutex to protect global variable access in tests
15
- var testMutex sync.Mutex
16
-
17
11
func TestNewReceiver (t * testing.T ) {
18
- testMutex .Lock ()
19
- originalTimeout := heartbeatTimeout
20
- heartbeatTimeout = 5 * time .Second
21
- testMutex .Unlock ()
22
-
23
- defer func () {
24
- testMutex .Lock ()
25
- heartbeatTimeout = originalTimeout
26
- testMutex .Unlock ()
27
- }()
28
-
29
- r := NewReceiver (log .WithContext (context .Background ()))
12
+
13
+ opts := ReceiverOptions {
14
+ HeartbeatTimeout : 5 * time .Second ,
15
+ }
16
+ r := NewReceiverWithOpts (log .WithContext (context .Background ()), opts )
30
17
defer r .Stop ()
31
18
32
19
select {
@@ -38,18 +25,10 @@ func TestNewReceiver(t *testing.T) {
38
25
}
39
26
40
27
func TestNewReceiverNotReceive (t * testing.T ) {
41
- testMutex .Lock ()
42
- originalTimeout := heartbeatTimeout
43
- heartbeatTimeout = 1 * time .Second
44
- testMutex .Unlock ()
45
-
46
- defer func () {
47
- testMutex .Lock ()
48
- heartbeatTimeout = originalTimeout
49
- testMutex .Unlock ()
50
- }()
51
-
52
- r := NewReceiver (log .WithContext (context .Background ()))
28
+ opts := ReceiverOptions {
29
+ HeartbeatTimeout : 1 * time .Second ,
30
+ }
31
+ r := NewReceiverWithOpts (log .WithContext (context .Background ()), opts )
53
32
defer r .Stop ()
54
33
55
34
select {
@@ -61,18 +40,10 @@ func TestNewReceiverNotReceive(t *testing.T) {
61
40
}
62
41
63
42
func TestNewReceiverAck (t * testing.T ) {
64
- testMutex .Lock ()
65
- originalTimeout := heartbeatTimeout
66
- heartbeatTimeout = 2 * time .Second
67
- testMutex .Unlock ()
68
-
69
- defer func () {
70
- testMutex .Lock ()
71
- heartbeatTimeout = originalTimeout
72
- testMutex .Unlock ()
73
- }()
74
-
75
- r := NewReceiver (log .WithContext (context .Background ()))
43
+ opts := ReceiverOptions {
44
+ HeartbeatTimeout : 2 * time .Second ,
45
+ }
46
+ r := NewReceiverWithOpts (log .WithContext (context .Background ()), opts )
76
47
defer r .Stop ()
77
48
78
49
r .Heartbeat ()
@@ -97,30 +68,19 @@ func TestReceiverHealthCheckAttemptThreshold(t *testing.T) {
97
68
98
69
for _ , tc := range testsCases {
99
70
t .Run (tc .name , func (t * testing.T ) {
100
- testMutex .Lock ()
101
- originalInterval := healthCheckInterval
102
- originalTimeout := heartbeatTimeout
103
- healthCheckInterval = 1 * time .Second
104
- heartbeatTimeout = healthCheckInterval + 500 * time .Millisecond
105
- testMutex .Unlock ()
106
-
107
- defer func () {
108
- testMutex .Lock ()
109
- healthCheckInterval = originalInterval
110
- heartbeatTimeout = originalTimeout
111
- testMutex .Unlock ()
112
- }()
113
- //nolint:tenv
114
- os .Setenv (defaultAttemptThresholdEnv , fmt .Sprintf ("%d" , tc .threshold ))
115
- defer os .Unsetenv (defaultAttemptThresholdEnv )
116
-
117
- receiver := NewReceiver (log .WithField ("test_name" , tc .name ))
118
-
119
- testTimeout := heartbeatTimeout * time .Duration (tc .threshold ) + healthCheckInterval
71
+ healthCheckInterval := 1 * time .Second
72
+
73
+ opts := ReceiverOptions {
74
+ HeartbeatTimeout : healthCheckInterval + 500 * time .Millisecond ,
75
+ AttemptThreshold : tc .threshold ,
76
+ }
77
+
78
+ receiver := NewReceiverWithOpts (log .WithField ("test_name" , tc .name ), opts )
79
+
80
+ testTimeout := opts .HeartbeatTimeout * time .Duration (tc .threshold ) + healthCheckInterval
120
81
121
82
if tc .resetCounterOnce {
122
83
receiver .Heartbeat ()
123
- t .Logf ("reset counter once" )
124
84
}
125
85
126
86
select {
@@ -134,7 +94,6 @@ func TestReceiverHealthCheckAttemptThreshold(t *testing.T) {
134
94
}
135
95
t .Fatalf ("should have timed out before %s" , testTimeout )
136
96
}
137
-
138
97
})
139
98
}
140
99
}
0 commit comments