@@ -18,9 +18,9 @@ package network
1818
1919import (
2020 "io/ioutil"
21- "log"
2221 "os"
2322 "testing"
23+ "time"
2424
2525 p2ptest "github.com/ethereum/go-ethereum/p2p/testing"
2626 "github.com/ethereum/go-ethereum/swarm/state"
@@ -35,6 +35,8 @@ func newHiveTester(t *testing.T, params *HiveParams, n int, store state.Store) (
3535 return newBzzBaseTester (t , n , addr , DiscoverySpec , pp .Run ), pp
3636}
3737
38+ // TestRegisterAndConnect verifies that the protocol runs successfully
39+ // and that the peer connection exists afterwards
3840func TestRegisterAndConnect (t * testing.T ) {
3941 params := NewHiveParams ()
4042 s , pp := newHiveTester (t , params , 1 , nil )
@@ -43,25 +45,57 @@ func TestRegisterAndConnect(t *testing.T) {
4345 raddr := NewAddr (node )
4446 pp .Register (raddr )
4547
46- // start the hive and wait for the connection
48+ // start the hive
4749 err := pp .Start (s .Server )
4850 if err != nil {
4951 t .Fatal (err )
5052 }
5153 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
5380 err = s .TestDisconnected (& p2ptest.Disconnect {
5481 Peer : s .Nodes [0 ].ID (),
5582 Error : nil ,
5683 })
5784
5885 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 " )
6087 }
6188}
6289
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
6398func TestHiveStatePersistance (t * testing.T ) {
64- log .SetOutput (os .Stdout )
6599
66100 dir , err := ioutil .TempDir ("" , "hive_test_store" )
67101 if err != nil {
@@ -84,34 +118,38 @@ func TestHiveStatePersistance(t *testing.T) {
84118 peers [raddr .String ()] = true
85119 }
86120
87- // start the hive and wait for the connection
121+ // start and stop the hive
122+ // the known peers should be saved upon stopping
88123 err = pp .Start (s .Server )
89124 if err != nil {
90125 t .Fatal (err )
91126 }
92127 pp .Stop ()
93128 store .Close ()
94129
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 )
96132 if err != nil {
97133 t .Fatal (err )
98134 }
99135
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 )
103137
138+ // start the hive and check that we know of all expected peers
104139 pp .Start (s1 .Server )
105140 i := 0
106141 pp .Kademlia .EachAddr (nil , 256 , func (addr * BzzAddr , po int ) bool {
107142 delete (peers , addr .String ())
108143 i ++
109144 return true
110145 })
146+ // TODO remove this line when verified that test passes
147+ time .Sleep (time .Second )
111148 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 )
113150 }
114151 if len (peers ) != 0 {
115152 t .Fatalf ("%d peers left over: %v" , len (peers ), peers )
116153 }
154+
117155}
0 commit comments