1
- use Test ::Nginx::Socket::Lua ;
1
+ use Test ::Nginx::Socket ' no_plan ' ;
2
2
use Cwd qw(cwd);
3
3
4
- plan tests => repeat_each() * (3 * blocks() - 1 );
5
-
6
4
my $ pwd = cwd();
7
5
8
6
our $ HttpConfig = qq {
@@ -17,48 +15,39 @@ run_tests();
17
15
18
16
__DATA__
19
17
20
- === TEST 1 : basic
18
+ === TEST 1 : basic connect
21
19
-- - http_config eval: $::HttpConfig
22
20
-- - config
23
21
location / t {
24
22
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
+ })
34
26
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" )
40
29
41
- ngx. say (" set dog: " , res)
30
+ assert(redis: set(" dog" , " an animal" ),
31
+ " redis:set should return positively" )
42
32
43
33
redis: close()
44
34
}
45
35
}
46
36
-- - request
47
37
GET / t
48
- -- - response_body
49
- set dog: OK
50
38
-- - no_error_log
51
39
[error]
52
40
53
41
54
- === TEST 2 : test we can try a list of hosts, and connect to the first working one
42
+ === TEST 2 : try_hosts
55
43
-- - http_config eval: $::HttpConfig
56
44
-- - config
57
45
location / t {
46
+ lua_socket_log_errors off;
58
47
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
+ } )
62
51
63
52
local hosts = {
64
53
{ host = " 127.0.0.1" , port = 1 },
@@ -67,36 +56,38 @@ location /t {
67
56
}
68
57
69
58
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" )
74
61
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'" )
77
66
78
- ngx. say (" connection 2 error: " , previous_errors[1 ])
67
+ assert(redis: set(" dog" , " an animal" ),
68
+ " redis connection should be working" )
79
69
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()
85
71
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
+ }
87
76
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" )
88
80
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'" )
90
85
}
91
86
}
92
87
-- - request
93
88
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]
100
91
101
92
102
93
=== TEST 3 : Test connect_to_host directly
0 commit comments