|
| 1 | +/* |
| 2 | + * SPDX-License-Identifier: AGPL-3.0-only |
| 3 | + * Copyright (c) 2022-2025, daeuniverse Organization <dae@daeuniverse.org> |
| 4 | + */ |
| 5 | + |
| 6 | +package control |
| 7 | + |
| 8 | +import ( |
| 9 | + "net/netip" |
| 10 | + "testing" |
| 11 | + "time" |
| 12 | +) |
| 13 | + |
| 14 | +// Regression guard: |
| 15 | +// When queue mapping is removed before convoy self-delete, the old convoy |
| 16 | +// must exit instead of looping forever outside the queue map. |
| 17 | +func TestConvoyExitsWhenQueueMappingDeletedBeforeSelfDelete(t *testing.T) { |
| 18 | + oldAging := UdpTaskPoolAgingTime |
| 19 | + UdpTaskPoolAgingTime = 20 * time.Millisecond |
| 20 | + defer func() { UdpTaskPoolAgingTime = oldAging }() |
| 21 | + |
| 22 | + pool := NewUdpTaskPool() |
| 23 | + key := netip.MustParseAddrPort("198.51.100.10:443") |
| 24 | + q := newTestQueue(pool, key) |
| 25 | + q.agingTime = UdpTaskPoolAgingTime |
| 26 | + pool.queues.Store(key, q) |
| 27 | + |
| 28 | + done := make(chan struct{}) |
| 29 | + go func() { |
| 30 | + q.convoy() |
| 31 | + close(done) |
| 32 | + }() |
| 33 | + |
| 34 | + // Wait until convoy reaches draining state. |
| 35 | + deadline := time.Now().Add(2 * time.Second) |
| 36 | + for !q.draining.Load() && time.Now().Before(deadline) { |
| 37 | + time.Sleep(1 * time.Millisecond) |
| 38 | + } |
| 39 | + if !q.draining.Load() { |
| 40 | + t.Fatal("convoy did not enter draining state in time") |
| 41 | + } |
| 42 | + |
| 43 | + // Simulate concurrent path deleting map entry first (acquireQueue draining path). |
| 44 | + if !pool.tryDeleteQueue(key, q) { |
| 45 | + t.Fatal("expected initial delete to succeed") |
| 46 | + } |
| 47 | + if got := pool.Count(); got != 0 { |
| 48 | + t.Fatalf("expected queue map to be empty, got count=%d", got) |
| 49 | + } |
| 50 | + |
| 51 | + // Old convoy should exit within one cleanup cycle. |
| 52 | + select { |
| 53 | + case <-done: |
| 54 | + // Expected: clean exit after failed self-delete with stale/absent mapping. |
| 55 | + case <-time.After(UdpTaskPoolAgingTime + 120*time.Millisecond): |
| 56 | + // Cleanup to avoid leaking goroutine into other tests, then fail. |
| 57 | + pool.queues.Store(key, q) |
| 58 | + select { |
| 59 | + case <-done: |
| 60 | + case <-time.After(1 * time.Second): |
| 61 | + t.Fatal("convoy did not exit during cleanup after timeout") |
| 62 | + } |
| 63 | + t.Fatal("convoy did not exit after mapping was deleted before self-delete") |
| 64 | + } |
| 65 | +} |
0 commit comments