diff --git a/.travis.yml b/.travis.yml index d8de17ec..a56e9b37 100644 --- a/.travis.yml +++ b/.travis.yml @@ -53,12 +53,12 @@ install: - git clone https://github.com/openresty/mockeagain.git - git clone https://github.com/openresty/test-nginx.git - git clone -b v2.1-agentzh https://github.com/openresty/luajit2.git - - git clone https://github.com/openresty/lua-nginx-module.git ../lua-nginx-module + - git clone https://github.com/webcore-no/lua-nginx-module.git ../lua-nginx-module - git clone https://github.com/openresty/echo-nginx-module.git ../echo-nginx-module - git clone https://github.com/openresty/memc-nginx-module.git ../memc-nginx-module - git clone https://github.com/openresty/headers-more-nginx-module.git ../headers-more-nginx-module - git clone https://github.com/openresty/lua-resty-lrucache.git ../lua-resty-lrucache - - git clone https://github.com/openresty/lua-resty-core.git ../lua-resty-core + - git clone https://github.com/webcore-no/lua-resty-core.git ../lua-resty-core script: - sudo iptables -I OUTPUT 1 -p udp --dport 10086 -j REJECT diff --git a/src/ngx_stream_lua_shdict.c b/src/ngx_stream_lua_shdict.c index 4779ce8d..73c80672 100644 --- a/src/ngx_stream_lua_shdict.c +++ b/src/ngx_stream_lua_shdict.c @@ -1496,7 +1496,7 @@ int ngx_stream_lua_ffi_shdict_get(ngx_shm_zone_t *zone, u_char *key, size_t key_len, int *value_type, u_char **str_value_buf, size_t *str_value_len, double *num_value, int *user_flags, - int get_stale, int *is_stale, char **err) + int *user_flags_neq, int get_stale, int *is_stale, char **err) { ngx_str_t name; uint32_t hash; @@ -1537,6 +1537,14 @@ ngx_stream_lua_ffi_shdict_get(ngx_shm_zone_t *zone, u_char *key, return NGX_OK; } + if (*user_flags_neq && *user_flags == (int) sd->user_flags) { + *is_stale = (rc == NGX_DONE); + *user_flags_neq = 0; + ngx_shmtx_unlock(&ctx->shpool->mutex); + *value_type = LUA_TNIL; + return NGX_OK; + } + /* rc == NGX_OK || (rc == NGX_DONE && get_stale) */ *value_type = sd->value_type; diff --git a/t/167-shdict-neq-flags.t b/t/167-shdict-neq-flags.t new file mode 100644 index 00000000..f1d250f3 --- /dev/null +++ b/t/167-shdict-neq-flags.t @@ -0,0 +1,259 @@ +# vim:set ft= ts=4 sw=4 et fdm=marker: +use Test::Nginx::Socket::Lua::Stream 'no_plan'; + +run_tests(); + +__DATA__ + +=== TEST 1: flag eq +--- stream_config + lua_shared_dict dogs 1m; +--- stream_server_config + content_by_lua_block { + local dogs = ngx.shared.dogs + dogs:set("Bernese", 42, 0, 1) + local val = dogs:get("Bernese", 1) + ngx.say(val, " ", type(val)) + } +--- stream_response +nil nil +--- no_error_log +[error] + + + +=== TEST 2: fleq neq +--- stream_config + lua_shared_dict dogs 1m; +--- stream_server_config + content_by_lua_block { + local dogs = ngx.shared.dogs + dogs:set("Bernese", 42, 0, 1) + local val = dogs:get("Bernese", 2) + ngx.say(val, " " , type(val)) + } +--- stream_response +42 number +--- no_error_log +[error] + + + +=== TEST 3: set with no flag +--- stream_config + lua_shared_dict dogs 1m; +--- stream_server_config + content_by_lua_block { + local dogs = ngx.shared.dogs + dogs:set("Bernese", 42, 0) + local val = dogs:get("Bernese", 2) + ngx.say(val, " " , type(val)) + } +--- stream_response +42 number +--- no_error_log +[error] + + + +=== TEST 4: get with no flag +--- stream_config + lua_shared_dict dogs 1m; +--- stream_server_config + content_by_lua_block { + local dogs = ngx.shared.dogs + dogs:set("Bernese", 42, 0, 1) + local val = dogs:get("Bernese") + ngx.say(val, " " , type(val)) + } +--- stream_response +42 number +--- no_error_log +[error] + + + +=== TEST 5: set and get with no flag +--- stream_config + lua_shared_dict dogs 1m; +--- stream_server_config + content_by_lua_block { + local dogs = ngx.shared.dogs + dogs:set("Bernese", 42, 0) + local val = dogs:get("Bernese") + ngx.say(val, " " , type(val)) + } +--- stream_response +42 number +--- no_error_log +[error] + + + +=== TEST 6: set no flag, and read with 0 flag +--- stream_config + lua_shared_dict dogs 1m; +--- stream_server_config + content_by_lua_block { + local dogs = ngx.shared.dogs + dogs:set("Bernese", 42) + local val = dogs:get("Bernese", 0) + ngx.say(val, " " , type(val)) + } +--- stream_response +nil nil +--- no_error_log +[error] + + +=== TEST 7: flags_match is true +--- stream_config + lua_shared_dict dogs 1m; +--- stream_server_config + content_by_lua_block { + local dogs = ngx.shared.dogs + dogs:set("Bernese", 42, 0, 1) + local val, err, flags_match = dogs:get("Bernese", 1) + + ngx.say(val, " ", type(val), " : ", + err, " ", type(err), " : ", + flags_match, " ", type(flags_match)) + } +--- stream_response +nil nil : nil nil : true boolean +--- no_error_log +[error] + + + +=== TEST 8: flags_match is nil +--- stream_config + lua_shared_dict dogs 1m; +--- stream_server_config + content_by_lua_block { + local dogs = ngx.shared.dogs + local val, err, flags_match = dogs:get("Bernese", 3) + ngx.say(flags_match, " ", type(flags_match)) + } +--- stream_response +nil nil +--- no_error_log +[error] + + + +=== TEST 9: get when flag is not number +--- stream_config + lua_shared_dict dogs 1m; +--- stream_server_config + content_by_lua_block { + local dogs = ngx.shared.dogs + dogs:set("Bernese", 42, 0, 1) + local val = dogs:get("Bernese", {}) + } +--- error_log +cannot convert 'table' to 'int' + + +=== TEST 10: flag eq stale +--- stream_config + lua_shared_dict dogs 1m; +--- stream_server_config + content_by_lua_block { + local dogs = ngx.shared.dogs + dogs:set("Bernese", 42, 0.01, 1) + ngx.sleep(0.02) + local val = dogs:get_stale("Bernese", 1) + ngx.say(val, " ", type(val)) + } +--- stream_response +nil nil +--- no_error_log +[error] + + + +=== TEST 11: fleq neq stale +--- stream_config + lua_shared_dict dogs 1m; +--- stream_server_config + content_by_lua_block { + local dogs = ngx.shared.dogs + dogs:set("Bernese", 42, 0.01, 1) + ngx.sleep(0.02) + local val = dogs:get_stale("Bernese", 2) + ngx.say(val, " " , type(val)) + } +--- stream_response +42 number +--- no_error_log +[error] + + + +=== TEST 12: get_stale with no flag +--- stream_config + lua_shared_dict dogs 1m; +--- stream_server_config + content_by_lua_block { + local dogs = ngx.shared.dogs + dogs:set("Bernese", 42, 0.01, 1) + ngx.sleep(0.02) + local val = dogs:get_stale("Bernese") + ngx.say(val, " " , type(val)) + } +--- stream_response +42 number +--- no_error_log +[error] + + + +=== TEST 13: flags_match is true +--- stream_config + lua_shared_dict dogs 1m; +--- stream_server_config + content_by_lua_block { + local dogs = ngx.shared.dogs + dogs:set("Bernese", 42, 0.01, 1) + ngx.sleep(0.02) + local val, err, stale, flags_match = dogs:get_stale("Bernese", 1) + + ngx.say(val, " ", type(val), " : ", + err, " ", type(err), " : ", + flags_match, " ", type(flags_match)) + } +--- stream_response +nil nil : nil nil : true boolean +--- no_error_log +[error] + + + +=== TEST 14: flags_match is nil +--- stream_config + lua_shared_dict dogs 1m; +--- stream_server_config + content_by_lua_block { + local dogs = ngx.shared.dogs + local val, err, stale, flags_match = dogs:get_stale("Bernese", 3) + ngx.say(flags_match, " ", type(flags_match)) + } +--- stream_response +nil nil +--- no_error_log +[error] + + + +=== TEST 15: get when flag is not number +--- stream_config + lua_shared_dict dogs 1m; +--- stream_server_config + content_by_lua_block { + local dogs = ngx.shared.dogs + dogs:set("Bernese", 42, 0, 1) + local val = dogs:get_stale("Bernese", {}) + } +--- error_log +cannot convert 'table' to 'int'