Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gitlab-ce
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Léo-Paul Géneau
gitlab-ce
Commits
8b939bfa
Commit
8b939bfa
authored
Dec 09, 2017
by
Stan Hu
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add spec for ActionRateLimiter
parent
ef78f67f
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
29 additions
and
2 deletions
+29
-2
lib/gitlab/action_rate_limiter.rb
lib/gitlab/action_rate_limiter.rb
+2
-2
spec/lib/gitlab/action_rate_limiter_spec.rb
spec/lib/gitlab/action_rate_limiter_spec.rb
+27
-0
No files found.
lib/gitlab/action_rate_limiter.rb
View file @
8b939bfa
...
...
@@ -16,12 +16,12 @@ module Gitlab
value
=
0
Gitlab
::
Redis
::
Cache
.
with
do
|
redis
|
cache_key
=
"action_rate_limiter:
#{
action
}
:
#{
key
}
"
cache_key
=
"action_rate_limiter:
#{
action
.
to_s
}
:
#{
key
}
"
value
=
redis
.
incr
(
cache_key
)
redis
.
expire
(
cache_key
,
expiry_time
)
if
value
==
1
end
value
.
to_i
value
end
def
throttled?
(
key
,
threshold_value
)
...
...
spec/lib/gitlab/action_rate_limiter_spec.rb
0 → 100644
View file @
8b939bfa
require
'spec_helper'
describe
Gitlab
::
ActionRateLimiter
do
let
(
:redis
)
{
double
(
'redis'
)
}
let
(
:key
)
{
'user:1'
}
let
(
:cache_key
)
{
"action_rate_limiter:test_action:
#{
key
}
"
}
subject
{
described_class
.
new
(
action: :test_action
,
expiry_time:
100
)
}
before
do
allow
(
Gitlab
::
Redis
::
Cache
).
to
receive
(
:with
).
and_yield
(
redis
)
end
it
'increases the throttle count and sets the expire time'
do
expect
(
redis
).
to
receive
(
:incr
).
with
(
cache_key
).
and_return
(
1
)
expect
(
redis
).
to
receive
(
:expire
).
with
(
cache_key
,
100
)
expect
(
subject
.
throttled?
(
key
,
1
)).
to
be
false
end
it
'returns true if the key is throttled'
do
expect
(
redis
).
to
receive
(
:incr
).
with
(
cache_key
).
and_return
(
2
)
expect
(
redis
).
not_to
receive
(
:expire
)
expect
(
subject
.
throttled?
(
key
,
1
)).
to
be
true
end
end
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