diff --git a/test/cmd/txsim/cli.go b/test/cmd/txsim/cli.go index 20dbce26b2..1b0b6b5660 100644 --- a/test/cmd/txsim/cli.go +++ b/test/cmd/txsim/cli.go @@ -177,11 +177,9 @@ account that can act as the master account. The command runs until all sequences } } - if os.Getenv(TxsimPoll) != "" && pollTime != user.DefaultPollTime { - pollTime, err = time.ParseDuration(os.Getenv(TxsimPoll)) - if err != nil { - return fmt.Errorf("parsing poll time: %w", err) - } + pollTime, err = getPollTime(pollTime, os.Getenv(TxsimPoll), user.DefaultPollTime, cmd.Flags().Changed("poll-time")) + if err != nil { + return fmt.Errorf("get poll time: %w", err) } opts := txsim.DefaultOptions(). @@ -296,3 +294,19 @@ func parseUpgradeSchedule(schedule string) (map[int64]uint64, error) { } return scheduleMap, nil } + +// getPollTime returns the pollTime value based on CLI flag, environment variable, and default. +// Priority: flag > env > default. +func getPollTime(flagValue time.Duration, envValue string, defaultValue time.Duration, flagChanged bool) (time.Duration, error) { + if flagChanged { + return flagValue, nil + } + if envValue != "" { + val, err := time.ParseDuration(envValue) + if err != nil { + return 0, fmt.Errorf("parsing poll time from env: %w", err) + } + return val, nil + } + return defaultValue, nil +} diff --git a/test/cmd/txsim/cli_test.go b/test/cmd/txsim/cli_test.go index 739a3f724a..94cfcdf468 100644 --- a/test/cmd/txsim/cli_test.go +++ b/test/cmd/txsim/cli_test.go @@ -90,6 +90,35 @@ func TestTxsimDefaultKeypath(t *testing.T) { require.NoError(t, err) } +func TestGetPollTime(t *testing.T) { + defaultPoll := 10 * time.Second + flagPoll := 30 * time.Second + + t.Run("should return default", func(t *testing.T) { + got, err := getPollTime(defaultPoll, "", defaultPoll, false) + require.NoError(t, err) + require.Equal(t, defaultPoll, got) + }) + + t.Run("should return env", func(t *testing.T) { + got, err := getPollTime(defaultPoll, "20s", defaultPoll, false) + require.NoError(t, err) + require.Equal(t, 20*time.Second, got) + }) + + t.Run("should return flag", func(t *testing.T) { + got, err := getPollTime(flagPoll, "20s", defaultPoll, true) + require.NoError(t, err) + require.Equal(t, flagPoll, got) + }) + + t.Run("should return error for invalid env", func(t *testing.T) { + _, err := getPollTime(defaultPoll, "notaduration", defaultPoll, false) + require.Error(t, err) + require.ErrorContains(t, err, "parsing poll time from env") + }) +} + func setup(t testing.TB) (keyring.Keyring, string, string) { if testing.Short() { t.Skip("skipping tx sim in short mode.")