Skip to content

Commit

Permalink
Extract common channel handling to helper func
Browse files Browse the repository at this point in the history
  • Loading branch information
iansuvak committed Aug 16, 2024
1 parent fa3d590 commit c182fa6
Show file tree
Hide file tree
Showing 9 changed files with 27 additions and 80 deletions.
29 changes: 4 additions & 25 deletions tests/allowed_addresses.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,12 +177,7 @@ func AllowedAddresses(network interfaces.LocalNetwork) {

// Wait for relayer to start up
log.Info("Waiting for the relayer to start up")
select {
case <-readyChan:
close(readyChan)
case <-time.After(15 * time.Second):
Expect(false).To(BeTrue(), "Relayer did not start up in time")
}
testUtils.WaitForChannelClose(readyChan, 15*time.Second)

// Allowed by Relayer 1
testUtils.RelayBasicMessage(
Expand All @@ -206,12 +201,7 @@ func AllowedAddresses(network interfaces.LocalNetwork) {

// Wait for relayer to start up
log.Info("Waiting for the relayer to start up")
select {
case <-readyChan:
close(readyChan)
case <-time.After(15 * time.Second):
Expect(false).To(BeTrue(), "Relayer did not start up in time")
}
testUtils.WaitForChannelClose(readyChan, 15*time.Second)

// Disallowed by Relayer 2
_, _, id := testUtils.SendBasicTeleporterMessage(
Expand Down Expand Up @@ -251,12 +241,7 @@ func AllowedAddresses(network interfaces.LocalNetwork) {

// Wait for relayer to start up
log.Info("Waiting for the relayer to start up")
select {
case <-readyChan:
close(readyChan)
case <-time.After(15 * time.Second):
Expect(false).To(BeTrue(), "Relayer did not start up in time")
}
testUtils.WaitForChannelClose(readyChan, 15*time.Second)

// Disallowed by Relayer 3
_, _, id = testUtils.SendBasicTeleporterMessage(
Expand Down Expand Up @@ -295,13 +280,7 @@ func AllowedAddresses(network interfaces.LocalNetwork) {
defer relayerCleanup()

// Wait for relayer to start up
log.Info("Waiting for the relayer to start up")
select {
case <-readyChan:
close(readyChan)
case <-time.After(15 * time.Second):
Expect(false).To(BeTrue(), "Relayer did not start up in time")
}
testUtils.WaitForChannelClose(readyChan, 15*time.Second)

// Disallowed by Relayer 4
_, _, id = testUtils.SendBasicTeleporterMessage(
Expand Down
14 changes: 2 additions & 12 deletions tests/basic_relay.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,7 @@ func BasicRelay(network interfaces.LocalNetwork) {
defer relayerCleanup()

// Wait for relayer to start up
log.Info("Waiting for the relayer to start up")
select {
case <-readyChan:
case <-time.After(15 * time.Second):
Expect(false).To(BeTrue(), "Relayer did not start up in time")
}
testUtils.WaitForChannelClose(readyChan, 15*time.Second)

log.Info("Sending transaction from Subnet A to Subnet B")
testUtils.RelayBasicMessage(
Expand Down Expand Up @@ -152,12 +147,7 @@ func BasicRelay(network interfaces.LocalNetwork) {

// Wait for relayer to start up
log.Info("Waiting for the relayer to start up")
select {
case <-readyChan:
close(readyChan)
case <-time.After(15 * time.Second):
Expect(false).To(BeTrue(), "Relayer did not start up in time")
}
testUtils.WaitForChannelClose(readyChan, 15*time.Second)

// We should not receive a new block on subnet B, since the relayer should have
// seen the Teleporter message was already delivered.
Expand Down
7 changes: 1 addition & 6 deletions tests/batch_relay.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,7 @@ func BatchRelay(network interfaces.LocalNetwork) {

// Wait for relayer to start up
log.Info("Waiting for the relayer to start up")
select {
case <-readyChan:
close(readyChan)
case <-time.After(15 * time.Second):
Expect(false).To(BeTrue(), "Relayer did not start up in time")
}
testUtils.WaitForChannelClose(readyChan, 15*time.Second)

//
// Send a batch message from subnet A -> B
Expand Down
6 changes: 1 addition & 5 deletions tests/manual_message.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,11 +114,7 @@ func ManualMessage(network interfaces.LocalNetwork) {

// Wait for relayer to startup.
log.Info("Waiting for the relayer to start up")
select {
case <-readyChan:
case <-time.After(15 * time.Second):
Expect(false).To(BeTrue(), "Relayer did not start up in time")
}
testUtils.WaitForChannelClose(readyChan, 15*time.Second)

reqBody := api.ManualWarpMessageRequest{
UnsignedMessageBytes: unsignedMessage.Bytes(),
Expand Down
7 changes: 1 addition & 6 deletions tests/relay_message_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,7 @@ func RelayMessageAPI(network interfaces.LocalNetwork) {

// Wait for relayer to start up
log.Info("Waiting for the relayer to start up")
select {
case <-readyChan:
close(readyChan)
case <-time.After(15 * time.Second):
Expect(false).To(BeTrue(), "Relayer did not start up in time")
}
testUtils.WaitForChannelClose(readyChan, 15*time.Second)

reqBody := api.RelayMessageRequest{
BlockchainID: subnetAInfo.BlockchainID.String(),
Expand Down
8 changes: 6 additions & 2 deletions tests/shared_db.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,12 @@ func SharedDatabaseAccess(network interfaces.LocalNetwork) {
log.Info("Waiting for the relayers to start up")
var wg sync.WaitGroup
wg.Add(2)
go testUtils.WaitFunc(&wg, readyChanA)
go testUtils.WaitFunc(&wg, readyChanB)
waitFunc := func(wg *sync.WaitGroup, readyChan chan struct{}) {
defer wg.Done()
<-readyChan
}
go waitFunc(&wg, readyChanA)
go waitFunc(&wg, readyChanB)
wg.Wait()

log.Info("Sending transaction from Subnet A to Subnet B")
Expand Down
7 changes: 1 addition & 6 deletions tests/signature_aggregator_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,7 @@ func SignatureAggregatorAPI(network interfaces.LocalNetwork) {

// Wait for signature-aggregator to start up
log.Info("Waiting for the relayer to start up")
select {
case <-readyChan:
close(readyChan)
case <-time.After(15 * time.Second):
Expect(false).To(BeTrue(), "Signature Aggregator did not start up in time")
}
testUtils.WaitForChannelClose(readyChan, 15*time.Second)

// End setup step
// Begin Test Case 1
Expand Down
22 changes: 10 additions & 12 deletions tests/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
"os"
"os/exec"
"strings"
"sync"
"time"

"github.com/ava-labs/avalanchego/ids"
Expand Down Expand Up @@ -490,13 +489,7 @@ func TriggerProcessMissedBlocks(
defer relayerCleanup()

// Wait for relayer to start up
log.Info("Waiting for the relayer to start up")
select {
case <-readyChan:
close(readyChan)
case <-time.After(15 * time.Second):
Expect(false).To(BeTrue(), "Relayer did not start up in time")
}
WaitForChannelClose(readyChan, 15*time.Second)

log.Info("Waiting for a new block confirmation on the destination")
<-newHeads
Expand Down Expand Up @@ -566,7 +559,7 @@ func runExecutable(
for scanner.Scan() {
text := scanner.Text()
if strings.Contains(text, "Initialization complete") {
readyChan <- struct{}{}
close(readyChan)
}
log.Info(text)
}
Expand All @@ -590,7 +583,12 @@ func runExecutable(
return readyChan
}

func WaitFunc(wg *sync.WaitGroup, ch chan struct{}) {
defer wg.Done()
<-ch
// Helper function that waits for a signaling channel to be closed
// or throws an error if the channel is not closed in time
func WaitForChannelClose(ch <-chan struct{}, timeout time.Duration) {
select {
case <-ch:
case <-time.After(timeout):
Expect(false).To(BeTrue(), "Channel did not close in time")
}
}
7 changes: 1 addition & 6 deletions tests/warp_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,7 @@ func WarpAPIRelay(network interfaces.LocalNetwork) {

// Wait for relayer to start up
log.Info("Waiting for the relayer to start up")
select {
case <-readyChan:
close(readyChan)
case <-time.After(15 * time.Second):
Expect(false).To(BeTrue(), "Relayer did not start up in time")
}
testUtils.WaitForChannelClose(readyChan, 15*time.Second)

log.Info("Sending transaction from Subnet A to Subnet B")
testUtils.RelayBasicMessage(
Expand Down

0 comments on commit c182fa6

Please sign in to comment.