Skip to content

Commit 3e0fd8d

Browse files
authored
testing: Use context in integration test (#539)
Cancellable contexts are use for proper node shutdown.
1 parent 6a7b5f7 commit 3e0fd8d

1 file changed

Lines changed: 23 additions & 7 deletions

File tree

node/integration_test.go

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,9 @@ func TestTxGossipingAndAggregation(t *testing.T) {
8989

9090
var wg sync.WaitGroup
9191
clientNodes := 4
92-
nodes, apps := createNodes(clientNodes+1, &wg, t)
92+
aggCtx, aggCancel := context.WithCancel(context.Background())
93+
ctx, cancel := context.WithCancel(context.Background())
94+
nodes, apps := createNodes(aggCtx, ctx, clientNodes+1, &wg, t)
9395

9496
wg.Add((clientNodes + 1) * clientNodes)
9597
for _, n := range nodes {
@@ -115,9 +117,13 @@ func TestTxGossipingAndAggregation(t *testing.T) {
115117
t.Fatal("failing after timeout")
116118
}
117119

118-
for _, n := range nodes {
120+
require.NoError(nodes[0].Stop())
121+
aggCancel()
122+
time.Sleep(100 * time.Millisecond)
123+
for _, n := range nodes[1:] {
119124
require.NoError(n.Stop())
120125
}
126+
cancel()
121127
time.Sleep(100 * time.Millisecond)
122128
aggApp := apps[0]
123129
apps = apps[1:]
@@ -160,9 +166,16 @@ func TestTxGossipingAndAggregation(t *testing.T) {
160166
}
161167
}
162168

163-
func createNodes(num int, wg *sync.WaitGroup, t *testing.T) ([]*Node, []*mocks.Application) {
169+
func createNodes(aggCtx, ctx context.Context, num int, wg *sync.WaitGroup, t *testing.T) ([]*Node, []*mocks.Application) {
164170
t.Helper()
165171

172+
if aggCtx == nil {
173+
aggCtx = context.Background()
174+
}
175+
if ctx == nil {
176+
ctx = context.Background()
177+
}
178+
166179
// create keys first, as they are required for P2P connections
167180
keys := make([]crypto.PrivKey, num)
168181
for i := 0; i < num; i++ {
@@ -175,15 +188,15 @@ func createNodes(num int, wg *sync.WaitGroup, t *testing.T) ([]*Node, []*mocks.A
175188
_ = dalc.Init([8]byte{}, nil, store.NewDefaultInMemoryKVStore(), log.TestingLogger())
176189
_ = dalc.Start()
177190

178-
nodes[0], apps[0] = createNode(0, true, dalc, keys, wg, t)
191+
nodes[0], apps[0] = createNode(aggCtx, 0, true, dalc, keys, wg, t)
179192
for i := 1; i < num; i++ {
180-
nodes[i], apps[i] = createNode(i, false, dalc, keys, wg, t)
193+
nodes[i], apps[i] = createNode(ctx, i, false, dalc, keys, wg, t)
181194
}
182195

183196
return nodes, apps
184197
}
185198

186-
func createNode(n int, aggregator bool, dalc da.DataAvailabilityLayerClient, keys []crypto.PrivKey, wg *sync.WaitGroup, t *testing.T) (*Node, *mocks.Application) {
199+
func createNode(ctx context.Context, n int, aggregator bool, dalc da.DataAvailabilityLayerClient, keys []crypto.PrivKey, wg *sync.WaitGroup, t *testing.T) (*Node, *mocks.Application) {
187200
t.Helper()
188201
require := require.New(t)
189202
// nodes will listen on consecutive ports on local interface
@@ -216,10 +229,13 @@ func createNode(n int, aggregator bool, dalc da.DataAvailabilityLayerClient, key
216229
app.On("DeliverTx", mock.Anything).Return(abci.ResponseDeliverTx{}).Run(func(args mock.Arguments) {
217230
wg.Done()
218231
})
232+
if ctx == nil {
233+
ctx = context.Background()
234+
}
219235

220236
signingKey, _, _ := crypto.GenerateEd25519Key(rand.Reader)
221237
node, err := NewNode(
222-
context.Background(),
238+
ctx,
223239
config.NodeConfig{
224240
P2P: p2pConfig,
225241
DALayer: "mock",

0 commit comments

Comments
 (0)