1/*
2Copyright 2016 The Kubernetes Authors.
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8    http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16
17package workqueue
18
19import (
20	"testing"
21	"time"
22)
23
24func TestItemExponentialFailureRateLimiter(t *testing.T) {
25	limiter := NewItemExponentialFailureRateLimiter(1*time.Millisecond, 1*time.Second)
26
27	if e, a := 1*time.Millisecond, limiter.When("one"); e != a {
28		t.Errorf("expected %v, got %v", e, a)
29	}
30	if e, a := 2*time.Millisecond, limiter.When("one"); e != a {
31		t.Errorf("expected %v, got %v", e, a)
32	}
33	if e, a := 4*time.Millisecond, limiter.When("one"); e != a {
34		t.Errorf("expected %v, got %v", e, a)
35	}
36	if e, a := 8*time.Millisecond, limiter.When("one"); e != a {
37		t.Errorf("expected %v, got %v", e, a)
38	}
39	if e, a := 16*time.Millisecond, limiter.When("one"); e != a {
40		t.Errorf("expected %v, got %v", e, a)
41	}
42	if e, a := 5, limiter.NumRequeues("one"); e != a {
43		t.Errorf("expected %v, got %v", e, a)
44	}
45
46	if e, a := 1*time.Millisecond, limiter.When("two"); e != a {
47		t.Errorf("expected %v, got %v", e, a)
48	}
49	if e, a := 2*time.Millisecond, limiter.When("two"); e != a {
50		t.Errorf("expected %v, got %v", e, a)
51	}
52	if e, a := 2, limiter.NumRequeues("two"); e != a {
53		t.Errorf("expected %v, got %v", e, a)
54	}
55
56	limiter.Forget("one")
57	if e, a := 0, limiter.NumRequeues("one"); e != a {
58		t.Errorf("expected %v, got %v", e, a)
59	}
60	if e, a := 1*time.Millisecond, limiter.When("one"); e != a {
61		t.Errorf("expected %v, got %v", e, a)
62	}
63
64}
65
66func TestItemExponentialFailureRateLimiterOverFlow(t *testing.T) {
67	limiter := NewItemExponentialFailureRateLimiter(1*time.Millisecond, 1000*time.Second)
68	for i := 0; i < 5; i++ {
69		limiter.When("one")
70	}
71	if e, a := 32*time.Millisecond, limiter.When("one"); e != a {
72		t.Errorf("expected %v, got %v", e, a)
73	}
74
75	for i := 0; i < 1000; i++ {
76		limiter.When("overflow1")
77	}
78	if e, a := 1000*time.Second, limiter.When("overflow1"); e != a {
79		t.Errorf("expected %v, got %v", e, a)
80	}
81
82	limiter = NewItemExponentialFailureRateLimiter(1*time.Minute, 1000*time.Hour)
83	for i := 0; i < 2; i++ {
84		limiter.When("two")
85	}
86	if e, a := 4*time.Minute, limiter.When("two"); e != a {
87		t.Errorf("expected %v, got %v", e, a)
88	}
89
90	for i := 0; i < 1000; i++ {
91		limiter.When("overflow2")
92	}
93	if e, a := 1000*time.Hour, limiter.When("overflow2"); e != a {
94		t.Errorf("expected %v, got %v", e, a)
95	}
96
97}
98
99func TestItemFastSlowRateLimiter(t *testing.T) {
100	limiter := NewItemFastSlowRateLimiter(5*time.Millisecond, 10*time.Second, 3)
101
102	if e, a := 5*time.Millisecond, limiter.When("one"); e != a {
103		t.Errorf("expected %v, got %v", e, a)
104	}
105	if e, a := 5*time.Millisecond, limiter.When("one"); e != a {
106		t.Errorf("expected %v, got %v", e, a)
107	}
108	if e, a := 5*time.Millisecond, limiter.When("one"); e != a {
109		t.Errorf("expected %v, got %v", e, a)
110	}
111	if e, a := 10*time.Second, limiter.When("one"); e != a {
112		t.Errorf("expected %v, got %v", e, a)
113	}
114	if e, a := 10*time.Second, limiter.When("one"); e != a {
115		t.Errorf("expected %v, got %v", e, a)
116	}
117	if e, a := 5, limiter.NumRequeues("one"); e != a {
118		t.Errorf("expected %v, got %v", e, a)
119	}
120
121	if e, a := 5*time.Millisecond, limiter.When("two"); e != a {
122		t.Errorf("expected %v, got %v", e, a)
123	}
124	if e, a := 5*time.Millisecond, limiter.When("two"); e != a {
125		t.Errorf("expected %v, got %v", e, a)
126	}
127	if e, a := 2, limiter.NumRequeues("two"); e != a {
128		t.Errorf("expected %v, got %v", e, a)
129	}
130
131	limiter.Forget("one")
132	if e, a := 0, limiter.NumRequeues("one"); e != a {
133		t.Errorf("expected %v, got %v", e, a)
134	}
135	if e, a := 5*time.Millisecond, limiter.When("one"); e != a {
136		t.Errorf("expected %v, got %v", e, a)
137	}
138
139}
140
141func TestMaxOfRateLimiter(t *testing.T) {
142	limiter := NewMaxOfRateLimiter(
143		NewItemFastSlowRateLimiter(5*time.Millisecond, 3*time.Second, 3),
144		NewItemExponentialFailureRateLimiter(1*time.Millisecond, 1*time.Second),
145	)
146
147	if e, a := 5*time.Millisecond, limiter.When("one"); e != a {
148		t.Errorf("expected %v, got %v", e, a)
149	}
150	if e, a := 5*time.Millisecond, limiter.When("one"); e != a {
151		t.Errorf("expected %v, got %v", e, a)
152	}
153	if e, a := 5*time.Millisecond, limiter.When("one"); e != a {
154		t.Errorf("expected %v, got %v", e, a)
155	}
156	if e, a := 3*time.Second, limiter.When("one"); e != a {
157		t.Errorf("expected %v, got %v", e, a)
158	}
159	if e, a := 3*time.Second, limiter.When("one"); e != a {
160		t.Errorf("expected %v, got %v", e, a)
161	}
162	if e, a := 5, limiter.NumRequeues("one"); e != a {
163		t.Errorf("expected %v, got %v", e, a)
164	}
165
166	if e, a := 5*time.Millisecond, limiter.When("two"); e != a {
167		t.Errorf("expected %v, got %v", e, a)
168	}
169	if e, a := 5*time.Millisecond, limiter.When("two"); e != a {
170		t.Errorf("expected %v, got %v", e, a)
171	}
172	if e, a := 2, limiter.NumRequeues("two"); e != a {
173		t.Errorf("expected %v, got %v", e, a)
174	}
175
176	limiter.Forget("one")
177	if e, a := 0, limiter.NumRequeues("one"); e != a {
178		t.Errorf("expected %v, got %v", e, a)
179	}
180	if e, a := 5*time.Millisecond, limiter.When("one"); e != a {
181		t.Errorf("expected %v, got %v", e, a)
182	}
183
184}
185