forked from kbala444/bssim
/
bssim_test.go
238 lines (199 loc) · 5.58 KB
/
bssim_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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
// test
package main
import (
blocks "github.com/ipfs/go-ipfs/blocks"
key "github.com/ipfs/go-ipfs/blocks/key"
"math"
"os"
"testing"
"time"
)
func TestMain(m *testing.M) {
setup()
ret := m.Run()
teardown()
os.Exit(ret)
}
func TestPutBlockCmd(t *testing.T) {
configure("node_count:1", nil)
net, peers = createTestNetwork()
testBlock := blocks.NewBlock([]byte("testblock"))
err := putCmd([]int{0}, testBlock)
check(err)
if !checkHasBlock([]int{0}, testBlock.Key()) {
t.Error("Peer 0 never add block to blockstore.")
}
}
func TestPutFileCmd(t *testing.T) {
configure("node_count:3", nil)
net, peers = createTestNetwork()
// test single node
err := putFileCmd([]int{0}, "samples/test.mp3")
check(err)
for _, block := range files["samples/test.mp3"] {
if !checkHasBlock([]int{0}, block) {
t.Error("Peer 0 added file but it's not in its blockstore.")
}
}
// test multiple nodes
err = putFileCmd([]int{1, 2}, "samples/test.mp3")
check(err)
for _, block := range files["samples/test.mp3"] {
if !checkHasBlock([]int{1, 2}, block) {
t.Error("Peers 1 and 2 added file but it's not in its blockstore.")
}
}
// test putting same file again
err = putFileCmd([]int{0}, "samples/test.mp3")
check(err)
for _, block := range files["samples/test.mp3"] {
if !checkHasBlock([]int{0}, block) {
t.Error("Peer 0 added file but it's not in its blockstore.")
}
}
// test single block file
err = putFileCmd([]int{0}, "samples/test.txt")
check(err)
for _, block := range files["samples/test.txt"] {
if !checkHasBlock([]int{0}, block) {
t.Error("Peer 0 added file but it's not in its blockstore.")
}
}
// try adding nonexistent file
err = putFileCmd([]int{1}, "samples/ghost.mp3")
// expect path error (no such file or directory)
if _, ok := err.(*os.PathError); ok {
t.Error("Somehow put nonexistent file.")
}
// ensure it hasn't been added to files map
if _, ok := files["samples/ghost.mp3"]; ok {
t.Error("Somehow added nonexistent file to files map.")
}
}
func TestGetFileCmd(t *testing.T) {
configure("node_count:2", nil)
net, peers = createTestNetwork()
err := putFileCmd([]int{0}, "samples/test.mp3")
check(err)
for _, block := range files["samples/test.mp3"] {
if !checkHasBlock([]int{0}, block) {
t.Error("Peer 0 added file but it's not in its blockstore.")
}
}
err = getFileCmd([]int{1}, "samples/test.mp3")
check(err)
for _, block := range files["samples/test.mp3"] {
if !checkHasBlock([]int{1}, block) {
t.Error("Peer 0 got file but it's not in its blockstore.")
}
}
// try getting same file again
err = getFileCmd([]int{1}, "samples/test.mp3")
check(err)
for _, block := range files["samples/test.mp3"] {
if !checkHasBlock([]int{1}, block) {
t.Error("Peer 0 got file but it's not in its blockstore.")
}
}
// try getting nonexistent file
err = getFileCmd([]int{1}, "samples/ghost.mp3")
if err == nil {
t.Error("Somehow got nonexistent file.")
}
}
func TestLeaveCmd(t *testing.T) {
t.Skip()
configure("node_count:2, deadline:0.25", nil)
net, peers = createTestNetwork()
wantedFile := "samples/test.txt"
err := putFileCmd([]int{0}, wantedFile)
check(err)
err = leaveCmd([]int{0}, "0")
check(err)
// wait for node to unlink
time.Sleep(time.Millisecond)
err = getFileCmd([]int{1}, wantedFile)
check(err)
for _, block := range files[normalizePath(wantedFile)] {
if checkHasBlock([]int{1}, block) {
t.Error("Peer 1 got file after peer 0 left.")
}
}
configure("node_count:2, deadline:0.25", nil)
net, peers = createTestNetwork()
err = putFileCmd([]int{0}, wantedFile)
check(err)
err = leaveCmd([]int{0}, "5")
check(err)
// peer 1 should still be able to get the file
err = getFileCmd([]int{1}, wantedFile)
check(err)
for _, block := range files[normalizePath(wantedFile)] {
if !checkHasBlock([]int{1}, block) {
t.Error("Peer 1 couldn't get file.")
}
}
}
func TestFilePaths(t *testing.T) {
configure("node_count: 5", nil)
net, peers = createTestNetwork()
err := putFileCmd([]int{0}, "samples/test.txt")
check(err)
err = putFileCmd([]int{0}, "../bssim/samples/test.mp3")
check(err)
err = getFileCmd([]int{1}, "samples/test.txt")
check(err)
err = getFileCmd([]int{1}, "./samples/test.mp3")
check(err)
}
func TestRecorderStats(t *testing.T) {
// create new recorder on different db
// cause I need to commit stuff for this test
saved := recorder
recorder = NewRecorder("data/test/testing2")
configure("node_count: 2", nil)
net, peers = createTestNetwork()
err := putFileCmd([]int{0}, "samples/test.txt")
check(err)
rn := time.Now()
err = getFileCmd([]int{1}, "samples/test.txt")
elapsed := time.Since(rn)
check(err)
recorder.Commit("testing")
mbt, err := recorder.MeanBlockTime(peers[1])
check(err)
if !within(time.Duration(mbt)*time.Millisecond, elapsed, time.Millisecond*2) {
t.Log(mbt)
t.Log(elapsed)
t.Fatal("Mean block times are off")
}
if mbt != recorder.TotalMeanBlockTime(peers) {
t.Fatal("mismatch")
}
mft := time.Duration(recorder.TotalMeanFileTime() * float64(time.Second))
if !within(elapsed, mft, time.Millisecond) {
t.Log(elapsed, mft)
t.Fatal("Total mean file time is off")
}
// restore recorder to original
recorder = saved
}
func within(t1 time.Duration, t2 time.Duration, tolerance time.Duration) bool {
return math.Abs(float64(t1)-float64(t2)) < float64(tolerance)
}
func setup() {
recorder = NewRecorder("data/test/testing")
}
func teardown() {
recorder.Kill()
}
func checkHasBlock(nodes []int, block key.Key) bool {
for _, node := range nodes {
has, err := peers[node].Blockstore().Has(block)
check(err)
if !has {
return false
}
}
return true
}