-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathglobalSnapshot_test.go
173 lines (155 loc) · 4.59 KB
/
globalSnapshot_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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package main_test
import (
"encoding/gob"
"fmt"
"github.com/DistributedClocks/GoVector/govec"
"github.com/DistributedClocks/GoVector/govec/vrpc"
"globalSnapshot/src/utils"
"golang.org/x/crypto/ssh"
"net/rpc"
"os"
"strconv"
"testing"
"time"
)
var RPCConn map[string]*rpc.Client
var sshConn map[string]*ssh.Client
// Connect via ssh and initialize RPC nodes
func TestMain(m *testing.M) {
fmt.Println("Starting tests for Global Snapshot... ")
setupNodes()
fmt.Println("Execute the rest of the tests...")
code := m.Run()
fmt.Println("Global Snapshot tests finished. Closing...")
terminate()
os.Exit(code)
}
// Source: http://networkbit.ch/golang-ssh-client/
func setupNodes() {
gob.Register(utils.Msg{})
var netLayout utils.NetLayout
netLayout = utils.ReadConfig()
if len(netLayout.Nodes) < 2 {
panic("At least 2 processes are needed")
}
fmt.Printf("Net layout: %v\n", netLayout.Nodes)
// Start logger govec
config := govec.GetDefaultConfig()
config.UseTimestamps = true
logger := govec.InitGoVector("T", utils.OutputDirRel+"GoVector/LogFileTest", config)
sshConn = make(map[string]*ssh.Client, 0)
RPCConn = make(map[string]*rpc.Client, 0)
for idx, node := range netLayout.Nodes {
//fmt.Println("Starting: " + node.Name)
sshConn[node.Name] = utils.ConnectSSH(node.User, node.IP)
// Initialize RPC
var cmd = "cd " + utils.WorkDirPath + ";/usr/local/go/bin/go run " + utils.WorkDirPath + "App.go " + strconv.Itoa(idx) + " " + strconv.Itoa(node.RPCPort)
go utils.RunCommandSSH(cmd, sshConn[node.Name])
// Connect via RPC to the server
var clientRPC *rpc.Client
var err error
for i := 0; i < netLayout.AttemptsSend; i++ {
time.Sleep(3 * time.Second) // Wait for RPC initialization
clientRPC, err = vrpc.RPCDial("tcp", node.IP+":"+strconv.Itoa(node.RPCPort), logger, govec.GetDefaultLogOptions())
if err == nil {
break
}
}
if err != nil {
panic(err)
}
RPCConn[node.Name] = clientRPC
}
}
func terminate() {
// var killed_once bool = false
for name, conn := range RPCConn {
_ = conn.Close()
utils.RunCommandSSH("pkill App", sshConn[name])
_ = sshConn[name].Close()
}
}
/*
func TestEmptySnapshot(t *testing.T) {
chRespSnap := make(chan utils.GlobalState, 1)
utils.RunRPCSnapshot(RPCConn["P0"], chRespSnap)
gs := <-chRespSnap
fmt.Printf("Snapshot completed: %s\n", gs)
}
*/
/*
func TestSimple(t *testing.T) {
chRespMsg := make(chan int, 1)
chRespSnap := make(chan utils.GlobalState, 1)
msg5 := utils.OutMsg{
Msg: utils.Msg{SrcName: "P0", Body: "MS5 - last"},
IdxDest: []int{2},
Delays: []int{0},
}
utils.RunRPCCommand("App.SendMsg", RPCConn["P0"], msg5, 5, chRespMsg)
fmt.Println("Test: ordered 5th msg")
time.Sleep(5*time.Second)
fmt.Println("Test: ordered last GS")
utils.RunRPCSnapshot( RPCConn["P1"], chRespSnap)
gs := <-chRespSnap
fmt.Printf("Snapshot completed: %s\n", gs)
}
*/
func TestMsgAndSnapshot(t *testing.T) {
NMsg := 4
chRespMsg := make(chan int, NMsg)
chRespSnap := make(chan utils.GlobalState, 1)
msg1 := utils.OutMsg{
Msg: utils.Msg{SrcName: "P0", Body: "MS1"},
IdxDest: []int{1},
Delays: []int{0},
}
go utils.RunRPCCommand("App.SendMsg", RPCConn["P0"], msg1, 1, chRespMsg)
fmt.Println("Test: ordered 1st msg")
msg2 := utils.OutMsg{
Msg: utils.Msg{
SrcName: "P2",
Body: "MSG2"},
IdxDest: []int{1},
Delays: []int{0},
}
go utils.RunRPCCommand("App.SendMsg", RPCConn["P2"], msg2, 2, chRespMsg)
fmt.Println("Test: ordered 2nd msg")
time.Sleep(2 * time.Second)
go utils.RunRPCSnapshot(RPCConn["P0"], chRespSnap)
fmt.Println("Test: ordered GS")
time.Sleep(4 * time.Second)
msg3 := utils.OutMsg{
Msg: utils.Msg{SrcName: "P1", Body: "MS3"},
IdxDest: []int{0},
Delays: []int{0},
}
utils.RunRPCCommand("App.SendMsg", RPCConn["P1"], msg3, 3, chRespMsg)
fmt.Println("Test: ordered 3rd msg")
msg4 := utils.OutMsg{
Msg: utils.Msg{SrcName: "P1", Body: "MS4"},
IdxDest: []int{2},
Delays: []int{0},
}
utils.RunRPCCommand("App.SendMsg", RPCConn["P1"], msg4, 4, chRespMsg)
fmt.Println("Test: ordered 4th msg")
for i := 0; i < NMsg; i++ {
nMsg := <-chRespMsg
fmt.Printf("Msg nº: %d sent\n", nMsg)
}
fmt.Println("All msg sent")
gs := <-chRespSnap
fmt.Printf("Snapshot completed: %s\n", gs)
msg5 := utils.OutMsg{
Msg: utils.Msg{SrcName: "P0", Body: "MS5 - last"},
IdxDest: []int{2},
Delays: []int{0},
}
utils.RunRPCCommand("App.SendMsg", RPCConn["P0"], msg5, 5, chRespMsg)
fmt.Println("Test: ordered 5th msg")
time.Sleep(10 * time.Second)
fmt.Println("Test: ordered last GS")
utils.RunRPCSnapshot(RPCConn["P1"], chRespSnap)
gs = <-chRespSnap
fmt.Printf("Snapshot completed: %s\n", gs)
}