File: 026-mysql.t

package info (click to toggle)
nginx 1.18.0-6.1%2Bdeb11u3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 19,344 kB
  • sloc: ansic: 250,653; perl: 7,548; sh: 1,408; ruby: 879; python: 358; makefile: 338; awk: 36; cpp: 18
file content (130 lines) | stat: -rw-r--r-- 3,463 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# vim:set ft= ts=4 sw=4 et fdm=marker:

use Test::Nginx::Socket::Lua;

repeat_each(2);

plan tests => blocks() * repeat_each() * 3;

#$ENV{LUA_PATH} = $ENV{HOME} . '/work/JSON4Lua-0.9.30/json/?.lua';
$ENV{TEST_NGINX_MYSQL_PORT} ||= 3306;

no_long_string();

run_tests();

__DATA__

=== TEST 1: when mysql query timed out, kill that query by Lua
--- http_config
    upstream backend {
        drizzle_server 127.0.0.1:$TEST_NGINX_MYSQL_PORT protocol=mysql
                       dbname=ngx_test user=ngx_test password=ngx_test;
        drizzle_keepalive max=300 mode=single overflow=ignore;
    }
--- config
    location = /mysql {
        #internal;
        drizzle_send_query_timeout 100ms;
        #drizzle_send_query_timeout 1s;
        drizzle_query $echo_request_body;
        drizzle_pass backend;

        #error_page 504 /ret/504;
        rds_json on;
        more_set_headers -s 504 "X-Mysql-Tid: $drizzle_thread_id";
    }

    location /lua {
        content_by_lua '
            local sql = "select sleep(5)"
            local res = ngx.location.capture("/mysql",
                { method = ngx.HTTP_POST, body = sql })

            ngx.say("status = " .. res.status)

            local tid = res.header["X-Mysql-Tid"]
            if tid == nil then
                ngx.say("thread id = nil")
                return
            end

            tid = tonumber(tid)
            ngx.say("thread id = " .. tid)

            res = ngx.location.capture("/mysql",
                { method = ngx.HTTP_POST,
                  body = "kill query " .. tid })

            ngx.say("kill status = " .. res.status)
            ngx.say("kill body = " .. res.body)
        ';
    }
--- request
    GET /lua
--- response_body_like
^status = 504
thread id = \d+
kill status = 200
kill body = \{"errcode":0\}$
--- error_log eval
qr{upstream timed out \(\d+: Connection timed out\) while sending query to drizzle upstream}



=== TEST 2: no error pages
--- http_config
    upstream backend {
        drizzle_server 127.0.0.1:$TEST_NGINX_MYSQL_PORT protocol=mysql
                       dbname=ngx_test user=ngx_test password=ngx_test;
        drizzle_keepalive max=300 mode=single overflow=ignore;
    }
--- config
    location @err { echo Hi; }
    error_page 504 = @err;
    location = /mysql {
        #internal;
        drizzle_send_query_timeout 100ms;
        #drizzle_send_query_timeout 1s;
        drizzle_query $echo_request_body;
        drizzle_pass backend;

        no_error_pages;

        rds_json on;
        more_set_headers -s 504 "X-Mysql-Tid: $drizzle_thread_id";
    }

    location /lua {
        content_by_lua '
            local sql = "select sleep(3)"
            local res = ngx.location.capture("/mysql",
                { method = ngx.HTTP_POST, body = sql })

            ngx.say("status = " .. res.status)

            local tid = res.header["X-Mysql-Tid"]
            if tid == nil then
                ngx.say("thread id = nil")
                return
            end

            tid = tonumber(tid)
            ngx.say("thread id = " .. tid)

            res = ngx.location.capture("/mysql",
                { method = ngx.HTTP_POST,
                  body = "kill query " .. tid })

            ngx.say("kill status = " .. res.status)
            ngx.say("kill body = " .. res.body)
        ';
    }
--- request
    GET /lua
--- response_body_like
^status = 504
thread id = \d+
kill status = 200
kill body = \{"errcode":0\}$
--- SKIP