/
factomd_test.go
208 lines (165 loc) · 3.65 KB
/
factomd_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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
package engine_test
import (
"testing"
"time"
"os"
"io/ioutil"
. "github.com/FactomProject/factomd/engine"
"github.com/FactomProject/factomd/state"
)
var _ = Factomd
// Wait so many blocks
func waitBlocks(s *state.State, blks int) {
currentHeight := int(s.LLeaderHeight)
for int(s.LLeaderHeight) < currentHeight+blks {
time.Sleep(time.Second)
}
}
// Wait to a given minute. If we are == to the minute or greater, then
// we first wait to the start of the next block.
func waitMinutes(s *state.State, min int) {
if s.CurrentMinute >= min {
for s.CurrentMinute > 0 {
time.Sleep(100*time.Millisecond)
}
}
for min > s.CurrentMinute {
time.Sleep(100*time.Millisecond)
}
}
func TestSetupANetwork(t *testing.T) {
rescueStdout := os.Stdout
r, w, _ := os.Pipe()
startCap := func() {
rescueStdout = os.Stdout
r, w, _ = os.Pipe()
os.Stdout = w
}
endCap := func() string {
<-ProcessChan
w.Close()
out, _ := ioutil.ReadAll(r)
os.Stdout = rescueStdout
return string(out)
}
runCmd := func(cmd string) string{
startCap()
InputChan <- cmd
v := endCap()
return v
}
args := append([]string{},
"-db=Map",
"-network=LOCAL",
"-enablenet=true",
"-blktime=15",
"-count=10",
"-logPort=37000",
"-port=37001",
"-ControlPanelPort=37002",
"-networkPort=37003")
go Factomd(args, false)
time.Sleep(3 * time.Second)
t.Log("Allocated 10 nodes")
if len(GetFnodes()) != 10 {
t.Fatal("Should have allocated 10 nodes")
t.Fail()
}
n0 := GetFnodes()[0]
runCmd("s")
runCmd("9")
runCmd("x")
runCmd("0")
waitBlocks(n0.State, 1)
runCmd("g10")
waitBlocks(n0.State, 1)
// Allocate 4 leaders
waitMinutes(n0.State, 1)
runCmd("l")
time.Sleep(100 * time.Millisecond)
runCmd("")
time.Sleep(100 * time.Millisecond)
runCmd("")
time.Sleep(100 * time.Millisecond)
runCmd("")
time.Sleep(100 * time.Millisecond)
// Allocate 3 audit servers
runCmd("o")
time.Sleep(100 * time.Millisecond)
runCmd("")
time.Sleep(100 * time.Millisecond)
runCmd("")
time.Sleep(100 * time.Millisecond)
waitBlocks(n0.State, 1)
waitMinutes(n0.State, 1)
leadercnt := 0
auditcnt := 0
for _, fn := range GetFnodes() {
s := fn.State
if s.Leader {
leadercnt++
}
list := s.ProcessLists.Get(s.LLeaderHeight)
if foundAudit, _ := list.GetAuditServerIndexHash(s.GetIdentityChainID()); foundAudit {
auditcnt++
}
}
if leadercnt != 4 {
t.Fatalf("found %d leaders, expected 4", leadercnt)
}
if auditcnt != 3 {
t.Fatalf("found %d audit servers, expected 3", auditcnt)
t.Fail()
}
fn1 := GetFocus()
if fn1.State.FactomNodeName != "FNode07" {
t.Fatalf("Expected FNode0, but got %s",fn1.State.FactomNodeName)
}
runCmd("8")
time.Sleep(100*time.Millisecond)
fn2 := GetFocus()
if fn2.State.FactomNodeName != "FNode08" {
t.Fatalf("Expected FNode08, but got %s",fn1.State.FactomNodeName)
}
// Test block recording lengths and error checking for pprof
runCmd("b100")
runCmd("b")
runCmd("babc")
runCmd("b1000000")
runCmd("/")
runCmd("w")
runCmd("g101")
runCmd("a1")
runCmd("e1")
runCmd("d1")
runCmd("f1")
runCmd("a100")
runCmd("e100")
runCmd("d100")
runCmd("f100")
runCmd("p")
runCmd("p")
runCmd("r")
time.Sleep(5*time.Second)
runCmd("r")
runCmd("9")
runCmd("x")
waitBlocks(fn1.State, 3)
runCmd("T10")
t.Log("Run to a dbht of 10")
n0.State.DirectoryBlockInSeconds = 4
for n0.State.LLeaderHeight < 8 {
time.Sleep(time.Second)
}
for n0.State.CurrentMinute < 1 {
time.Sleep(time.Second)
}
t.Log("Shutting down the network")
for _, fn := range GetFnodes() {
fn.State.ShutdownChan <- 1
}
time.Sleep(15 * time.Second)
if n0.State.LLeaderHeight > 10 {
t.Fatal("Failed to shut down factomd via ShutdownChan")
}
}