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 {