Skip to content
24 changes: 19 additions & 5 deletions test/cmd/txsim/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
if err != nil {
return fmt.Errorf("get poll time: %w", err)
}

opts := txsim.DefaultOptions().
Expand Down Expand Up @@ -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) (time.Duration, error) {
if flagValue != defaultValue {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this can be simplified using cmd.Flags().Changed() which would return true if the flag has been changed

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this can be simplified using cmd.Flags().Changed() which would return true if the flag has been changed

yes

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
}
29 changes: 29 additions & 0 deletions test/cmd/txsim/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
require.NoError(t, err)
require.Equal(t, defaultPoll, got)
})

t.Run("should return env", func(t *testing.T) {
got, err := getPollTime(defaultPoll, "20s", defaultPoll)
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)
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)
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.")
Expand Down