forked from zalando/skipper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fifo_test.go
86 lines (77 loc) · 1.73 KB
/
fifo_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package scheduler
import (
"context"
"testing"
"time"
"github.com/zalando/skipper/metrics"
)
func TestFifo(t *testing.T) {
waitForStatus := func(t *testing.T, fq *FifoQueue, s QueueStatus) {
t.Helper()
if fq == nil {
t.Fatal("fq nil")
}
timeout := time.After(120 * time.Millisecond)
for {
time.Sleep(time.Millisecond)
cur := fq.Status()
if cur == s {
return
}
select {
case <-timeout:
t.Fatalf("failed to reach status, want %v, got: %v", s, cur)
default:
}
}
}
t.Run("queue full", func(t *testing.T) {
reg := RegistryWith(Options{
MetricsUpdateTimeout: 100 * time.Millisecond,
EnableRouteFIFOMetrics: true,
Metrics: metrics.Default,
})
cfg := Config{
MaxConcurrency: 1,
MaxQueueSize: 2,
Timeout: 500 * time.Millisecond,
CloseTimeout: 1000 * time.Millisecond,
}
fq := reg.newFifoQueue("", cfg)
ctx := context.Background()
f, err := fq.Wait(ctx)
if err != nil {
t.Fatalf("Failed to call wait: %v", err)
}
f()
go fq.Wait(ctx)
go fq.Wait(ctx)
go fq.Wait(ctx)
waitForStatus(t, fq, QueueStatus{ActiveRequests: 1, QueuedRequests: 2})
f, err = fq.Wait(ctx)
if err != ErrQueueFull {
t.Fatalf("Failed to get ErrQueueFull: %v", err)
}
if err == nil {
f()
}
waitForStatus(t, fq, QueueStatus{ActiveRequests: 1, QueuedRequests: 2})
})
t.Run("semaphore actions and close", func(t *testing.T) {
reg := NewRegistry()
cfg := Config{
MaxConcurrency: 1,
MaxQueueSize: 2,
Timeout: 200 * time.Millisecond,
CloseTimeout: 100 * time.Millisecond,
}
fq := reg.newFifoQueue("foo", cfg)
ctx := context.Background()
f, err := fq.Wait(ctx)
if err == nil {
f()
}
fq.close()
fq.close()
})
}