forked from goraft/raft
-
Notifications
You must be signed in to change notification settings - Fork 0
/
peer_test.go
40 lines (32 loc) · 1.19 KB
/
peer_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
package raft
import (
"testing"
"time"
)
func TestHeartbeatDuration(t *testing.T) {
heartbeatInterval := time.Duration(20)
p := newPeer(nil, "testpeer", "testpeer", heartbeatInterval)
if p.failedHeartbeats != 0 {
t.Error("Failed heartbeat counter's default is not 0.")
}
if p.heartbeatDuration() != heartbeatInterval {
t.Errorf("Unexpected heartbeat duration. Expected %d, got %d", heartbeatInterval, p.heartbeatDuration())
}
p.backoffHeartbeat()
if p.failedHeartbeats != 1 {
t.Errorf("Failed heartbeat counter did not increment. Expected %d, got %d", 1, p.failedHeartbeats)
}
firstBackoff := time.Duration(heartbeatInterval * 2)
if p.heartbeatDuration() != firstBackoff {
t.Errorf("Unexpected heartbeat duration. Expected %d, got %d", firstBackoff, p.heartbeatDuration())
}
p.backoffHeartbeat()
secondBackoff := time.Duration(heartbeatInterval * 4)
if p.heartbeatDuration() != secondBackoff {
t.Errorf("Unexpected heartbeat duration. Expected %d, got %d", secondBackoff, p.heartbeatDuration())
}
p.resetHeartbeatInterval()
if p.heartbeatDuration() != heartbeatInterval {
t.Errorf("Unexpected heartbeat interval. Expected %d, got %d", heartbeatInterval, p.heartbeatDuration())
}
}