-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclock_test.go
More file actions
41 lines (35 loc) · 1.02 KB
/
Copy pathclock_test.go
File metadata and controls
41 lines (35 loc) · 1.02 KB
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
package main
import (
"fmt"
"syscall"
"testing"
"time"
)
// On a very busy machine this test can be flaky
func TestRepeatUntilInterrupt(t *testing.T) {
tests := []struct {
totalRuntime int
interval int
}{
// For intervals < 10, notable delays happen too often on my machine
{80, 30},
{90, 40},
{70, 300}, // callback is never called
}
for _, test := range tests {
t.Run(fmt.Sprintf("%d/%d", test.totalRuntime, test.interval), func(t *testing.T) {
var called []string
// Kill ourselves after totalRuntime
time.AfterFunc(time.Duration(test.totalRuntime)*time.Millisecond, func() {
syscall.Kill(syscall.Getpid(), syscall.SIGINT)
})
repeatUntilInterrupt(func() {
called = append(called, "called")
}, time.Duration(test.interval)*time.Millisecond, syscall.SIGINT)
expectedCalls := test.totalRuntime / test.interval
if len(called) != expectedCalls {
t.Errorf("callback was not called the correct number of times. Expected %v, got %v", expectedCalls, len(called))
}
})
}
}