-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjitter_test.go
87 lines (80 loc) · 2.64 KB
/
jitter_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package main
import (
"fmt"
"math"
"os/exec"
"testing"
"time"
)
// Ensure that --jitter requires an argument
func TestJitterRequiresArg(t *testing.T) {
out, err := exec.Command("go", "run", "cronwrap.go", "--jitter").CombinedOutput()
if err == nil {
t.Error(string(out))
}
}
// Ensure that the argument must be a time delta
func TestJitterArgIsDelta(t *testing.T) {
out, err := exec.Command("go", "run", "cronwrap.go", "--jitter", "bogus", "true").CombinedOutput()
if err == nil {
t.Error(string(out))
}
out, err = exec.Command("go", "run", "cronwrap.go", "--jitter", "1r", "true").CombinedOutput()
if err == nil {
t.Error(string(out))
}
out, err = exec.Command("go", "run", "cronwrap.go", "--jitter", "1", "true").CombinedOutput()
if err == nil {
t.Error(string(out))
}
out, err = exec.Command("go", "run", "cronwrap.go", "--jitter", "0", "true").CombinedOutput()
if err != nil {
t.Error(string(out))
}
out, err = exec.Command("go", "run", "cronwrap.go", "--jitter", "1s", "true").CombinedOutput()
if err != nil {
t.Error(string(out))
}
// out, err = exec.Command("go", "run", "cronwrap.go", "--jitter", "1m", "true").CombinedOutput()
// if err != nil {
// t.Error(string(out))
// }
// out, err = exec.Command("go", "run", "cronwrap.go", "--jitter", "1h", "true").CombinedOutput()
// if err != nil {
// t.Error(string(out))
// }
}
// I actually don't know how to test that the job was delayed, as some machines
// are going to have a jitter value very close to zero.
//
// However, we do expect the jitter to be consistent on any given machine. That
// we can test. One minute (--jitter 1m) is good enough for testing as cronwrap
// actually sleeps for a random number of seconds, so even with one minute of
// jitter cronwrap will sleep anywhere from 0-59 seconds.
func TestJitterIsConsistent(t *testing.T) {
// FIXME: It would speed up the test suite considerably to run all three tests
// simultaneously in goroutines and compare the results
// Time one run
start := time.Now()
err := exec.Command("go", "run", "cronwrap.go", "--jitter", "1m", "true").Run()
end := time.Now()
elapsed := end.Sub(start)
if err != nil {
t.FailNow()
}
// Now verify that a few more runs are within one second of the same delay
allowed_diff := 2
for i := 1; i <= 2; i++ {
start := time.Now()
err := exec.Command("go", "run", "cronwrap.go", "--jitter", "1m", "true").Run()
end := time.Now()
test_elapsed := end.Sub(start)
if err != nil {
t.FailNow()
}
diff := math.Abs(test_elapsed.Seconds() - elapsed.Seconds())
if diff > float64(allowed_diff) {
t.Error(fmt.Sprintf("Expected diff <= %d, was %f", allowed_diff, diff))
}
}
}