Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
K
k3s
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Jacklull
k3s
Commits
94ca8cba
Unverified
Commit
94ca8cba
authored
Dec 07, 2016
by
Jordan Liggitt
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add a multi-threaded test for ratelimiter
parent
aa08702d
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
49 additions
and
0 deletions
+49
-0
throttle_test.go
pkg/util/flowcontrol/throttle_test.go
+49
-0
No files found.
pkg/util/flowcontrol/throttle_test.go
View file @
94ca8cba
...
@@ -23,6 +23,55 @@ import (
...
@@ -23,6 +23,55 @@ import (
"time"
"time"
)
)
func
TestMultithreadedThrottling
(
t
*
testing
.
T
)
{
// Bucket with 100QPS and no burst
r
:=
NewTokenBucketRateLimiter
(
100
,
1
)
// channel to collect 100 tokens
taken
:=
make
(
chan
bool
,
100
)
// Set up goroutines to hammer the throttler
startCh
:=
make
(
chan
bool
)
endCh
:=
make
(
chan
bool
)
for
i
:=
0
;
i
<
10
;
i
++
{
go
func
()
{
// wait for the starting signal
<-
startCh
for
{
// get a token
r
.
Accept
()
select
{
// try to add it to the taken channel
case
taken
<-
true
:
continue
// if taken is full, notify and return
default
:
endCh
<-
true
return
}
}
}()
}
// record wall time
startTime
:=
time
.
Now
()
// take the initial capacity so all tokens are the result of refill
r
.
Accept
()
// start the thundering herd
close
(
startCh
)
// wait for the first signal that we collected 100 tokens
<-
endCh
// record wall time
endTime
:=
time
.
Now
()
if
duration
:=
endTime
.
Sub
(
startTime
);
duration
<
time
.
Second
{
// We shouldn't be able to get 100 tokens out of the bucket in less than 1 second of wall clock time, no matter what
t
.
Errorf
(
"Expected it to take at least 1 second to get 100 tokens, took %v"
,
duration
)
}
else
{
t
.
Logf
(
"Took %v to get 100 tokens"
,
duration
)
}
}
func
TestBasicThrottle
(
t
*
testing
.
T
)
{
func
TestBasicThrottle
(
t
*
testing
.
T
)
{
r
:=
NewTokenBucketRateLimiter
(
1
,
3
)
r
:=
NewTokenBucketRateLimiter
(
1
,
3
)
for
i
:=
0
;
i
<
3
;
i
++
{
for
i
:=
0
;
i
<
3
;
i
++
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment