-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathraft_test.go
More file actions
103 lines (86 loc) · 2.43 KB
/
raft_test.go
File metadata and controls
103 lines (86 loc) · 2.43 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package main
import (
"math/rand"
"sync/atomic"
"testing"
"time"
"github.com/alecthomas/assert"
log "github.com/sirupsen/logrus"
)
func TestClient(t *testing.T) {
id := 0
addrs := map[int]string{
0: "http://localhost:8081",
}
raft := initNode(id, addrs, nil)
receiveTimer := time.NewTimer(time.Duration((raft.heartBeat - 1000)) * time.Millisecond)
raft.receiveTimer = receiveTimer
message := make(chan string, MaxMessgaeSize)
go raft.receive(addrs[id], message)
time.Sleep(time.Duration(500) * time.Millisecond)
raft.send("test from send", addrs[id])
msg := <-message
ast := assert.New(t)
ast.Equal("test from send", msg)
}
func TestServerTimeout(t *testing.T) {
id := 0
addrs := map[int]string{
0: "http://localhost:8081",
}
raft := initNode(id, addrs, nil)
receiveTimer := time.NewTimer(time.Duration((raft.heartBeat - 1000)) * time.Millisecond)
raft.receiveTimer = receiveTimer
message := make(chan string, MaxMessgaeSize)
go raft.receiveTimeoutHandler(message) // 开启接受超时
msg := <-message
log.Infof("message:%v", msg)
ast := assert.New(t)
ast.Equal("", msg)
}
func TestLeadElection(t *testing.T) {
addrs := map[int]string{
0: "http://localhost:8081",
1: "http://localhost:8082",
2: "http://localhost:8083",
}
called := uint64(0)
leaderCall := func() {
log.Infof("I'm leader!")
atomic.AddUint64(&called, 1)
}
raft0 := initNode(0, addrs, leaderCall)
go raft0.run()
raft1 := initNode(1, addrs, leaderCall)
go raft1.run()
raft2 := initNode(2, addrs, leaderCall)
go raft2.run()
time.Sleep(time.Duration(20000) * time.Millisecond)
log.Infof("### called:%d", called)
ast := assert.New(t)
ast.Equal(uint64(1), called)
}
// 单独组件测试
func TestTimer(t *testing.T) {
heartbeat := 3000
log.Infof("start timer")
receiveTimer := time.NewTimer(time.Duration((heartbeat - 1000)) * time.Millisecond)
go func(receiveTimer *time.Timer) {
<-receiveTimer.C
log.Infof("timeout!!")
}(receiveTimer)
time.Sleep(time.Duration(1) * time.Second)
log.Infof("first sleep end, reset")
receiveTimer.Reset(time.Duration((heartbeat - 1000)) * time.Millisecond)
time.Sleep(time.Duration(1) * time.Second)
log.Infof("2ed sleep end, reset")
receiveTimer.Reset(time.Duration((heartbeat - 1000)) * time.Millisecond)
time.Sleep(time.Duration(4) * time.Second)
}
func TestRandom(t *testing.T) {
max := 700
min := 300
rand.Seed(time.Now().Unix())
res := rand.Intn(max-min) + min
log.Infof("res:%d", res)
}