Skip to content

Commit 0abe543

Browse files
committed
fix: P2P integration test
1 parent fd0e7d3 commit 0abe543

File tree

1 file changed

+44
-38
lines changed

1 file changed

+44
-38
lines changed

rolling-shutter/p2p/p2p_test.go

Lines changed: 44 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -4,29 +4,32 @@ import (
44
"bytes"
55
"context"
66
"fmt"
7-
"sync"
87
"testing"
98
"time"
109

1110
pubsub "github.com/libp2p/go-libp2p-pubsub"
11+
"github.com/pkg/errors"
1212
"github.com/rs/zerolog/log"
1313
"gotest.tools/assert"
1414

1515
"github.com/shutter-network/rolling-shutter/rolling-shutter/medley/encodeable"
1616
"github.com/shutter-network/rolling-shutter/rolling-shutter/medley/encodeable/address"
17+
"github.com/shutter-network/rolling-shutter/rolling-shutter/medley/service"
1718
"github.com/shutter-network/rolling-shutter/rolling-shutter/medley/testlog"
1819
)
1920

2021
func init() {
2122
testlog.Setup()
2223
}
2324

25+
var ErrTestComplete = errors.New("test complete")
26+
2427
// TestStartNetworkNode test that we can init two p2p nodes and make them send/receive messages.
2528
func TestStartNetworkNodeIntegration(t *testing.T) {
2629
if testing.Short() {
2730
t.Skip("skipping integration test")
2831
}
29-
ctx, cancel := context.WithTimeout(context.Background(), 1200*time.Millisecond)
32+
ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
3033
defer cancel()
3134

3235
numBootstrappers := 2
@@ -82,51 +85,54 @@ func TestStartNetworkNodeIntegration(t *testing.T) {
8285
gossipTopicNames := []string{"testTopic1", "testTopic2"}
8386
testMessage := []byte("test message")
8487

85-
runctx, stopRun := context.WithCancel(ctx)
86-
87-
waitGroup := sync.WaitGroup{}
8888
p2ps := []*P2PNode{}
89-
for _, cfg := range configs {
89+
services := make([]service.Service, len(configs))
90+
for i, cfg := range configs {
9091
p2pHandler, err := New(cfg)
9192
assert.NilError(t, err)
9293
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}
9998
}
100-
defer func() {
101-
stopRun()
102-
waitGroup.Wait()
103-
}()
99+
104100
// The following loop publishes the same message over and over. Even though we did call
105101
// 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+
}
112111

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):
118121
}
119-
case <-ctx.Done():
120-
t.Fatalf("waiting for message: %s", ctx.Err())
121-
case <-time.After(5 * time.Millisecond):
122122
}
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
123132
}
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())
132138
}

0 commit comments

Comments
 (0)