1
# vim:set ft= ts=4 sw=4 et fdm=marker:
3
use Test::Nginx::Socket;
5
#worker_connections(1014);
6
#master_process_enabled(1);
11
plan tests => repeat_each() * (blocks() * 3);
24
lua_shared_dict dogs 1m;
28
local ffi = require "ffi"
31
void *ngx_http_lua_find_zone(char *data, size_t len);
34
local buf = ffi.new("char[?]", 4)
35
ffi.copy(buf, "foo", 3)
36
local zone = ffi.C.ngx_http_lua_find_zone(buf, 3)
37
ngx.say("foo zone: ", tonumber(ffi.cast("long", zone)) ~= 0 and "defined" or "undef")
39
ffi.copy(buf, "dogs", 4)
40
zone = ffi.C.ngx_http_lua_find_zone(buf, 4)
41
ngx.say("dogs zone: ", tonumber(ffi.cast("long", zone)) ~= 0 and "defined" or "undef")
54
=== TEST 2: number typed value
56
lua_shared_dict dogs 1m;
60
local ffi = require "ffi"
73
double n; /* number */
74
ngx_str_t s; /* string */
77
} ngx_http_lua_value_t;
79
void *ngx_http_lua_find_zone(char *data, size_t len);
80
intptr_t ngx_http_lua_shared_dict_get(void *zone, char *kdata, size_t klen, ngx_http_lua_value_t *val);
83
local dogs = ngx.shared.dogs
84
dogs:set("foo", 1234567)
85
dogs:set("bar", 3.14159)
87
local buf = ffi.new("char[?]", 4)
89
ffi.copy(buf, "dogs", 4)
90
zone = ffi.C.ngx_http_lua_find_zone(buf, 4)
92
local val = ffi.new("ngx_http_lua_value_t[?]", 1)
94
ffi.copy(buf, "foo", 3)
95
local rc = ffi.C.ngx_http_lua_shared_dict_get(zone, buf, 3, val)
96
ngx.say("foo: rc=", tonumber(rc),
97
", type=", val[0].type,
98
", val=", tonumber(val[0].value.n))
100
ffi.copy(buf, "bar", 3)
101
local rc = ffi.C.ngx_http_lua_shared_dict_get(zone, buf, 3, val)
102
ngx.say("bar: rc=", tonumber(rc),
103
", type=", val[0].type,
104
", val=", tonumber(val[0].value.n))
110
foo: rc=0, type=3, val=1234567
111
bar: rc=0, type=3, val=3.14159
117
=== TEST 3: boolean typed value
119
lua_shared_dict dogs 1m;
123
local ffi = require "ffi"
136
double n; /* number */
137
ngx_str_t s; /* string */
140
} ngx_http_lua_value_t;
142
void *ngx_http_lua_find_zone(char *data, size_t len);
143
intptr_t ngx_http_lua_shared_dict_get(void *zone, char *kdata, size_t klen, ngx_http_lua_value_t *val);
146
local dogs = ngx.shared.dogs
147
dogs:set("foo", true)
148
dogs:set("bar", false)
150
local buf = ffi.new("char[?]", 4)
152
ffi.copy(buf, "dogs", 4)
153
zone = ffi.C.ngx_http_lua_find_zone(buf, 4)
155
local val = ffi.new("ngx_http_lua_value_t[?]", 1)
157
ffi.copy(buf, "foo", 3)
158
local rc = ffi.C.ngx_http_lua_shared_dict_get(zone, buf, 3, val)
159
ngx.say("foo: rc=", tonumber(rc),
160
", type=", tonumber(val[0].type),
161
", val=", tonumber(val[0].value.b))
163
local val = ffi.new("ngx_http_lua_value_t[?]", 1)
164
ffi.copy(buf, "bar", 3)
165
local rc = ffi.C.ngx_http_lua_shared_dict_get(zone, buf, 3, val)
166
ngx.say("bar: rc=", tonumber(rc),
167
", type=", tonumber(val[0].type),
168
", val=", tonumber(val[0].value.b))
174
foo: rc=0, type=1, val=1
175
bar: rc=0, type=1, val=0
181
=== TEST 4: key not found
183
lua_shared_dict dogs 1m;
187
local ffi = require "ffi"
200
double n; /* number */
201
ngx_str_t s; /* string */
204
} ngx_http_lua_value_t;
206
void *ngx_http_lua_find_zone(char *data, size_t len);
207
intptr_t ngx_http_lua_shared_dict_get(void *zone, char *kdata, size_t klen, ngx_http_lua_value_t *val);
210
local dogs = ngx.shared.dogs
213
local buf = ffi.new("char[?]", 4)
215
ffi.copy(buf, "dogs", 4)
216
zone = ffi.C.ngx_http_lua_find_zone(buf, 4)
218
local val = ffi.new("ngx_http_lua_value_t[?]", 1)
220
ffi.copy(buf, "foo", 3)
221
local rc = ffi.C.ngx_http_lua_shared_dict_get(zone, buf, 3, val)
222
ngx.say("foo: rc=", tonumber(rc))
224
local val = ffi.new("ngx_http_lua_value_t[?]", 1)
225
ffi.copy(buf, "bar", 3)
226
local rc = ffi.C.ngx_http_lua_shared_dict_get(zone, buf, 3, val)
227
ngx.say("bar: rc=", tonumber(rc))
240
=== TEST 5: string typed value
242
lua_shared_dict dogs 1m;
246
local ffi = require "ffi"
259
double n; /* number */
260
ngx_str_t s; /* string */
263
} ngx_http_lua_value_t;
265
void *ngx_http_lua_find_zone(char *data, size_t len);
266
intptr_t ngx_http_lua_shared_dict_get(void *zone, char *kdata, size_t klen, ngx_http_lua_value_t *val);
269
local dogs = ngx.shared.dogs
270
dogs:set("foo", "hello world")
273
local buf = ffi.new("char[?]", 4)
275
ffi.copy(buf, "dogs", 4)
276
zone = ffi.C.ngx_http_lua_find_zone(buf, 4)
278
local s = ffi.new("char[?]", 20)
280
local val = ffi.new("ngx_http_lua_value_t[?]", 1)
281
val[0].value.s.len = 20
282
val[0].value.s.data = s
284
ffi.copy(buf, "foo", 3)
285
local rc = ffi.C.ngx_http_lua_shared_dict_get(zone, buf, 3, val)
286
ngx.say("foo: rc=", tonumber(rc),
287
", type=", tonumber(val[0].type),
288
", val=", ffi.string(val[0].value.s.data, val[0].value.s.len),
289
", len=", tonumber(val[0].value.s.len))
291
local val = ffi.new("ngx_http_lua_value_t[?]", 1)
292
val[0].value.s.len = 20
293
val[0].value.s.data = s
295
ffi.copy(buf, "bar", 3)
296
local rc = ffi.C.ngx_http_lua_shared_dict_get(zone, buf, 3, val)
297
ngx.say("bar: rc=", tonumber(rc),
298
", type=", tonumber(val[0].type),
299
", val=", ffi.string(val[0].value.s.data, val[0].value.s.len),
300
", len=", tonumber(val[0].value.s.len))
306
foo: rc=0, type=4, val=hello world, len=11
307
bar: rc=0, type=4, val=, len=0
313
=== TEST 6: nil typed value
315
lua_shared_dict dogs 1m;
319
local ffi = require "ffi"
332
double n; /* number */
333
ngx_str_t s; /* string */
336
} ngx_http_lua_value_t;
338
void *ngx_http_lua_find_zone(char *data, size_t len);
339
intptr_t ngx_http_lua_shared_dict_get(void *zone, char *kdata, size_t klen, ngx_http_lua_value_t *val);
342
local dogs = ngx.shared.dogs
345
local buf = ffi.new("char[?]", 4)
347
ffi.copy(buf, "dogs", 4)
348
zone = ffi.C.ngx_http_lua_find_zone(buf, 4)
350
local val = ffi.new("ngx_http_lua_value_t[?]", 1)
352
ffi.copy(buf, "foo", 3)
353
local rc = ffi.C.ngx_http_lua_shared_dict_get(zone, buf, 3, val)
354
ngx.say("foo: rc=", tonumber(rc))