1- use Test ::Nginx::Socket::Lua ;
1+ use Test ::Nginx::Socket ' no_plan ' ;
22use Cwd qw(cwd);
33
4- plan tests => repeat_each() * (3 * blocks() - 1 );
5-
64my $ pwd = cwd();
75
86our $ HttpConfig = qq {
@@ -17,48 +15,39 @@ run_tests();
1715
1816__DATA__
1917
20- === TEST 1 : basic
18+ === TEST 1 : basic connect
2119-- - http_config eval: $::HttpConfig
2220-- - config
2321location / t {
2422 content_by_lua_block {
25- local rc = require (" resty.redis.connector" ). new ()
26-
27- local params = { host = " 127.0.0.1" , port = $ TEST_NGINX_REDIS_PORT }
28-
29- local redis, err = rc: connect(params)
30- if not redis then
31- ngx. say (" failed to connect: " , err)
32- return
33- end
23+ local rc = require (" resty.redis.connector" ). new ({
24+ port = $ TEST_NGINX_REDIS_PORT
25+ })
3426
35- local res, err = redis: set(" dog" , " an animal" )
36- if not res then
37- ngx. say (" failed to set dog: " , err)
38- return
39- end
27+ local redis = assert(rc: connect(params),
28+ " connect should return positively" )
4029
41- ngx. say (" set dog: " , res)
30+ assert(redis: set(" dog" , " an animal" ),
31+ " redis:set should return positively" )
4232
4333 redis: close()
4434 }
4535}
4636-- - request
4737GET / t
48- -- - response_body
49- set dog: OK
5038-- - no_error_log
5139[error]
5240
5341
54- === TEST 2 : test we can try a list of hosts, and connect to the first working one
42+ === TEST 2 : try_hosts
5543-- - http_config eval: $::HttpConfig
5644-- - config
5745location / t {
46+ lua_socket_log_errors off;
5847 content_by_lua_block {
59- local redis_connector = require " resty.redis.connector"
60- local rc = redis_connector . new ()
61- rc : set_connect_timeout( 100 )
48+ local rc = require ( " resty.redis.connector" ) . new ({
49+ connect_timeout = 100 ,
50+ } )
6251
6352 local hosts = {
6453 { host = " 127.0.0.1" , port = 1 },
@@ -67,36 +56,38 @@ location /t {
6756 }
6857
6958 local redis, err, previous_errors = rc: try_hosts(hosts)
70- if not redis then
71- ngx. say (" failed to connect: " , err)
72- return
73- end
59+ assert(redis and not err,
60+ " try_hosts should return a connection and no error" )
7461
75- -- Print the failed connection errors
76- ngx. say (" connection 1 error: " , err)
62+ assert(previous_errors[1 ] == " connection refused" ,
63+ " previous_errors[1] should be 'connection refused'" )
64+ assert(previous_errors[2 ] == " connection refused" ,
65+ " previous_errors[2] should be 'connection refused'" )
7766
78- ngx. say (" connection 2 error: " , previous_errors[1 ])
67+ assert(redis: set(" dog" , " an animal" ),
68+ " redis connection should be working" )
7969
80- local res, err = redis: set(" dog" , " an animal" )
81- if not res then
82- ngx. say (" failed to set dog: " , err)
83- return
84- end
70+ redis: close()
8571
86- ngx. say (" set dog: " , res)
72+ local hosts = {
73+ { host = " 127.0.0.1" , port = 1 },
74+ { host = " 127.0.0.1" , port = 2 },
75+ }
8776
77+ local redis, err, previous_errors = rc: try_hosts(hosts)
78+ assert(not redis and err == " no hosts available" ,
79+ " no available hosts should return an error" )
8880
89- redis: close()
81+ assert(previous_errors[1 ] == " connection refused" ,
82+ " previous_errors[1] should be 'connection refused'" )
83+ assert(previous_errors[2 ] == " connection refused" ,
84+ " previous_errors[2] should be 'connection refused'" )
9085 }
9186}
9287-- - request
9388 GET / t
94- -- - response_body
95- connection 1 error: connection refused
96- connection 2 error: connection refused
97- set dog: OK
98- -- - error_log
99- 111 : Connection refused
89+ -- - no_error_log
90+ [error]
10091
10192
10293=== TEST 3 : Test connect_to_host directly
0 commit comments