From 899c206c0326675096668791b7d692b27fb14d1d Mon Sep 17 00:00:00 2001 From: fantasticbin Date: Tue, 10 Dec 2024 18:14:04 +0800 Subject: [PATCH] =?UTF-8?q?=E5=BB=B6=E6=97=B6=E9=98=9F=E5=88=97=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0=E6=95=B0=E9=87=8F=E8=8E=B7=E5=8F=96=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lock_free/queues.go | 8 ++++++++ lock_free/queues_test.go | 16 ++++------------ 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/lock_free/queues.go b/lock_free/queues.go index d238b1b..62f41cc 100644 --- a/lock_free/queues.go +++ b/lock_free/queues.go @@ -115,6 +115,14 @@ func (q *Queues[TKey, TValue, TRoute]) Len(route TRoute) int { return 0 } +// DelayCount 获取延时队列数量 +func (q *Queues[TKey, TValue, TRoute]) DelayCount(route TRoute) uint64 { + if queue, ok := q.queues.Load(route); ok { + return queue.(*DelayLkQueue[TKey, TValue]).DelayCount() + } + return 0 +} + // DelayEnqueue 延迟入队 func (q *Queues[TKey, TValue, TRoute]) DelayEnqueue(route TRoute, value TValue, duration time.Duration) { // 入队时检查上一次出队是否确认成功 diff --git a/lock_free/queues_test.go b/lock_free/queues_test.go index a56300c..36c7795 100644 --- a/lock_free/queues_test.go +++ b/lock_free/queues_test.go @@ -16,22 +16,14 @@ func TestQueues(t *testing.T) { route := "test" q := NewQueues[struct{}, int, string]() - for _, c := range cases { - q.Enqueue(route, c.value) - } - - if q.Len(route) != len(cases) { - t.Errorf("queue length error, want %d, got %d", len(cases), q.Len(route)) - } - - for range cases { - q.Dequeue(route) - } - for _, c := range cases { q.DelayEnqueue(route, c.value, c.duration) } + if q.DelayCount(route) != uint64(len(cases)) { + t.Errorf("queue length error, want %d, got %d", len(cases), q.DelayCount(route)) + } + notify := make(chan int) go func() { for data := range notify {