debian-mirror-gitlab/workhorse/internal/queueing/queue_test.go

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

65 lines
1.3 KiB
Go
Raw Permalink Normal View History

2021-02-22 17:27:13 +05:30
package queueing
import (
"testing"
"time"
2022-11-25 23:54:43 +05:30
"github.com/prometheus/client_golang/prometheus"
2021-02-22 17:27:13 +05:30
)
func TestNormalQueueing(t *testing.T) {
2022-11-25 23:54:43 +05:30
q := newQueue("queue name", 2, 1, time.Microsecond, prometheus.NewRegistry())
2021-02-22 17:27:13 +05:30
err1 := q.Acquire()
if err1 != nil {
t.Fatal("we should acquire a new slot")
}
err2 := q.Acquire()
if err2 != nil {
t.Fatal("we should acquire a new slot")
}
err3 := q.Acquire()
if err3 != ErrQueueingTimedout {
t.Fatal("we should timeout")
}
q.Release()
err4 := q.Acquire()
if err4 != nil {
t.Fatal("we should acquire a new slot")
}
}
func TestQueueLimit(t *testing.T) {
2022-11-25 23:54:43 +05:30
q := newQueue("queue name", 1, 0, time.Microsecond, prometheus.NewRegistry())
2021-02-22 17:27:13 +05:30
err1 := q.Acquire()
if err1 != nil {
t.Fatal("we should acquire a new slot")
}
err2 := q.Acquire()
if err2 != ErrTooManyRequests {
t.Fatal("we should fail because of not enough slots in queue")
}
}
func TestQueueProcessing(t *testing.T) {
2022-11-25 23:54:43 +05:30
q := newQueue("queue name", 1, 1, time.Second, prometheus.NewRegistry())
2021-02-22 17:27:13 +05:30
err1 := q.Acquire()
if err1 != nil {
t.Fatal("we should acquire a new slot")
}
go func() {
time.Sleep(50 * time.Microsecond)
q.Release()
}()
err2 := q.Acquire()
if err2 != nil {
t.Fatal("we should acquire slot after the previous one finished")
}
}