@@ -21,6 +21,7 @@ import (
2121 "context"
2222 "errors"
2323 "fmt"
24+ "sync"
2425 "testing"
2526 "time"
2627
@@ -172,8 +173,11 @@ func protoHandshakeExchange(id enode.ID, proto *protoHandshake) []p2ptest.Exchan
172173}
173174
174175func runProtoHandshake (t * testing.T , proto * protoHandshake , errs ... error ) {
176+ t .Helper ()
175177 pp := p2ptest .NewTestPeerPool ()
176178 s := protocolTester (pp )
179+ defer s .Stop ()
180+
177181 // TODO: make this more than one handshake
178182 node := s .Nodes [0 ]
179183 if err := s .TestExchanges (protoHandshakeExchange (node .ID (), proto )... ); err != nil {
@@ -195,13 +199,17 @@ type dummyHook struct {
195199 send bool
196200 err error
197201 waitC chan struct {}
202+ mu sync.Mutex
198203}
199204
200205type dummyMsg struct {
201206 Content string
202207}
203208
204209func (d * dummyHook ) Send (peer * Peer , size uint32 , msg interface {}) error {
210+ d .mu .Lock ()
211+ defer d .mu .Unlock ()
212+
205213 d .peer = peer
206214 d .size = size
207215 d .msg = msg
@@ -210,6 +218,9 @@ func (d *dummyHook) Send(peer *Peer, size uint32, msg interface{}) error {
210218}
211219
212220func (d * dummyHook ) Receive (peer * Peer , size uint32 , msg interface {}) error {
221+ d .mu .Lock ()
222+ defer d .mu .Unlock ()
223+
213224 d .peer = peer
214225 d .size = size
215226 d .msg = msg
@@ -263,6 +274,7 @@ func TestProtocolHook(t *testing.T) {
263274 if err != nil {
264275 t .Fatal (err )
265276 }
277+ testHook .mu .Lock ()
266278 if testHook .msg == nil || testHook .msg .(* dummyMsg ).Content != "handshake" {
267279 t .Fatal ("Expected msg to be set, but it is not" )
268280 }
@@ -278,6 +290,7 @@ func TestProtocolHook(t *testing.T) {
278290 if testHook .size != 11 { //11 is the length of the encoded message
279291 t .Fatalf ("Expected size to be %d, but it is %d " , 1 , testHook .size )
280292 }
293+ testHook .mu .Unlock ()
281294
282295 err = tester .TestExchanges (p2ptest.Exchange {
283296 Triggers : []p2ptest.Trigger {
@@ -294,6 +307,8 @@ func TestProtocolHook(t *testing.T) {
294307 if err != nil {
295308 t .Fatal (err )
296309 }
310+
311+ testHook .mu .Lock ()
297312 if testHook .msg == nil || testHook .msg .(* dummyMsg ).Content != "response" {
298313 t .Fatal ("Expected msg to be set, but it is not" )
299314 }
@@ -306,6 +321,7 @@ func TestProtocolHook(t *testing.T) {
306321 if testHook .size != 10 { //11 is the length of the encoded message
307322 t .Fatalf ("Expected size to be %d, but it is %d " , 1 , testHook .size )
308323 }
324+ testHook .mu .Unlock ()
309325
310326 testHook .err = fmt .Errorf ("dummy error" )
311327 err = tester .TestExchanges (p2ptest.Exchange {
@@ -325,7 +341,6 @@ func TestProtocolHook(t *testing.T) {
325341 if err != nil {
326342 t .Fatalf ("Expected a specific disconnect error, but got different one: %v" , err )
327343 }
328-
329344}
330345
331346//We need to test that if the hook is not defined, then message infrastructure
@@ -342,16 +357,19 @@ func TestNoHook(t *testing.T) {
342357 ctx := context .TODO ()
343358 msg := & perBytesMsgSenderPays {Content : "testBalance" }
344359 //send a message
345- err := peer . Send ( ctx , msg )
346- if err != nil {
360+
361+ if err := peer . Send ( ctx , msg ); err != nil {
347362 t .Fatal (err )
348363 }
349364 //simulate receiving a message
350365 rw .msg = msg
351- peer . handleIncoming ( func (ctx context.Context , msg interface {}) error {
366+ handler := func (ctx context.Context , msg interface {}) error {
352367 return nil
353- })
354- //all should just work and not result in any error
368+ }
369+
370+ if err := peer .handleIncoming (handler ); err != nil {
371+ t .Fatal (err )
372+ }
355373}
356374
357375func TestProtoHandshakeVersionMismatch (t * testing.T ) {
@@ -391,8 +409,11 @@ func moduleHandshakeExchange(id enode.ID, resp uint) []p2ptest.Exchange {
391409}
392410
393411func runModuleHandshake (t * testing.T , resp uint , errs ... error ) {
412+ t .Helper ()
394413 pp := p2ptest .NewTestPeerPool ()
395414 s := protocolTester (pp )
415+ defer s .Stop ()
416+
396417 node := s .Nodes [0 ]
397418 if err := s .TestExchanges (protoHandshakeExchange (node .ID (), & protoHandshake {42 , "420" })... ); err != nil {
398419 t .Fatal (err )
@@ -471,8 +492,10 @@ func testMultiPeerSetup(a, b enode.ID) []p2ptest.Exchange {
471492}
472493
473494func runMultiplePeers (t * testing.T , peer int , errs ... error ) {
495+ t .Helper ()
474496 pp := p2ptest .NewTestPeerPool ()
475497 s := protocolTester (pp )
498+ defer s .Stop ()
476499
477500 if err := s .TestExchanges (testMultiPeerSetup (s .Nodes [0 ].ID (), s .Nodes [1 ].ID ())... ); err != nil {
478501 t .Fatal (err )
@@ -542,14 +565,14 @@ WAIT:
542565 }
543566
544567}
545- func XTestMultiplePeersDropSelf (t * testing.T ) {
568+ func TestMultiplePeersDropSelf (t * testing.T ) {
546569 runMultiplePeers (t , 0 ,
547570 fmt .Errorf ("subprotocol error" ),
548571 fmt .Errorf ("Message handler error: (msg code 3): dropped" ),
549572 )
550573}
551574
552- func XTestMultiplePeersDropOther (t * testing.T ) {
575+ func TestMultiplePeersDropOther (t * testing.T ) {
553576 runMultiplePeers (t , 1 ,
554577 fmt .Errorf ("Message handler error: (msg code 3): dropped" ),
555578 fmt .Errorf ("subprotocol error" ),
0 commit comments