go-study/lock_free/queues_test.go

43 lines
785 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 {
2024-12-10 18:14:04 +08:00
q.DelayEnqueue(route, c.value, c.duration)
}
2024-12-10 18:14:04 +08:00
if q.DelayCount(route) != uint64(len(cases)) {
t.Errorf("queue length error, want %d, got %d", len(cases), q.DelayCount(route))
}
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)
2024-12-10 18:32:38 +08:00
if q.DelayCount(route) != 0 {
t.Errorf("queue length error, want %d, got %d", 0, q.DelayCount(route))
}
}