diff --git a/tests/utils/utils.go b/tests/utils/utils.go index 01df542d6..965fc63b2 100644 --- a/tests/utils/utils.go +++ b/tests/utils/utils.go @@ -1207,3 +1207,40 @@ func InstantiateGenesisTemplate( return subnetGenesisFile.Name() } + +// +// Teleporter message utils +// + +// Blocks until the given teleporter message is delivered to the specified TeleporterMessenger +// before the timeout, or if an error occurred. +func WaitTeleporterMessageDelivered( + ctx context.Context, + teleporterMessenger *teleportermessenger.TeleporterMessenger, + teleporterMessageID ids.ID, +) error { + cctx, cancel := context.WithTimeout(ctx, 20*time.Second) + defer cancel() + + queryTicker := time.NewTicker(200 * time.Millisecond) + defer queryTicker.Stop() + for { + delivered, err := teleporterMessenger.MessageReceived( + &bind.CallOpts{}, teleporterMessageID, + ) + if err != nil { + return err + } + + if delivered { + return nil + } + + // Wait for the next round. + select { + case <-cctx.Done(): + return cctx.Err() + case <-queryTicker.C: + } + } +}