Skip to content

Commit

Permalink
Fix flaky TestNode_RunCoordinationLayer test
Browse files Browse the repository at this point in the history
  • Loading branch information
lukasz-zimnoch committed Nov 29, 2023
1 parent a35d7b3 commit b4582db
Showing 1 changed file with 23 additions and 14 deletions.
37 changes: 23 additions & 14 deletions pkg/tbtc/node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -324,13 +324,13 @@ func TestNode_RunCoordinationLayer(t *testing.T) {
return nil, false
}

// Simply add processed results to the list.
var processedResults []*coordinationResult
// Simply pass processed results to the channel.
processedResultsChan := make(chan *coordinationResult, 5)
processCoordinationResultFn := func(
_ *node,
result *coordinationResult,
) {
processedResults = append(processedResults, result)
processedResultsChan <- result
}

ctx, cancelCtx := context.WithCancel(context.Background())
Expand All @@ -347,21 +347,30 @@ func TestNode_RunCoordinationLayer(t *testing.T) {
t.Fatal(err)
}

// Wait until the second-last coordination window passes.
err = localChain.blockCounter.WaitForBlockHeight(4000)
// Set up a stop signal that will be triggered after the last coordination
// window passes.
waiter, err := localChain.blockCounter.BlockHeightWaiter(5000)
if err != nil {
t.Fatal(err)
}

// Stop coordination layer. As we are between the second-last and the last
// coordination window, the last window should not be processed. This
// allows us to test that the coordination layer's shutdown works as expected.
cancelCtx()

// Wait until the last coordination window passes.
err = localChain.blockCounter.WaitForBlockHeight(5000)
if err != nil {
t.Fatal(err)
var processedResults []*coordinationResult
loop:
for {
select {
case result := <-processedResultsChan:
processedResults = append(processedResults, result)

// Once the second-last coordination window is processed, stop the
// coordination layer. In that case, the last window should not be
// processed. This allows us to test that the coordination layer's
// shutdown works as expected.
if len(processedResults) == 3 {
cancelCtx()
}
case <-waiter:
break loop
}
}

testutils.AssertIntsEqual(
Expand Down

0 comments on commit b4582db

Please sign in to comment.