Skip to content

Commit 1a53941

Browse files
test
Signed-off-by: Abhishek Choudhary <[email protected]>
1 parent c37f07c commit 1a53941

File tree

3 files changed

+67
-20
lines changed

3 files changed

+67
-20
lines changed

.github/workflows/test.yaml

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
name: test
2+
3+
on:
4+
push:
5+
branches: [ master ]
6+
pull_request:
7+
branches: [ master ]
8+
9+
jobs:
10+
test:
11+
runs-on: "ubuntu-20.04"
12+
env:
13+
OPENRESTY_PREFIX: "/usr/local/openresty"
14+
15+
steps:
16+
- name: Check out code
17+
uses: actions/[email protected]
18+
with:
19+
submodules: recursive
20+
21+
- name: Linux Get dependencies
22+
run: |
23+
sudo apt install -y cpanminus build-essential libncurses5-dev libreadline-dev libssl-dev perl lua5.1 liblua5.1-0-dev
24+
25+
- name: Linux Before install
26+
run: |
27+
sudo cpanm --notest Test::Nginx > build.log 2>&1 || (cat build.log && exit 1)
28+
29+
- name: Linux Install
30+
run: |
31+
wget -qO - https://openresty.org/package/pubkey.gpg | sudo apt-key add -
32+
sudo apt-get -y install software-properties-common
33+
sudo add-apt-repository -y "deb http://openresty.org/package/ubuntu $(lsb_release -sc) main"
34+
sudo apt-get update
35+
sudo apt-get install openresty
36+
git clone https://github.com/openresty/test-nginx.git test-nginx
37+
38+
- name: Linux Script
39+
run: |
40+
export PATH=$OPENRESTY_PREFIX/nginx/sbin:$PATH
41+
make test

lib/resty/limit/count.lua

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -125,16 +125,16 @@ function _M.uncommit(self, key)
125125
local dict = self.dict
126126
local limit = self.limit
127127

128-
local remaining, err = dict:incr(key, 1)
129-
if not remaining then
128+
local consumed, err = dict:incr(key, -1)
129+
if not consumed then
130130
if err == "not found" then
131-
remaining = limit
131+
consumed = limit
132132
else
133133
return nil, err
134134
end
135135
end
136136

137-
return remaining
137+
return consumed
138138
end
139139

140140

t/count.t

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,15 @@ __DATA__
3434
content_by_lua_block {
3535
local limit_count = require "resty.limit.count"
3636
ngx.shared.store:flush_all()
37-
local lim = limit_count.new("store", 10, 100)
37+
local limit = 10
38+
local lim = limit_count.new("store", limit, 100)
3839
local uri = ngx.var.uri
3940
for i = 1, 12 do
4041
local delay, err = lim:incoming(uri, true)
4142
if not delay then
4243
ngx.say(err)
4344
else
44-
local remaining = err
45+
local remaining = limit - err
4546
ngx.say("remaining: ", remaining)
4647
end
4748
end
@@ -75,36 +76,37 @@ rejected
7576
content_by_lua_block {
7677
local limit_count = require "resty.limit.count"
7778
ngx.shared.store:flush_all()
78-
local lim = limit_count.new("store", 1, 10)
79+
local limit = 1
80+
local lim = limit_count.new("store", limit, 10)
7981
local delay1, err1 = lim:incoming("foo", true)
8082
local delay2, err2 = lim:incoming("foo", true)
8183
local delay3, err3 = lim:incoming("bar", true)
8284
local delay4, err4 = lim:incoming("bar", true)
8385
if not delay1 then
8486
ngx.say(err1)
8587
else
86-
local remaining1 = err1
88+
local remaining1 = limit - err1
8789
ngx.say("remaining1: ", remaining1)
8890
end
8991
9092
if not delay2 then
9193
ngx.say(err2)
9294
else
93-
local remaining2 = err2
95+
local remaining2 = limit - err2
9496
ngx.say("remaining2: ", remaining2)
9597
end
9698
9799
if not delay3 then
98100
ngx.say(err3)
99101
else
100-
local remaining3 = err3
102+
local remaining3 = limit -err3
101103
ngx.say("remaining3: ", remaining3)
102104
end
103105
104106
if not delay4 then
105107
ngx.say(err4)
106108
else
107-
local remaining4 = err4
109+
local remaining4 = limit - err4
108110
ngx.say("remaining4: ", remaining4)
109111
end
110112
}
@@ -129,23 +131,24 @@ rejected
129131
content_by_lua_block {
130132
local limit_count = require "resty.limit.count"
131133
ngx.shared.store:flush_all()
132-
local lim = limit_count.new("store", 1, 1)
134+
local limit = 1
135+
local lim = limit_count.new("store", limit, 1)
133136
134137
local uri = ngx.var.uri
135138
for i = 1, 2 do
136139
local delay, err = lim:incoming(uri, true)
137140
if not delay then
138141
ngx.say(err)
139142
else
140-
local remaining = err
143+
local remaining = limit - err
141144
ngx.say("remaining: ", remaining)
142145
end
143146
144147
local delay, err = lim:incoming(uri, true)
145148
if not delay then
146149
ngx.say(err)
147150
else
148-
local remaining = err
151+
local remaining = limit - err
149152
ngx.say("remaining: ", remaining)
150153
end
151154
ngx.sleep(1)
@@ -172,15 +175,16 @@ rejected
172175
content_by_lua_block {
173176
local limit_count = require "resty.limit.count"
174177
ngx.shared.store:flush_all()
175-
local lim = limit_count.new("store", 5, 10)
178+
local limit = 5
179+
local lim = limit_count.new("store", limit, 10)
176180
local begin = ngx.time()
177181
178182
for i = 1, 4 do
179183
local delay, err = lim:incoming("foo", i < 3)
180184
if not delay then
181185
ngx.say(err)
182186
else
183-
local remaining = err
187+
local remaining = limit - err
184188
ngx.say("remaining: ", remaining)
185189
end
186190
end
@@ -205,15 +209,16 @@ remaining: 2
205209
location = /t {
206210
content_by_lua_block {
207211
local limit_count = require "resty.limit.count"
208-
local lim = limit_count.new("store", 2, 10)
212+
local limit = 2
213+
local lim = limit_count.new("store", limit, 10)
209214
ngx.shared.store:flush_all()
210215
local key = "foo"
211216
for i = 1, 3 do
212217
local delay, err = lim:incoming(key, true)
213218
if not delay then
214219
ngx.say("failed to limit count: ", err)
215220
else
216-
local remaining = err
221+
local remaining = limit - err
217222
ngx.say("remaining: ", remaining)
218223
end
219224
local ok, err = lim:uncommit(key)
@@ -242,14 +247,15 @@ remaining: 1
242247
content_by_lua_block {
243248
local limit_count = require "resty.limit.count"
244249
ngx.shared.store:flush_all()
245-
local lim = limit_count.new("store", 10, 100)
250+
local limit = 10
251+
local lim = limit_count.new("store", limit, 100)
246252
local uri = ngx.var.uri
247253
for i = 1, 7 do
248254
local delay, err = lim:incoming(uri, true, 2)
249255
if not delay then
250256
ngx.say(err)
251257
else
252-
local remaining = err
258+
local remaining = limit - err
253259
ngx.say("remaining: ", remaining)
254260
end
255261
end

0 commit comments

Comments
 (0)