-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathreplay_test.go
107 lines (90 loc) · 2.22 KB
/
replay_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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package replay
import (
"testing"
"time"
)
func TestNew(t *testing.T) {
New(1)
panicked := false
func() {
defer func() { panicked = recover() != nil }()
r := New(0)
t.Errorf("Failed to panic at 0, got %#v", r)
}()
if !panicked {
t.Errorf("Failed to panick at zero")
}
func() {
defer func() { panicked = recover() != nil }()
r := New(-1)
t.Errorf("Failed to panic at -1, got %#v", r)
}()
if !panicked {
t.Errorf("Failed to panick at -1")
}
}
type fakeTime struct {
base time.Time
passed time.Duration
}
func (f *fakeTime) now() time.Time {
return f.base.Add(f.passed)
}
func (f *fakeTime) sleep(d time.Duration) {
f.passed += d
}
func useFakeTime(r *Replay) (*Replay, *fakeTime) {
tm := &fakeTime{}
r.now = tm.now
r.sleep = tm.sleep
return r, tm
}
type dumbEvent time.Time
func (d dumbEvent) TS() time.Time { return time.Time(d) }
func genEvents() []Event {
base := time.Now()
return []Event{
dumbEvent(base.Add(5 * time.Second)),
dumbEvent(base.Add(6 * time.Second)),
dumbEvent(base.Add(9 * time.Second)),
dumbEvent(base.Add(13 * time.Second)),
}
}
var noopAction = FunctionAction(func(Event) {})
func TestRun(t *testing.T) {
r, tm := useFakeTime(New(1))
off := r.Run(CollectionSource(genEvents()), noopAction)
if off != 0 {
t.Errorf("Expected to be off by 0, was off by %v", off)
}
if tm.passed != (8 * time.Second) {
t.Errorf("Expected to take 8 seconds, took %v", tm.passed)
}
}
func TestRun2x(t *testing.T) {
r, tm := useFakeTime(New(2))
off := r.Run(CollectionSource(genEvents()), noopAction)
if off != 0 {
t.Errorf("Expected to be off by 0, was off by %v", off)
}
if tm.passed != (4 * time.Second) {
t.Errorf("Expected to take 8 seconds, took %v", tm.passed)
}
}
func TestRunHalfx(t *testing.T) {
r, tm := useFakeTime(New(0.5))
off := r.Run(CollectionSource(genEvents()), noopAction)
if off != 0 {
t.Errorf("Expected to be off by 0, was off by %v", off)
}
if tm.passed != (16 * time.Second) {
t.Errorf("Expected to take 8 seconds, took %v", tm.passed)
}
}
func TestRunNil(t *testing.T) {
r, _ := useFakeTime(New(1))
off := r.Run(CollectionSource(nil), FunctionAction(func(Event) {}))
if off != 0 {
t.Errorf("Expected nil input to run with 0 off, got %v", off)
}
}