Skip to content

Commit 92bd161

Browse files
committed
Add retry interval
1 parent d2db150 commit 92bd161

File tree

6 files changed

+51
-2
lines changed

6 files changed

+51
-2
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# v1.2.0
2+
3+
- Add `interval` option for `retries` which allows to execute a retry after a given period of time. I.e. `interval: 50ms`
4+
15
# v1.1.0
26

37
- Add `not-contains` assertion on `stdout` and `stderr`

commander_unix.yaml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,5 +52,7 @@ tests:
5252
stdout:
5353
contains:
5454
- ✗ echo hello, retries 3
55-
- ✓ ./integration/unix/_fixtures/retries.sh, retries 2
55+
- ✓ it should retry failed commands, retries 2
56+
- ✗ it should retry failed commands with an interval, retries 2
57+
- "Duration: 0.1" # Assertion that the interval is working
5658
exit-code: 1

docs/manual.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,5 +68,6 @@ tests:
6868
ANOTHER: yeah # Add another env variable
6969
timeout: 1000 # Overwrite timeout
7070
retries: 5
71+
interval: 30ms
7172
exit-code: 0
7273
```

integration/unix/retries.yaml

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,16 @@ tests:
44
config:
55
retries: 3
66

7-
./integration/unix/_fixtures/retries.sh:
7+
it should retry failed commands:
8+
command: ./integration/unix/_fixtures/retries.sh
89
exit-code: 0
910
config:
11+
retries: 2
12+
13+
it should retry failed commands with an interval:
14+
command: echo hello
15+
stdout: fail
16+
exit-code: 0
17+
config:
18+
interval: 50ms
1019
retries: 2

pkg/runtime/runtime.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
package runtime
22

33
import (
4+
"fmt"
45
"github.com/SimonBaeumer/commander/pkg/cmd"
56
"log"
67
"runtime"
78
"strings"
89
"sync"
10+
"time"
911
)
1012

1113
// Constants for defining the various tested properties
@@ -122,6 +124,14 @@ func Start(tests []TestCase, maxConcurrent int) <-chan TestResult {
122124
if result.ValidationResult.Success {
123125
break
124126
}
127+
128+
if t.Command.GetRetries() > 1 && t.Command.Interval != "" {
129+
interval, err := time.ParseDuration(t.Command.Interval)
130+
if err != nil {
131+
panic(fmt.Sprintf("'%s' interval error: %s", t.Command.Cmd, err))
132+
}
133+
time.Sleep(interval)
134+
}
125135
}
126136

127137
out <- result

pkg/runtime/runtime_test.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"github.com/stretchr/testify/assert"
66
"runtime"
77
"testing"
8+
"time"
89
)
910

1011
const SingleConcurrent = 1
@@ -37,7 +38,29 @@ func TestRuntime_WithRetries(t *testing.T) {
3738
assert.False(t, r.ValidationResult.Success)
3839
assert.Equal(t, 3, r.Tries)
3940
}
41+
42+
assert.Equal(t, 1, counter)
43+
}
44+
45+
func TestRuntime_WithRetriesAndInterval(t *testing.T) {
46+
s := getExampleTestSuite()
47+
s[0].Command.Retries = 3
48+
s[0].Command.Cmd = "echo fail"
49+
s[0].Command.Interval = "50ms"
50+
51+
start := time.Now()
52+
got := Start(s, 1)
53+
54+
var counter = 0
55+
for r := range got {
56+
counter++
57+
assert.False(t, r.ValidationResult.Success)
58+
assert.Equal(t, 3, r.Tries)
59+
}
60+
duration := time.Since(start)
61+
4062
assert.Equal(t, 1, counter)
63+
assert.True(t, duration.Seconds() > 0.15, "Retry interval did not work")
4164
}
4265

4366
func TestRuntime_WithEnvVariables(t *testing.T) {

0 commit comments

Comments
 (0)