| 25 | } |
| 26 | |
| 27 | func TestFuzzTimeQueue(t *testing.T) { |
| 28 | t.Parallel() |
| 29 | |
| 30 | // generate random values and enqueue them |
| 31 | values := make([]val, 100) |
| 32 | for i := 0; i < 100; i++ { |
| 33 | values[i] = val{v: rand.Intn(1000) - 500} |
| 34 | } |
| 35 | |
| 36 | tqueue := NewTimeQueue[val]() |
| 37 | now := time.Now() |
| 38 | for _, v := range values { |
| 39 | tqueue.Enqueue(now.Add(time.Millisecond*time.Duration(v.v)), v) |
| 40 | } |
| 41 | |
| 42 | slices.SortFunc(values, func(i, j val) int { |
| 43 | if i.v > j.v { |
| 44 | return 1 |
| 45 | } |
| 46 | return -1 |
| 47 | }) |
| 48 | |
| 49 | // dequeue the values and check if they are in the correct order |
| 50 | for i := 0; i < 100; i++ { |
| 51 | v := tqueue.Dequeue(context.Background()) |
| 52 | if v.v != values[i].v { |
| 53 | t.Errorf("expected %d, got %d", values[i].v, v.v) |
| 54 | } |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | func TestTimeQueueEnqueueWhileWaiting(t *testing.T) { |
| 59 | t.Parallel() |