Skip to content

Commit 05e4fe7

Browse files
committed
Update unit tests
1 parent 68dbdba commit 05e4fe7

1 file changed

Lines changed: 25 additions & 1 deletion

File tree

queue_test.go

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ func (o *object) OnTimer(t time.Time) {
1919
func populateQueue(t *testing.T, now time.Time) *Queue {
2020
q := New()
2121

22-
count := 200
22+
count := 300
2323
objects := make([]*object, count)
2424

2525
// Add a bunch of objects to the queue in random order.
@@ -29,21 +29,45 @@ func populateQueue(t *testing.T, now time.Time) *Queue {
2929
q.Schedule(objects[j], tm)
3030
}
3131

32+
// Reschedule all the objects in a different random order.
33+
for i, j := range rand.Perm(count) {
34+
tm := now.Add(time.Duration(i+1) * time.Hour)
35+
q.Schedule(objects[j], tm)
36+
}
37+
3238
if q.Len() != count {
3339
t.Error("invalid queue length:", q.Len())
3440
}
3541

3642
return q
3743
}
3844

45+
func TestEmptyQueue(t *testing.T) {
46+
queue := New()
47+
48+
o, _ := queue.PeekFirst()
49+
if o != nil {
50+
t.Error("Expected nil peek")
51+
}
52+
53+
o, _ = queue.PopFirst()
54+
if o != nil {
55+
t.Error("Expected nil pop")
56+
}
57+
}
58+
3959
func TestQueue(t *testing.T) {
4060
for iter := 0; iter < 100; iter++ {
4161
now := time.Date(2015, 1, 1, 0, 0, 0, 0, time.UTC)
4262
queue := populateQueue(t, now)
4363

4464
// Make sure objects are removed from the queue in order.
4565
for prev := now; queue.Len() > 0; {
66+
_, ptm := queue.PeekFirst()
4667
_, tm := queue.PopFirst()
68+
if tm != ptm {
69+
t.Errorf("Peek/Pop mismatch.\n")
70+
}
4771
if tm.Sub(prev) != time.Hour {
4872
t.Errorf("Invalid queue ordering.\n"+
4973
" Got: %v\n"+

0 commit comments

Comments
 (0)