go-study/lock_free/queues_test.go

47 lines
755 B
Go
Raw Normal View History

package lock_free
import (
"testing"
"time"
)
2024-12-09 10:50:31 +08:00
func TestQueues(t *testing.T) {
cases := []struct {
value int
duration time.Duration
}{
{1, time.Second},
{3, time.Second * 3},
}
2024-12-09 10:50:31 +08:00
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 {
2024-12-09 10:50:31 +08:00
q.DelayEnqueue(route, c.value, c.duration)
}
2024-12-03 10:49:09 +08:00
notify := make(chan int)
go func() {
2024-12-03 10:49:09 +08:00
for data := range notify {
t.Log(data)
t.Log(time.Now().Unix())
}
}()
2024-12-09 10:50:31 +08:00
go q.ContinuousDequeueNotify(route, notify)
time.Sleep(time.Second * 5)
2024-12-03 10:49:09 +08:00
close(notify)
}