|
| 1 | +package mpsc |
| 2 | + |
| 3 | +import "core:testing" |
| 4 | +import "core:thread" |
| 5 | + |
| 6 | +// ---------------------------------------------------------------------------- |
| 7 | +// Edge cases and stress tests |
| 8 | +// ---------------------------------------------------------------------------- |
| 9 | + |
| 10 | +// test_stub_recycling_explicit exercises the stub-recycling path in pop. |
| 11 | +// That path runs when exactly one item remains (head == tail, next == nil). |
| 12 | +// Each push/pop cycle of a single item triggers it. |
| 13 | +@(test) |
| 14 | +test_stub_recycling_explicit :: proc(t: ^testing.T) { |
| 15 | + q: Queue(_Test_Msg) |
| 16 | + init(&q) |
| 17 | + for i in 0 ..< 5 { |
| 18 | + msg := _Test_Msg{data = i} |
| 19 | + push(&q, &msg) |
| 20 | + got := pop(&q) |
| 21 | + testing.expectf(t, got != nil && got.data == i, "round %d: pop should return the pushed message", i) |
| 22 | + testing.expectf(t, length(&q) == 0, "round %d: length should be 0 after pop", i) |
| 23 | + } |
| 24 | +} |
| 25 | + |
| 26 | +// test_pop_all_drains_to_zero pushes N messages then pops until the queue is empty. |
| 27 | +// Verifies all messages are received and length reaches zero. |
| 28 | +@(test) |
| 29 | +test_pop_all_drains_to_zero :: proc(t: ^testing.T) { |
| 30 | + q: Queue(_Test_Msg) |
| 31 | + init(&q) |
| 32 | + |
| 33 | + N :: 50 |
| 34 | + msgs: [N]_Test_Msg |
| 35 | + for i in 0 ..< N { |
| 36 | + msgs[i].data = i |
| 37 | + push(&q, &msgs[i]) |
| 38 | + } |
| 39 | + |
| 40 | + count := 0 |
| 41 | + for length(&q) > 0 || count < N { |
| 42 | + if pop(&q) != nil { |
| 43 | + count += 1 |
| 44 | + } |
| 45 | + if length(&q) == 0 && count == N { |
| 46 | + break |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + testing.expect(t, count == N, "should drain all pushed messages") |
| 51 | + testing.expect(t, length(&q) == 0, "length should be 0 after full drain") |
| 52 | +} |
| 53 | + |
| 54 | +// _Stress_Ctx passes queue and message slice to each producer thread. |
| 55 | +@(private) |
| 56 | +_Stress_Ctx :: struct { |
| 57 | + q: ^Queue(_Test_Msg), |
| 58 | + msgs: []_Test_Msg, |
| 59 | +} |
| 60 | + |
| 61 | +_STRESS_PRODUCERS :: 10 |
| 62 | +_STRESS_ITEMS_PER_PROD :: 1000 |
| 63 | + |
| 64 | +// test_concurrent_push_stress runs _STRESS_PRODUCERS threads each pushing |
| 65 | +// _STRESS_ITEMS_PER_PROD messages. The main thread consumes all of them. |
| 66 | +// Verifies no messages are lost and length reaches zero. |
| 67 | +@(test) |
| 68 | +test_concurrent_push_stress :: proc(t: ^testing.T) { |
| 69 | + q: Queue(_Test_Msg) |
| 70 | + init(&q) |
| 71 | + |
| 72 | + total :: _STRESS_PRODUCERS * _STRESS_ITEMS_PER_PROD |
| 73 | + |
| 74 | + msgs := make([]_Test_Msg, total) |
| 75 | + defer delete(msgs) |
| 76 | + |
| 77 | + ctxs := make([]_Stress_Ctx, _STRESS_PRODUCERS) |
| 78 | + defer delete(ctxs) |
| 79 | + |
| 80 | + for i in 0 ..< _STRESS_PRODUCERS { |
| 81 | + ctxs[i] = _Stress_Ctx { |
| 82 | + q = &q, |
| 83 | + msgs = msgs[i * _STRESS_ITEMS_PER_PROD:(i + 1) * _STRESS_ITEMS_PER_PROD], |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + threads := make([dynamic]^thread.Thread, 0, _STRESS_PRODUCERS) |
| 88 | + defer delete(threads) |
| 89 | + |
| 90 | + for i in 0 ..< _STRESS_PRODUCERS { |
| 91 | + th := thread.create_and_start_with_poly_data( |
| 92 | + &ctxs[i], |
| 93 | + proc(ctx: ^_Stress_Ctx) { |
| 94 | + for j in 0 ..< len(ctx.msgs) { |
| 95 | + push(ctx.q, &ctx.msgs[j]) |
| 96 | + } |
| 97 | + }, |
| 98 | + ) |
| 99 | + append(&threads, th) |
| 100 | + } |
| 101 | + |
| 102 | + // Consume until all messages are received. |
| 103 | + received := 0 |
| 104 | + for received < total { |
| 105 | + if pop(&q) != nil { |
| 106 | + received += 1 |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + for th in threads { |
| 111 | + thread.join(th) |
| 112 | + thread.destroy(th) |
| 113 | + } |
| 114 | + |
| 115 | + testing.expect(t, received == total, "should receive all pushed messages") |
| 116 | + testing.expect(t, length(&q) == 0, "length should be 0 after full drain") |
| 117 | +} |
| 118 | + |
| 119 | +// test_length_consistency verifies that after a concurrent stress run |
| 120 | +// the length counter reaches zero and matches the drained count. |
| 121 | +@(test) |
| 122 | +test_length_consistency :: proc(t: ^testing.T) { |
| 123 | + q: Queue(_Test_Msg) |
| 124 | + init(&q) |
| 125 | + |
| 126 | + N :: 200 |
| 127 | + msgs: [N]_Test_Msg |
| 128 | + for i in 0 ..< N { |
| 129 | + push(&q, &msgs[i]) |
| 130 | + } |
| 131 | + |
| 132 | + testing.expect(t, length(&q) == N, "length should equal number of pushes") |
| 133 | + |
| 134 | + count := 0 |
| 135 | + for pop(&q) != nil { |
| 136 | + count += 1 |
| 137 | + } |
| 138 | + |
| 139 | + testing.expect(t, count == N, "should pop exactly N messages") |
| 140 | + testing.expect(t, length(&q) == 0, "length should be 0 after draining") |
| 141 | +} |
0 commit comments