From 9e45887bb79c570366e089ab23894d822f19ab20 Mon Sep 17 00:00:00 2001 From: Yuansheng Wang Date: Wed, 8 May 2019 14:43:32 +0800 Subject: [PATCH] doc: how to install the depend library. --- README.md | 16 +++++++++------ t/etcd-dir.t | 17 ++++++--------- t/etcd-key.t | 16 +++++---------- t/etcd-stats.t | 13 ++++-------- t/normalize.t | 10 +++------ t/typeof.t | 56 +++++++++++++++++++++++--------------------------- 6 files changed, 54 insertions(+), 74 deletions(-) diff --git a/README.md b/README.md index 086ec6e7..46ffe48a 100644 --- a/README.md +++ b/README.md @@ -9,6 +9,10 @@ lua-resty-etcd - lua-resty-http: https://github.com/ledgetech/lua-resty-http +```shell +luarocks install lua-resty-http +``` + ## Create client object #### cli, err = Etcd.new([option:table]) @@ -21,18 +25,18 @@ local cli, err = require('resty.etcd').new() - `option:table` - `host`: string - default `http://127.0.0.1:2379` - - `ttl`: int - default `-1` + - `ttl`: int - default `-1` default ttl for key operation. set -1 to disable ttl. - - `prefix`: string + - `prefix`: string append this prefix path string to key operation url `'/v2/keys'`. - - `timeout`: int + - `timeout`: int request timeout seconds. **Returns** 1. `cli`: client object. -2. `err`: error string. +2. `err`: error string. ## About the return values of client methods. @@ -48,7 +52,7 @@ a `HTTP Response Entity` contains the following fields except `408` timeout stat **Note:** a client method will decode a response body as a JSON string if a `Content-Type` response header value is a `application/json`. -please refer the **etcd API documentaion** at - https://github.com/coreos/etcd for more details of a response entity. +please refer the **etcd API documentaion** at - https://github.com/coreos/etcd for more details of a response entity. ## Key-value operations @@ -224,7 +228,7 @@ local res, err = cli:waitdir('/path/to/dir') #### res, err = cli:push(key:string, val:JSON encodable value [, ttl:int]) -push a value into the specified directory. +push a value into the specified directory. ```lua local res, err = cli:mkdir('/path/to/dir') diff --git a/t/etcd-dir.t b/t/etcd-dir.t index 1c1f60dc..365f54d0 100644 --- a/t/etcd-dir.t +++ b/t/etcd-dir.t @@ -1,16 +1,11 @@ -# vim:set ft= ts=4 sw=4 et fdm=marker: - -use Test::Nginx::Socket::Lua; +use Test::Nginx::Socket::Lua 'no_plan'; log_level('warn'); - repeat_each(2); -plan tests => repeat_each() * (blocks() * 3); - our $HttpConfig = <<'_EOC_'; lua_socket_log_errors off; - lua_package_path 'lib/?.lua;;'; + lua_package_path '/usr/share/lua/5.1/?.lua;lib/?.lua;;'; init_by_lua_block { function check_res(data, err, val, err_msg, is_dir) if err then @@ -30,17 +25,17 @@ our $HttpConfig = <<'_EOC_'; if err_msg then if err_msg ~= data.body.message then - ngx.say("failed to check error msg, got:", + ngx.say("failed to check error msg, got:", data.body.message, ", expect: ", val) ngx.exit(200) else ngx.say("checked error msg as expect: ", err_msg) end end - + if is_dir then if not data.body.node.dir then - ngx.say("failed to check dir, got normal file:", + ngx.say("failed to check dir, got normal file:", data.body.node.dir) ngx.exit(200) else @@ -213,7 +208,7 @@ checked val as expect: b local res2, err = etcd:wait("/dir", res.body.node.modifiedIndex + 1, 1) ngx.say("err: ", err, ", more than 1sec: ", ngx.now() - cur_time > 1) - ngx.timer.at(1, function () + ngx.timer.at(1, function () etcd:set("/dir/a", "a") end) diff --git a/t/etcd-key.t b/t/etcd-key.t index 894522e0..f7b8bf85 100644 --- a/t/etcd-key.t +++ b/t/etcd-key.t @@ -1,16 +1,11 @@ -# vim:set ft= ts=4 sw=4 et fdm=marker: - -use Test::Nginx::Socket::Lua; +use Test::Nginx::Socket::Lua 'no_plan'; log_level('warn'); - repeat_each(2); -plan tests => repeat_each() * (blocks() * 3); - our $HttpConfig = <<'_EOC_'; lua_socket_log_errors off; - lua_package_path 'lib/?.lua;;'; + lua_package_path '/usr/share/lua/5.1/?.lua;lib/?.lua;;'; init_by_lua_block { function check_res(data, err, val, err_msg) if err then @@ -30,14 +25,13 @@ our $HttpConfig = <<'_EOC_'; if err_msg then if err_msg ~= data.body.message then - ngx.say("failed to check error msg, got:", + ngx.say("failed to check error msg, got:", data.body.message, ", expect: ", val) ngx.exit(200) else ngx.say("checked error msg as expect: ", err_msg) end end - end } _EOC_ @@ -138,7 +132,7 @@ checked error msg as expect: Key not found res, err = etcd:get("/test") check_res(res, err, "abc") - + etcd:delete("/test", "abc") res, err = etcd:get("/test") @@ -232,7 +226,7 @@ checked val as expect: abc local res2, err = etcd:wait("/test", res.body.node.modifiedIndex + 1, 1) ngx.say("err: ", err, ", more than 1sec: ", ngx.now() - cur_time > 1) - ngx.timer.at(1, function () + ngx.timer.at(1, function () etcd:set("/test", "bcd") end) diff --git a/t/etcd-stats.t b/t/etcd-stats.t index fe236612..a9835714 100644 --- a/t/etcd-stats.t +++ b/t/etcd-stats.t @@ -1,16 +1,11 @@ -# vim:set ft= ts=4 sw=4 et fdm=marker: - -use Test::Nginx::Socket::Lua; +use Test::Nginx::Socket::Lua 'no_plan'; log_level('warn'); - repeat_each(2); -plan tests => repeat_each() * (blocks() * 3); - our $HttpConfig = <<'_EOC_'; lua_socket_log_errors off; - lua_package_path 'lib/?.lua;;'; + lua_package_path '/usr/share/lua/5.1/?.lua;lib/?.lua;;'; init_by_lua_block { function check_res(data, err) if err then @@ -86,7 +81,7 @@ followers type: table check_res(res, err) -- ngx.say(require "cjson" .encode(res.body)) - + assert(res.body.id) assert(res.body.startTime) assert(res.body.leaderInfo) @@ -112,7 +107,7 @@ GET /t check_res(res, err) -- ngx.log(ngx.WARN, require "cjson" .encode(res.body)) - + assert(res.body.compareAndSwapFail) assert(res.body.updateFail) assert(res.body.getsFail) diff --git a/t/normalize.t b/t/normalize.t index 33d5d3a2..62fb80f0 100644 --- a/t/normalize.t +++ b/t/normalize.t @@ -1,15 +1,11 @@ -# vim:set ft= ts=4 sw=4 et fdm=marker: - -use Test::Nginx::Socket::Lua; +use Test::Nginx::Socket::Lua 'no_plan'; log_level('warn'); - repeat_each(2); -plan tests => repeat_each() * (blocks() * 3); - our $HttpConfig = <<'_EOC_'; - lua_package_path 'lib/?.lua;;'; + lua_socket_log_errors off; + lua_package_path '/usr/share/lua/5.1/?.lua;lib/?.lua;;'; _EOC_ run_tests(); diff --git a/t/typeof.t b/t/typeof.t index 7d1df65d..0f029c5c 100644 --- a/t/typeof.t +++ b/t/typeof.t @@ -1,15 +1,11 @@ -# vim:set ft= ts=4 sw=4 et fdm=marker: - -use Test::Nginx::Socket::Lua; +use Test::Nginx::Socket::Lua 'no_plan'; log_level('warn'); - repeat_each(2); -plan tests => repeat_each() * (blocks() * 3); - our $HttpConfig = <<'_EOC_'; - lua_package_path 'lib/?.lua;;'; + lua_socket_log_errors off; + lua_package_path '/usr/share/lua/5.1/?.lua;lib/?.lua;;'; _EOC_ run_tests(); @@ -47,38 +43,38 @@ __DATA__ -- nil { chk = { ['nil'] = true, - ['non'] = true + ['non'] = true } }, -- boolean { val = true, chk = { - ['boolean'] = true + ['boolean'] = true } }, { val = false, chk = { ['boolean'] = true, - ['non'] = true + ['non'] = true } }, -- string { val = 'hello', chk = { - ['string'] = true + ['string'] = true } }, { val = 'world', chk = { - ['string'] = true + ['string'] = true } }, { val = '', chk = { ['string'] = true, - ['non'] = true + ['non'] = true } }, @@ -96,7 +92,7 @@ __DATA__ ['uint8'] = true, ['uint16'] = true, ['uint32'] = true, - ['non'] = true + ['non'] = true } }, { val = 1, @@ -128,28 +124,28 @@ __DATA__ chk = { ['number'] = true, ['finite'] = true, - ['unsigned']= true + ['unsigned']= true } }, { val = -0.1, chk = { ['number'] = true, - ['finite'] = true + ['finite'] = true } }, { val = 1/0, chk = { - ['number'] = true + ['number'] = true } }, { val = 0/0, chk = { ['number'] = true, ['nan'] = true, - ['non'] = true + ['non'] = true } }, - + -- integer { val = -128, chk = { @@ -158,7 +154,7 @@ __DATA__ ['int'] = true, ['int8'] = true, ['int16'] = true, - ['int32'] = true + ['int32'] = true } }, { val = 127, @@ -203,7 +199,7 @@ __DATA__ ['number'] = true, ['finite'] = true, ['int'] = true, - ['int32'] = true + ['int32'] = true } }, { val = 2147483647, @@ -229,7 +225,7 @@ __DATA__ ['uint'] = true, ['uint8'] = true, ['uint16'] = true, - ['uint32'] = true + ['uint32'] = true } }, { val = 65535, @@ -251,35 +247,35 @@ __DATA__ ['unsigned']= true, ['int'] = true, ['uint'] = true, - ['uint32'] = true + ['uint32'] = true } }, - + -- function { val = function()end, chk = { - ['Function']= true + ['Function']= true } }, - + -- table { val = {}, chk = { - ['table'] = true + ['table'] = true } }, - + -- thread { val = coroutine.create(function() end), chk = { - ['thread'] = true + ['thread'] = true } } } local nilVal local msg - for _, field in ipairs(data) do + for _, field in ipairs(data) do for method, res in pairs(DEFAULT_CMP) do if field.chk[method] ~= nil then res = field.chk[method]