File tree 1 file changed +9
-0
lines changed
1 file changed +9
-0
lines changed Original file line number Diff line number Diff line change 4
4
"context"
5
5
"errors"
6
6
"net/url"
7
+ "sync"
7
8
"testing"
8
9
"time"
9
10
@@ -16,13 +17,16 @@ var _ heartbeat.HeartBeat = &mockHeartbeat{}
16
17
type mockHeartbeat struct {
17
18
interval time.Duration
18
19
err error
20
+ mu sync.Mutex
19
21
}
20
22
21
23
func (hb * mockHeartbeat ) Interval () time.Duration {
22
24
return hb .interval
23
25
}
24
26
25
27
func (hb * mockHeartbeat ) IsHealthy (ctx context.Context , addr string ) error {
28
+ hb .mu .Lock ()
29
+ defer hb .mu .Unlock ()
26
30
return hb .err
27
31
}
28
32
@@ -54,6 +58,7 @@ func TestStartHeartbeat(t *testing.T) {
54
58
hb := & mockHeartbeat {
55
59
interval : 10 * time .Millisecond ,
56
60
err : nil ,
61
+ mu : sync.Mutex {},
57
62
}
58
63
59
64
done := make (chan struct {})
@@ -69,14 +74,18 @@ func TestStartHeartbeat(t *testing.T) {
69
74
}, time .Second , 100 * time .Millisecond )
70
75
71
76
// change heartbeat to error, node eventually becomes inactive.
77
+ hb .mu .Lock ()
72
78
hb .err = errors .New ("failed connection" )
79
+ hb .mu .Unlock ()
73
80
74
81
assert .Eventually (t , func () bool {
75
82
return ! node .IsActive ()
76
83
}, time .Second , 100 * time .Millisecond )
77
84
78
85
// If error is removed node becomes active again.
86
+ hb .mu .Lock ()
79
87
hb .err = nil
88
+ hb .mu .Unlock ()
80
89
81
90
assert .Eventually (t , func () bool {
82
91
return node .IsActive ()
You can’t perform that action at this time.
0 commit comments