@@ -18,9 +18,9 @@ package network
18
18
19
19
import (
20
20
"io/ioutil"
21
- "log"
22
21
"os"
23
22
"testing"
23
+ "time"
24
24
25
25
p2ptest "github.com/ethereum/go-ethereum/p2p/testing"
26
26
"github.com/ethereum/go-ethereum/swarm/state"
@@ -35,6 +35,8 @@ func newHiveTester(t *testing.T, params *HiveParams, n int, store state.Store) (
35
35
return newBzzBaseTester (t , n , addr , DiscoverySpec , pp .Run ), pp
36
36
}
37
37
38
+ // TestRegisterAndConnect verifies that the protocol runs successfully
39
+ // and that the peer connection exists afterwards
38
40
func TestRegisterAndConnect (t * testing.T ) {
39
41
params := NewHiveParams ()
40
42
s , pp := newHiveTester (t , params , 1 , nil )
@@ -43,25 +45,57 @@ func TestRegisterAndConnect(t *testing.T) {
43
45
raddr := NewAddr (node )
44
46
pp .Register (raddr )
45
47
46
- // start the hive and wait for the connection
48
+ // start the hive
47
49
err := pp .Start (s .Server )
48
50
if err != nil {
49
51
t .Fatal (err )
50
52
}
51
53
defer pp .Stop ()
52
- // retrieve and broadcast
54
+
55
+ // both hive connect and disconect check have time delays
56
+ // therefore we need to verify that peer is connected
57
+ // so that we are sure that the disconnect timeout doesn't complete
58
+ // before the hive connect method is run at least once
59
+ timeout := time .After (time .Second )
60
+ for {
61
+ select {
62
+ case <- timeout :
63
+ t .Fatalf ("expected connection" )
64
+ default :
65
+ }
66
+ i := 0
67
+ pp .Kademlia .EachConn (nil , 256 , func (addr * Peer , po int ) bool {
68
+ i ++
69
+ return true
70
+ })
71
+ if i > 0 {
72
+ break
73
+ }
74
+ time .Sleep (time .Millisecond )
75
+ }
76
+
77
+ // check that the connection actually exists
78
+ // the timeout error means no disconnection events
79
+ // were received within the a certain timeout
53
80
err = s .TestDisconnected (& p2ptest.Disconnect {
54
81
Peer : s .Nodes [0 ].ID (),
55
82
Error : nil ,
56
83
})
57
84
58
85
if err == nil || err .Error () != "timed out waiting for peers to disconnect" {
59
- t .Fatalf ("expected peer to connect " )
86
+ t .Fatalf ("expected no disconnection event " )
60
87
}
61
88
}
62
89
90
+ // TestHiveStatePersistance creates a protocol simulation with n peers for a node
91
+ // After protocols complete, the node is shut down and the state is stored.
92
+ // Another simulation is created, where 0 nodes are created, but where the stored state is passed
93
+ // The test succeeds if all the peers from the stored state are known after the protocols of the
94
+ // second simulation have completed
95
+ //
96
+ // Actual connectivity is not in scope for this test, as the peers loaded from state are not known to
97
+ // the simulation; the test only verifies that the peers are known to the node
63
98
func TestHiveStatePersistance (t * testing.T ) {
64
- log .SetOutput (os .Stdout )
65
99
66
100
dir , err := ioutil .TempDir ("" , "hive_test_store" )
67
101
if err != nil {
@@ -84,34 +118,38 @@ func TestHiveStatePersistance(t *testing.T) {
84
118
peers [raddr .String ()] = true
85
119
}
86
120
87
- // start the hive and wait for the connection
121
+ // start and stop the hive
122
+ // the known peers should be saved upon stopping
88
123
err = pp .Start (s .Server )
89
124
if err != nil {
90
125
t .Fatal (err )
91
126
}
92
127
pp .Stop ()
93
128
store .Close ()
94
129
95
- persistedStore , err := state .NewDBStore (dir ) //start the hive with an empty dbstore
130
+ // start the hive with an empty dbstore
131
+ persistedStore , err := state .NewDBStore (dir )
96
132
if err != nil {
97
133
t .Fatal (err )
98
134
}
99
135
100
- s1 , pp := newHiveTester (t , params , 1 , persistedStore )
101
-
102
- //start the hive and wait for the connection
136
+ s1 , pp := newHiveTester (t , params , 0 , persistedStore )
103
137
138
+ // start the hive and check that we know of all expected peers
104
139
pp .Start (s1 .Server )
105
140
i := 0
106
141
pp .Kademlia .EachAddr (nil , 256 , func (addr * BzzAddr , po int ) bool {
107
142
delete (peers , addr .String ())
108
143
i ++
109
144
return true
110
145
})
146
+ // TODO remove this line when verified that test passes
147
+ time .Sleep (time .Second )
111
148
if i != 5 {
112
- t .Errorf ("invalid number of entries: got %v, want %v" , i , 5 )
149
+ t .Fatalf ("invalid number of entries: got %v, want %v" , i , 5 )
113
150
}
114
151
if len (peers ) != 0 {
115
152
t .Fatalf ("%d peers left over: %v" , len (peers ), peers )
116
153
}
154
+
117
155
}
0 commit comments