diff --git a/replay.go b/replay.go index f94aab5..6f8e7d0 100644 --- a/replay.go +++ b/replay.go @@ -73,6 +73,18 @@ func FunctionSource(f func() Event) Source { return functionSource(f) } +// CollectionSource emits events from an input slice +func CollectionSource(evs []Event) Source { + return FunctionSource(func() Event { + if len(evs) == 0 { + return nil + } + rv := evs[0] + evs = evs[1:] + return rv + }) +} + // New creates a new Replay with time scaled to the given amount. // // scale should be > 0 diff --git a/replay_test.go b/replay_test.go index b774692..2abc895 100644 --- a/replay_test.go +++ b/replay_test.go @@ -39,43 +39,29 @@ func (f *fakeTime) sleep(d time.Duration) { f.passed += d } -type noopAction struct{} - -func (noopAction) Process(Event) {} - type dumbEvent time.Time func (d dumbEvent) TS() time.Time { return time.Time(d) } -type directSource struct { - events []Event -} - -func (d *directSource) Next() Event { - if len(d.events) == 0 { - return nil +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)), } - rv := d.events[0] - d.events = d.events[1:] - return rv } +var noopAction = FunctionAction(func(Event) {}) + func TestRun(t *testing.T) { r := New(1) tm := &fakeTime{} r.now = tm.now r.sleep = tm.sleep - base := time.Now() - s := &directSource{ - events: []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)), - }} - - off := r.Run(s, noopAction{}) + off := r.Run(CollectionSource(genEvents()), noopAction) if off != 0 { t.Errorf("Expected to be off by 0, was off by %v", off) } @@ -83,3 +69,41 @@ func TestRun(t *testing.T) { t.Errorf("Expected to take 8 seconds, took %v", tm.passed) } } + +func TestRun2x(t *testing.T) { + r := New(2) + tm := &fakeTime{} + r.now = tm.now + r.sleep = tm.sleep + + 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 := New(0.5) + tm := &fakeTime{} + r.now = tm.now + r.sleep = tm.sleep + + 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 := 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) + } +}