-
Notifications
You must be signed in to change notification settings - Fork 1
/
server_test.go
109 lines (90 loc) · 2.5 KB
/
server_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
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
104
105
106
107
108
109
package trr
import (
"log"
"os"
"testing"
)
func TestSingleServer(t *testing.T) {
log.Println("TEST >>>>>TestSingleServer<<<<")
srv := StartServer("127.0.0.1:1236", 1)
argP := PutArgs{Key: "test1", Value: []byte("v1")}
var reP PutReply
err := srv.Put(&argP, &reP)
if err != nil {
t.Error("Error happen on ", err)
}
log.Println(">>", reP, err)
argP = PutArgs{Key: "test1", Value: []byte("v2")}
err = srv.Put(&argP, &reP)
if err != nil {
t.Error("Error happen on ", err)
}
log.Println(">>", reP, err)
if string(reP.PreviousValue) != "v1" {
t.Error("Error on last value, expect v1, got :", reP.PreviousValue)
}
argP = PutArgs{Key: "test1", Value: []byte("v3")}
err = srv.Put(&argP, &reP)
if err != nil {
t.Error("Error happen on ", err)
}
log.Println(">>", reP, err)
if string(reP.PreviousValue) != "v2" {
t.Error("Error on last value, expect v1, got :", reP.PreviousValue)
}
argG := GetArgs{Key: "test1"}
var reG GetReply
err = srv.Get(&argG, &reG)
if err != nil || string(reG.Value) != "v3" {
t.Error("Error happen on ", err, reG)
}
log.Println(">>", reG, err)
log.Println("Stop server..")
srv.kill()
os.RemoveAll("trr-1")
}
func TestTwoServers(t *testing.T) {
log.Println("TEST >>>>>TestTwoServers<<<<")
var raftMsgSrvList []string
raftMsgSrvList = append(raftMsgSrvList, "http://127.0.0.1:12379")
raftMsgSrvList = append(raftMsgSrvList, "http://127.0.0.1:22379")
srv1 := StartClusterServers("127.0.0.1:1232", 1, raftMsgSrvList)
srv2 := StartClusterServers("127.0.0.1:1233", 2, raftMsgSrvList)
argP := PutArgs{Key: "test1", Value: []byte("v1")}
var reP PutReply
err := srv1.Put(&argP, &reP)
if err != nil {
t.Error("Error happen on ", err)
}
log.Println(">>", reP, err)
argP = PutArgs{Key: "test1", Value: []byte("v2")}
err = srv2.Put(&argP, &reP)
if err != nil {
t.Error("Error happen on ", err)
}
log.Println(">>", reP, err)
argP = PutArgs{Key: "test1", Value: []byte("v3")}
err = srv1.Put(&argP, &reP)
if err != nil {
t.Error("Error happen on ", err)
}
log.Println(">>", reP, err)
argG := GetArgs{Key: "test1"}
var reG GetReply
err = srv1.Get(&argG, &reG)
if err != nil || string(reG.Value) != "v3" {
t.Error("Error happen on ", err, reG)
}
err = srv2.Get(&argG, &reG)
log.Println(">>", reG, err)
log.Println("Stop server..")
//Kill leader test
srv1.kill()
os.RemoveAll("trr-1")
err = srv2.Get(&argG, &reG)
if err != nil || string(reG.Value) != "v3" {
t.Error("Error on kill leader happen on ", err, reG)
}
srv2.kill()
os.RemoveAll("trr-2")
}