Skip to content

Commit

Permalink
Defer cancelfunc
Browse files Browse the repository at this point in the history
  • Loading branch information
geoff-vball committed Jan 24, 2024
1 parent 657b9e0 commit c756da2
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 18 deletions.
18 changes: 7 additions & 11 deletions tests/basic_relay.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,8 @@ func BasicRelay(network interfaces.LocalNetwork) {
log.Info("Test Relaying from Subnet A to Subnet B")

log.Info("Starting the relayer")
relayerCmd, relayerCancel := testUtils.RunRelayerExecutable(ctx, relayerConfigPath)
relayerCleanup := testUtils.RunRelayerExecutable(ctx, relayerConfigPath)
defer relayerCleanup()

// Sleep for some time to make sure relayer has started up and subscribed.
log.Info("Waiting for the relayer to start up")
Expand Down Expand Up @@ -122,8 +123,7 @@ func BasicRelay(network interfaces.LocalNetwork) {

log.Info("Finished sending warp message, closing down output channel")
// Cancel the command and stop the relayer
relayerCancel()
_ = relayerCmd.Wait()
relayerCleanup()

//
// Try Relaying Already Delivered Message
Expand Down Expand Up @@ -152,15 +152,15 @@ func BasicRelay(network interfaces.LocalNetwork) {

// Run the relayer
log.Info("Creating new relayer instance to test already delivered message")
relayerCmd, relayerCancel = testUtils.RunRelayerExecutable(ctx, relayerConfigPath)
relayerCleanup = testUtils.RunRelayerExecutable(ctx, relayerConfigPath)
defer relayerCleanup()

// We should not receive a new block on subnet B, since the relayer should have seen the Teleporter message was already delivered
log.Info("Waiting for 10s to ensure no new block confirmations on destination chain")
Consistently(newHeadsB, 10*time.Second, 500*time.Millisecond).ShouldNot(Receive())

// Cancel the command and stop the relayer
relayerCancel()
_ = relayerCmd.Wait()
relayerCleanup()

//
// Set StartBlockHeight in config
Expand All @@ -187,7 +187,7 @@ func BasicRelay(network interfaces.LocalNetwork) {
relayerConfigPath = writeRelayerConfig(modifiedRelayerConfig)

log.Info("Starting the relayer")
relayerCmd, relayerCancel = testUtils.RunRelayerExecutable(ctx, relayerConfigPath)
relayerCancel = testUtils.RunRelayerExecutable(ctx, relayerConfigPath)

Check failure on line 190 in tests/basic_relay.go

View workflow job for this annotation

GitHub Actions / Unit tests

undefined: relayerCancel
log.Info("Waiting for a new block confirmation on subnet B")
<-newHeadsB
delivered1, err := subnetBInfo.TeleporterMessenger.MessageReceived(
Expand All @@ -205,10 +205,6 @@ func BasicRelay(network interfaces.LocalNetwork) {
Expect(delivered1).Should(BeFalse())
Expect(delivered2).Should(BeFalse())
Expect(delivered3).Should(BeTrue())

// Cancel the command and stop the relayer
relayerCancel()
_ = relayerCmd.Wait()
}

func sendBasicTeleporterMessage(
Expand Down
7 changes: 2 additions & 5 deletions tests/manual_message.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,8 @@ func ManualMessage(network interfaces.LocalNetwork) {
defer sub.Unsubscribe()

log.Info("Starting the relayer")
relayerCmd, relayerCancel := testUtils.RunRelayerExecutable(ctx, relayerConfigPath)
relayerCleanup := testUtils.RunRelayerExecutable(ctx, relayerConfigPath)
defer relayerCleanup()

log.Info("Waiting for a new block confirmation on subnet B")
<-newHeadsB
Expand All @@ -125,10 +126,6 @@ func ManualMessage(network interfaces.LocalNetwork) {
)
Expect(err).Should(BeNil())
Expect(delivered2).Should(BeFalse())

// Cancel the command and stop the relayer
relayerCancel()
_ = relayerCmd.Wait()
}

func getWarpMessageFromLog(ctx context.Context, receipt *types.Receipt, source interfaces.SubnetTestInfo) *avalancheWarp.UnsignedMessage {
Expand Down
7 changes: 5 additions & 2 deletions tests/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ var (
storageLocation = fmt.Sprintf("%s/.awm-relayer-storage", os.TempDir())
)

func RunRelayerExecutable(ctx context.Context, relayerConfigPath string) (*exec.Cmd, context.CancelFunc) {
func RunRelayerExecutable(ctx context.Context, relayerConfigPath string) context.CancelFunc {
cmdOutput := make(chan string)

// Run awm relayer binary with config path
Expand Down Expand Up @@ -61,7 +61,10 @@ func RunRelayerExecutable(ctx context.Context, relayerConfigPath string) (*exec.
}
cmdOutput <- "Command execution finished"
}()
return relayerCmd, relayerCancel
return func() {
relayerCancel()
relayerCmd.Wait()
}
}

func ReadHexTextFile(filename string) string {
Expand Down

0 comments on commit c756da2

Please sign in to comment.