forked from argoproj/argo-workflows
-
Notifications
You must be signed in to change notification settings - Fork 0
/
throttler_test.go
152 lines (131 loc) · 4.2 KB
/
throttler_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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package sync
import (
"context"
"testing"
"time"
"github.com/stretchr/testify/assert"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/tools/cache"
wfv1 "github.com/argoproj/argo-workflows/v3/pkg/apis/workflow/v1alpha1"
fakewfclientset "github.com/argoproj/argo-workflows/v3/pkg/client/clientset/versioned/fake"
)
func Test_NamespaceBucket(t *testing.T) {
assert.Equal(t, "a", NamespaceBucket("a/b"))
}
func TestNoParallelismSamePriority(t *testing.T) {
throttler := NewThrottler(0, SingleBucket, nil)
throttler.Add("c", 0, time.Now().Add(2*time.Hour))
throttler.Add("b", 0, time.Now().Add(1*time.Hour))
throttler.Add("a", 0, time.Now())
assert.True(t, throttler.Admit("a"))
assert.True(t, throttler.Admit("b"))
assert.True(t, throttler.Admit("c"))
}
func TestNoParallelismMultipleBuckets(t *testing.T) {
throttler := NewThrottler(1, func(key Key) BucketKey {
namespace, _, _ := cache.SplitMetaNamespaceKey(key)
return namespace
}, func(key string) {})
throttler.Add("a/0", 0, time.Now())
throttler.Add("a/1", 0, time.Now())
throttler.Add("b/0", 0, time.Now())
throttler.Add("b/1", 0, time.Now())
assert.True(t, throttler.Admit("a/0"))
assert.False(t, throttler.Admit("a/1"))
assert.True(t, throttler.Admit("b/0"))
throttler.Remove("a/0")
assert.True(t, throttler.Admit("a/1"))
}
func TestWithParallelismLimitAndPriority(t *testing.T) {
queuedKey := ""
throttler := NewThrottler(2, SingleBucket, func(key string) { queuedKey = key })
throttler.Add("a", 1, time.Now())
throttler.Add("b", 2, time.Now())
throttler.Add("c", 3, time.Now())
throttler.Add("d", 4, time.Now())
assert.True(t, throttler.Admit("a"), "is started, even though low priority")
assert.True(t, throttler.Admit("b"), "is started, even though low priority")
assert.False(t, throttler.Admit("c"), "cannot start")
assert.False(t, throttler.Admit("d"), "cannot start")
assert.Equal(t, "b", queuedKey)
queuedKey = ""
throttler.Remove("a")
assert.True(t, throttler.Admit("b"), "stays running")
assert.True(t, throttler.Admit("d"), "top priority")
assert.False(t, throttler.Admit("c"))
assert.Equal(t, "d", queuedKey)
queuedKey = ""
throttler.Remove("b")
assert.True(t, throttler.Admit("d"), "top priority")
assert.True(t, throttler.Admit("c"), "now running too")
assert.Equal(t, "c", queuedKey)
}
func TestInitWithWorkflows(t *testing.T) {
queuedKey := ""
throttler := NewThrottler(1, SingleBucket, func(key string) { queuedKey = key })
ctx := context.Background()
wfclientset := fakewfclientset.NewSimpleClientset(
wfv1.MustUnmarshalWorkflow(`
apiVersion: argoproj.io/v1alpha1
kind: Workflow
metadata:
labels:
workflows.argoproj.io/phase: Running
name: a
namespace: default
spec:
entrypoint: whalesay
templates:
- name: whalesay
container:
image: docker/whalesay:latest
command: [cowsay]
args: ["hello world"]
status:
phase: Running
startedAt: "2020-06-19T17:37:05Z"
`),
wfv1.MustUnmarshalWorkflow(`
apiVersion: argoproj.io/v1alpha1
kind: Workflow
metadata:
labels:
workflows.argoproj.io/phase: Running
name: b
namespace: default
spec:
entrypoint: whalesay
templates:
- name: whalesay
container:
image: docker/whalesay:latest
command: [cowsay]
args: ["hello world"]
status:
phase: Running
startedAt: "2020-06-19T17:37:05Z"
`))
wfList, err := wfclientset.ArgoprojV1alpha1().Workflows("default").List(ctx, metav1.ListOptions{})
assert.NoError(t, err)
err = throttler.Init(wfList.Items)
assert.NoError(t, err)
assert.True(t, throttler.Admit("default/a"))
assert.True(t, throttler.Admit("default/b"))
throttler.Add("default/c", 0, time.Now())
throttler.Add("default/d", 0, time.Now())
assert.False(t, throttler.Admit("default/c"))
assert.False(t, throttler.Admit("default/d"))
throttler.Remove("default/a")
assert.Equal(t, "", queuedKey)
assert.False(t, throttler.Admit("default/c"))
assert.False(t, throttler.Admit("default/d"))
queuedKey = ""
throttler.Remove("default/b")
assert.Equal(t, "default/c", queuedKey)
assert.True(t, throttler.Admit("default/c"))
assert.False(t, throttler.Admit("default/d"))
queuedKey = ""
throttler.Remove("default/c")
assert.Equal(t, "default/d", queuedKey)
assert.True(t, throttler.Admit("default/d"))
}