Skip to content

Commit

Permalink
Cleaned up tests, added tests for time scaling
Browse files Browse the repository at this point in the history
  • Loading branch information
dustin committed Sep 24, 2014
1 parent 55d5d2c commit 8aebd59
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 24 deletions.
12 changes: 12 additions & 0 deletions replay.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
72 changes: 48 additions & 24 deletions replay_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,47 +39,71 @@ 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)
}
if tm.passed != (8 * time.Second) {
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)
}
}

0 comments on commit 8aebd59

Please sign in to comment.