1
# vim:set ft= ts=4 sw=4 et fdm=marker:
2
use Test::Nginx::Socket::Lua;
4
#worker_connections(1014);
5
#master_process_enabled(1);
10
plan tests => repeat_each() * (blocks() * 3);
23
lua_shared_dict dogs 1m;
27
local ffi = require "ffi"
30
void *ngx_http_lua_find_zone(char *data, size_t len);
33
local buf = ffi.new("char[?]", 4)
34
ffi.copy(buf, "foo", 3)
35
local zone = ffi.C.ngx_http_lua_find_zone(buf, 3)
36
ngx.say("foo zone: ", tonumber(ffi.cast("long", zone)) ~= 0 and "defined" or "undef")
38
ffi.copy(buf, "dogs", 4)
39
zone = ffi.C.ngx_http_lua_find_zone(buf, 4)
40
ngx.say("dogs zone: ", tonumber(ffi.cast("long", zone)) ~= 0 and "defined" or "undef")
53
=== TEST 2: number typed value
55
lua_shared_dict dogs 1m;
59
local ffi = require "ffi"
72
double n; /* number */
73
ngx_str_t s; /* string */
76
} ngx_http_lua_value_t;
78
void *ngx_http_lua_find_zone(char *data, size_t len);
79
intptr_t ngx_http_lua_shared_dict_get(void *zone, char *kdata, size_t klen, ngx_http_lua_value_t *val);
82
local dogs = ngx.shared.dogs
83
dogs:set("foo", 1234567)
84
dogs:set("bar", 3.14159)
86
local buf = ffi.new("char[?]", 4)
88
ffi.copy(buf, "dogs", 4)
89
zone = ffi.C.ngx_http_lua_find_zone(buf, 4)
91
local val = ffi.new("ngx_http_lua_value_t[?]", 1)
93
ffi.copy(buf, "foo", 3)
94
local rc = ffi.C.ngx_http_lua_shared_dict_get(zone, buf, 3, val)
95
ngx.say("foo: rc=", tonumber(rc),
96
", type=", val[0].type,
97
", val=", tonumber(val[0].value.n))
99
ffi.copy(buf, "bar", 3)
100
local rc = ffi.C.ngx_http_lua_shared_dict_get(zone, buf, 3, val)
101
ngx.say("bar: rc=", tonumber(rc),
102
", type=", val[0].type,
103
", val=", tonumber(val[0].value.n))
109
foo: rc=0, type=3, val=1234567
110
bar: rc=0, type=3, val=3.14159
116
=== TEST 3: boolean typed value
118
lua_shared_dict dogs 1m;
122
local ffi = require "ffi"
135
double n; /* number */
136
ngx_str_t s; /* string */
139
} ngx_http_lua_value_t;
141
void *ngx_http_lua_find_zone(char *data, size_t len);
142
intptr_t ngx_http_lua_shared_dict_get(void *zone, char *kdata, size_t klen, ngx_http_lua_value_t *val);
145
local dogs = ngx.shared.dogs
146
dogs:set("foo", true)
147
dogs:set("bar", false)
149
local buf = ffi.new("char[?]", 4)
151
ffi.copy(buf, "dogs", 4)
152
zone = ffi.C.ngx_http_lua_find_zone(buf, 4)
154
local val = ffi.new("ngx_http_lua_value_t[?]", 1)
156
ffi.copy(buf, "foo", 3)
157
local rc = ffi.C.ngx_http_lua_shared_dict_get(zone, buf, 3, val)
158
ngx.say("foo: rc=", tonumber(rc),
159
", type=", tonumber(val[0].type),
160
", val=", tonumber(val[0].value.b))
162
local val = ffi.new("ngx_http_lua_value_t[?]", 1)
163
ffi.copy(buf, "bar", 3)
164
local rc = ffi.C.ngx_http_lua_shared_dict_get(zone, buf, 3, val)
165
ngx.say("bar: rc=", tonumber(rc),
166
", type=", tonumber(val[0].type),
167
", val=", tonumber(val[0].value.b))
173
foo: rc=0, type=1, val=1
174
bar: rc=0, type=1, val=0
180
=== TEST 4: key not found
182
lua_shared_dict dogs 1m;
186
local ffi = require "ffi"
199
double n; /* number */
200
ngx_str_t s; /* string */
203
} ngx_http_lua_value_t;
205
void *ngx_http_lua_find_zone(char *data, size_t len);
206
intptr_t ngx_http_lua_shared_dict_get(void *zone, char *kdata, size_t klen, ngx_http_lua_value_t *val);
209
local dogs = ngx.shared.dogs
212
local buf = ffi.new("char[?]", 4)
214
ffi.copy(buf, "dogs", 4)
215
zone = ffi.C.ngx_http_lua_find_zone(buf, 4)
217
local val = ffi.new("ngx_http_lua_value_t[?]", 1)
219
ffi.copy(buf, "foo", 3)
220
local rc = ffi.C.ngx_http_lua_shared_dict_get(zone, buf, 3, val)
221
ngx.say("foo: rc=", tonumber(rc))
223
local val = ffi.new("ngx_http_lua_value_t[?]", 1)
224
ffi.copy(buf, "bar", 3)
225
local rc = ffi.C.ngx_http_lua_shared_dict_get(zone, buf, 3, val)
226
ngx.say("bar: rc=", tonumber(rc))
239
=== TEST 5: string typed value
241
lua_shared_dict dogs 1m;
245
local ffi = require "ffi"
258
double n; /* number */
259
ngx_str_t s; /* string */
262
} ngx_http_lua_value_t;
264
void *ngx_http_lua_find_zone(char *data, size_t len);
265
intptr_t ngx_http_lua_shared_dict_get(void *zone, char *kdata, size_t klen, ngx_http_lua_value_t *val);
268
local dogs = ngx.shared.dogs
269
dogs:set("foo", "hello world")
272
local buf = ffi.new("char[?]", 4)
274
ffi.copy(buf, "dogs", 4)
275
zone = ffi.C.ngx_http_lua_find_zone(buf, 4)
277
local s = ffi.new("char[?]", 20)
279
local val = ffi.new("ngx_http_lua_value_t[?]", 1)
280
val[0].value.s.len = 20
281
val[0].value.s.data = s
283
ffi.copy(buf, "foo", 3)
284
local rc = ffi.C.ngx_http_lua_shared_dict_get(zone, buf, 3, val)
285
ngx.say("foo: rc=", tonumber(rc),
286
", type=", tonumber(val[0].type),
287
", val=", ffi.string(val[0].value.s.data, val[0].value.s.len),
288
", len=", tonumber(val[0].value.s.len))
290
local val = ffi.new("ngx_http_lua_value_t[?]", 1)
291
val[0].value.s.len = 20
292
val[0].value.s.data = s
294
ffi.copy(buf, "bar", 3)
295
local rc = ffi.C.ngx_http_lua_shared_dict_get(zone, buf, 3, val)
296
ngx.say("bar: rc=", tonumber(rc),
297
", type=", tonumber(val[0].type),
298
", val=", ffi.string(val[0].value.s.data, val[0].value.s.len),
299
", len=", tonumber(val[0].value.s.len))
305
foo: rc=0, type=4, val=hello world, len=11
306
bar: rc=0, type=4, val=, len=0
312
=== TEST 6: nil typed value
314
lua_shared_dict dogs 1m;
318
local ffi = require "ffi"
331
double n; /* number */
332
ngx_str_t s; /* string */
335
} ngx_http_lua_value_t;
337
void *ngx_http_lua_find_zone(char *data, size_t len);
338
intptr_t ngx_http_lua_shared_dict_get(void *zone, char *kdata, size_t klen, ngx_http_lua_value_t *val);
341
local dogs = ngx.shared.dogs
344
local buf = ffi.new("char[?]", 4)
346
ffi.copy(buf, "dogs", 4)
347
zone = ffi.C.ngx_http_lua_find_zone(buf, 4)
349
local val = ffi.new("ngx_http_lua_value_t[?]", 1)
351
ffi.copy(buf, "foo", 3)
352
local rc = ffi.C.ngx_http_lua_shared_dict_get(zone, buf, 3, val)
353
ngx.say("foo: rc=", tonumber(rc))
365
=== TEST 7: find zone (multiple zones)
367
lua_shared_dict dogs 1m;
368
lua_shared_dict cats 1m;
372
local ffi = require "ffi"
375
void *ngx_http_lua_find_zone(char *data, size_t len);
378
local buf = ffi.new("char[?]", 4)
379
ffi.copy(buf, "cats", 4)
380
local zone = ffi.C.ngx_http_lua_find_zone(buf, 4)
381
local cats = tostring(zone)
383
ffi.copy(buf, "dogs", 4)
384
zone = ffi.C.ngx_http_lua_find_zone(buf, 4)
385
local dogs = tostring(zone)
387
ngx.say("dogs == cats ? ", dogs == cats)
388
-- ngx.say("dogs: ", dogs)
389
-- ngx.say("cats ", cats)