1- # vim:set ft= ts=4 sw=4 et:
2-
31use Test ::Nginx::Socket::Lua;
42use Cwd qw(cwd);
53
@@ -17,41 +15,39 @@ $ENV{TEST_NGINX_RESOLVER} = '8.8.8.8';
1715$ ENV {TEST_NGINX_REDIS_PORT} ||= 6379 ;
1816
1917no_long_string();
20- # no_diff();
21-
2218run_tests();
2319
2420__DATA__
2521
2622=== TEST 1 : basic
2723-- - http_config eval: $::HttpConfig
2824-- - config
29- location / t {
30- content_by_lua '
31- local redis_connector = require "resty.redis.connector"
32- local rc = redis_connector.new()
25+ location / t {
26+ content_by_lua_block {
27+ local redis_connector = require " resty.redis.connector"
28+ local rc = redis_connector. new ()
3329
34- local params = { host = "127.0.0.1", port = $TEST_NGINX_REDIS_PORT }
30+ local params = { host = " 127.0.0.1" , port = $ TEST_NGINX_REDIS_PORT }
3531
36- local redis, err = rc:connect(params)
37- if not redis then
38- ngx.say("failed to connect: ", err)
39- return
40- end
32+ local redis, err = rc: connect(params)
33+ if not redis then
34+ ngx. say (" failed to connect: " , err)
35+ return
36+ end
4137
42- local res, err = redis:set("dog", "an animal")
43- if not res then
44- ngx.say("failed to set dog: ", err)
45- return
46- end
38+ local res, err = redis: set(" dog" , " an animal" )
39+ if not res then
40+ ngx. say (" failed to set dog: " , err)
41+ return
42+ end
4743
48- ngx.say("set dog: ", res)
44+ ngx. say (" set dog: " , res)
4945
50- redis:close()
51- ' ;
46+ redis: close()
5247 }
48+ }
5349-- - request
54- GET / t
50+ GET / t
5551-- - response_body
5652set dog: OK
5753-- - no_error_log
@@ -61,41 +57,41 @@ set dog: OK
6157=== TEST 2 : test we can try a list of hosts, and connect to the first working one
6258-- - http_config eval: $::HttpConfig
6359-- - config
64- location / t {
65- content_by_lua '
66- local redis_connector = require "resty.redis.connector"
67- local rc = redis_connector.new()
68- rc:set_connect_timeout(100)
60+ location / t {
61+ content_by_lua_block {
62+ local redis_connector = require " resty.redis.connector"
63+ local rc = redis_connector. new ()
64+ rc: set_connect_timeout(100 )
6965
70- local hosts = {
71- { host = "127.0.0.1", port = 1 },
72- { host = "127.0.0.1", port = 2 },
73- { host = "127.0.0.1", port = $TEST_NGINX_REDIS_PORT },
74- }
66+ local hosts = {
67+ { host = " 127.0.0.1" , port = 1 },
68+ { host = " 127.0.0.1" , port = 2 },
69+ { host = " 127.0.0.1" , port = $ TEST_NGINX_REDIS_PORT },
70+ }
7571
76- local redis, err, previous_errors = rc:try_hosts(hosts)
77- if not redis then
78- ngx.say("failed to connect: ", err)
79- return
80- end
72+ local redis, err, previous_errors = rc: try_hosts(hosts)
73+ if not redis then
74+ ngx. say (" failed to connect: " , err)
75+ return
76+ end
8177
82- -- Print the failed connection errors
83- ngx.say("connection 1 error: ", err)
78+ -- Print the failed connection errors
79+ ngx. say (" connection 1 error: " , err)
8480
85- ngx.say("connection 2 error: ", previous_errors[1])
81+ ngx. say (" connection 2 error: " , previous_errors[1 ])
8682
87- local res, err = redis:set("dog", "an animal")
88- if not res then
89- ngx.say("failed to set dog: ", err)
90- return
91- end
83+ local res, err = redis: set(" dog" , " an animal" )
84+ if not res then
85+ ngx. say (" failed to set dog: " , err)
86+ return
87+ end
9288
93- ngx.say("set dog: ", res)
89+ ngx. say (" set dog: " , res)
9490
9591
96- redis:close()
97- ' ;
92+ redis: close()
9893 }
94+ }
9995-- - request
10096 GET / t
10197-- - response_body
@@ -109,30 +105,30 @@ set dog: OK
109105=== TEST 3 : Test connect_to_host directly
110106-- - http_config eval: $::HttpConfig
111107-- - config
112- location / t {
113- content_by_lua '
114- local redis_connector = require "resty.redis.connector"
115- local rc = redis_connector.new()
116-
117- local host = { host = "127.0.0.1", port = $TEST_NGINX_REDIS_PORT }
118-
119- local redis, err = rc:connect_to_host(host)
120- if not redis then
121- ngx.say("failed to connect: ", err)
122- return
123- end
124-
125- local res, err = redis:set("dog", "an animal")
126- if not res then
127- ngx.say("failed to set dog: ", err)
128- return
129- end
130-
131- ngx.say("set dog: ", res)
132-
133- redis:close()
134- ' ;
135- }
108+ location / t {
109+ content_by_lua_block {
110+ local redis_connector = require " resty.redis.connector"
111+ local rc = redis_connector. new ()
112+
113+ local host = { host = " 127.0.0.1" , port = $ TEST_NGINX_REDIS_PORT }
114+
115+ local redis, err = rc: connect_to_host(host)
116+ if not redis then
117+ ngx. say (" failed to connect: " , err)
118+ return
119+ end
120+
121+ local res, err = redis: set(" dog" , " an animal" )
122+ if not res then
123+ ngx. say (" failed to set dog: " , err)
124+ return
125+ end
126+
127+ ngx. say (" set dog: " , res)
128+
129+ redis: close()
130+ }
131+ }
136132-- - request
137133 GET / t
138134-- - response_body
@@ -144,40 +140,40 @@ set dog: OK
144140=== TEST 4 : Test connect options override
145141-- - http_config eval: $::HttpConfig
146142-- - config
147- location / t {
148- content_by_lua '
149- local redis_connector = require "resty.redis.connector"
150- local rc = redis_connector.new()
151-
152- local host = {
153- host = "127.0.0.1",
154- port = $TEST_NGINX_REDIS_PORT,
155- db = 1,
156- }
157-
158- local redis, err = rc:connect_to_host(host)
159- if not redis then
160- ngx.say("failed to connect: ", err)
161- return
162- end
163-
164- local res, err = redis:set("dog", "an animal")
165- if not res then
166- ngx.say("failed to set dog: ", err)
167- return
168- end
169-
170- ngx.say("set dog: ", res)
171-
172- redis:select(2)
173- ngx.say(redis:get("dog"))
174-
175- redis:select(1)
176- ngx.say(redis:get("dog"))
177-
178- redis:close()
179- ' ;
143+ location / t {
144+ content_by_lua_block {
145+ local redis_connector = require " resty.redis.connector"
146+ local rc = redis_connector. new ()
147+
148+ local host = {
149+ host = " 127.0.0.1" ,
150+ port = $ TEST_NGINX_REDIS_PORT ,
151+ db = 1 ,
152+ }
153+
154+ local redis, err = rc: connect_to_host(host)
155+ if not redis then
156+ ngx. say (" failed to connect: " , err)
157+ return
158+ end
159+
160+ local res, err = redis: set(" dog" , " an animal" )
161+ if not res then
162+ ngx. say (" failed to set dog: " , err)
163+ return
164+ end
165+
166+ ngx. say (" set dog: " , res)
167+
168+ redis: select(2 )
169+ ngx. say (redis: get(" dog" ))
170+
171+ redis: select(1 )
172+ ngx. say (redis: get(" dog" ))
173+
174+ redis: close()
180175 }
176+ }
181177-- - request
182178 GET / t
183179-- - response_body
0 commit comments