@@ -4,29 +4,32 @@ import (
4
4
"bytes"
5
5
"context"
6
6
"fmt"
7
- "sync"
8
7
"testing"
9
8
"time"
10
9
11
10
pubsub "github.com/libp2p/go-libp2p-pubsub"
11
+ "github.com/pkg/errors"
12
12
"github.com/rs/zerolog/log"
13
13
"gotest.tools/assert"
14
14
15
15
"github.com/shutter-network/rolling-shutter/rolling-shutter/medley/encodeable"
16
16
"github.com/shutter-network/rolling-shutter/rolling-shutter/medley/encodeable/address"
17
+ "github.com/shutter-network/rolling-shutter/rolling-shutter/medley/service"
17
18
"github.com/shutter-network/rolling-shutter/rolling-shutter/medley/testlog"
18
19
)
19
20
20
21
func init () {
21
22
testlog .Setup ()
22
23
}
23
24
25
+ var ErrTestComplete = errors .New ("test complete" )
26
+
24
27
// TestStartNetworkNode test that we can init two p2p nodes and make them send/receive messages.
25
28
func TestStartNetworkNodeIntegration (t * testing.T ) {
26
29
if testing .Short () {
27
30
t .Skip ("skipping integration test" )
28
31
}
29
- ctx , cancel := context .WithTimeout (context .Background (), 1200 * time .Millisecond )
32
+ ctx , cancel := context .WithTimeout (context .Background (), 20 * time .Second )
30
33
defer cancel ()
31
34
32
35
numBootstrappers := 2
@@ -82,51 +85,54 @@ func TestStartNetworkNodeIntegration(t *testing.T) {
82
85
gossipTopicNames := []string {"testTopic1" , "testTopic2" }
83
86
testMessage := []byte ("test message" )
84
87
85
- runctx , stopRun := context .WithCancel (ctx )
86
-
87
- waitGroup := sync.WaitGroup {}
88
88
p2ps := []* P2PNode {}
89
- for _ , cfg := range configs {
89
+ services := make ([]service.Service , len (configs ))
90
+ for i , cfg := range configs {
90
91
p2pHandler , err := New (cfg )
91
92
assert .NilError (t , err )
92
93
p2ps = append (p2ps , p2pHandler .P2P )
93
- waitGroup .Add (1 )
94
- go func () {
95
- defer waitGroup .Done ()
96
- err := p2pHandler .P2P .Run (runctx , gossipTopicNames , map [string ]pubsub.ValidatorEx {})
97
- assert .Assert (t , err == context .Canceled )
98
- }()
94
+ fn := func (ctx context.Context , runner service.Runner ) error {
95
+ return p2pHandler .P2P .Run (ctx , runner , gossipTopicNames , map [string ]pubsub.ValidatorEx {})
96
+ }
97
+ services [i ] = service.Function {Func : fn }
99
98
}
100
- defer func () {
101
- stopRun ()
102
- waitGroup .Wait ()
103
- }()
99
+
104
100
// The following loop publishes the same message over and over. Even though we did call
105
101
// ConnectToPeer, libp2p takes some time until the peer receives the first message.
106
- var message * pubsub.Message
107
- topicName := gossipTopicNames [0 ]
108
- for message == nil {
109
- if err := p2ps [1 ].Publish (ctx , topicName , testMessage ); err != nil {
110
- t .Fatalf ("error while publishing message: %v" , err )
111
- }
102
+ testFn := func (ctx context.Context , _ service.Runner ) error {
103
+ // HACK:
104
+ time .Sleep (1 * time .Second )
105
+ var message * pubsub.Message
106
+ topicName := gossipTopicNames [0 ]
107
+ for message == nil {
108
+ if err := p2ps [1 ].Publish (ctx , topicName , testMessage ); err != nil {
109
+ t .Fatalf ("error while publishing message: %v" , err )
110
+ }
112
111
113
- select {
114
- case message = <- p2ps [0 ].GossipMessages :
115
- log .Info ().Interface ("message" , message ).Msg ("got message" )
116
- if message == nil {
117
- t .Fatalf ("channel closed unexpectedly" )
112
+ select {
113
+ case message = <- p2ps [0 ].GossipMessages :
114
+ log .Info ().Interface ("message" , message ).Msg ("got message" )
115
+ if message == nil {
116
+ t .Fatalf ("channel closed unexpectedly" )
117
+ }
118
+ case <- ctx .Done ():
119
+ t .Fatalf ("waiting for message: %s" , ctx .Err ())
120
+ case <- time .After (5 * time .Millisecond ):
118
121
}
119
- case <- ctx .Done ():
120
- t .Fatalf ("waiting for message: %s" , ctx .Err ())
121
- case <- time .After (5 * time .Millisecond ):
122
122
}
123
+ assert .Equal (t , topicName , message .GetTopic (), "received message with wrong topic" )
124
+ assert .Check (t , bytes .Equal (testMessage , message .GetData ()), "received wrong message" )
125
+ assert .Equal (
126
+ t ,
127
+ p2ps [1 ].HostID (),
128
+ message .GetFrom ().String (),
129
+ "received message with wrong sender" ,
130
+ )
131
+ return ErrTestComplete
123
132
}
124
- assert .Equal (t , topicName , message .GetTopic (), "received message with wrong topic" )
125
- assert .Check (t , bytes .Equal (testMessage , message .GetData ()), "received wrong message" )
126
- assert .Equal (
127
- t ,
128
- p2ps [1 ].HostID (),
129
- message .GetFrom ().String (),
130
- "received message with wrong sender" ,
131
- )
133
+ testService := service.Function {Func : testFn }
134
+ services = append (services , testService )
135
+
136
+ err := service .Run (ctx , services ... )
137
+ assert .Error (t , err , ErrTestComplete .Error ())
132
138
}
0 commit comments