summaryrefslogtreecommitdiff
path: root/ngx_stream_lua-0.0.16/t/131-duplex-req-socket.t
blob: ff09f3fb93805cab511daaaab7caa529dd34dec5 (plain)
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
131
132
133
134
135
136
137
138
# vim:set ft= ts=4 sw=4 et fdm=marker:

BEGIN {
    if (!defined $ENV{LD_PRELOAD}) {
        $ENV{LD_PRELOAD} = '';
    }

    if ($ENV{LD_PRELOAD} !~ /\bmockeagain\.so\b/) {
        $ENV{LD_PRELOAD} = "mockeagain.so $ENV{LD_PRELOAD}";
    }

    if ($ENV{MOCKEAGAIN} eq 'r') {
        $ENV{MOCKEAGAIN} = 'rw';

    } else {
        $ENV{MOCKEAGAIN} = 'w';
    }

    $ENV{TEST_NGINX_EVENT_TYPE} = 'poll';
    $ENV{MOCKEAGAIN_WRITE_TIMEOUT_PATTERN} = 'slow';
}

use Test::Nginx::Socket::Lua::Stream;
log_level('debug');

repeat_each(2);

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

run_tests();

__DATA__

=== TEST 1: raw downstream cosocket used in two different threads. See issue #481
--- stream_server_config
    lua_socket_read_timeout 1ms;
    lua_socket_send_timeout 1s;
    lua_socket_log_errors off;
    #lua_lingering_timeout 1ms;

    content_by_lua_block {
        local function reader(req_socket)
           -- First we receive in a blocking fashion so that ctx->downstream_co_ctx will be changed
           local data, err, partial = req_socket:receive(1)
           if err ~= "timeout" then
              ngx.log(ngx.ERR, "Did not get timeout in the receiving thread!")
              return
           end

           -- Now, sleep so that coctx->data is changed to sleep handler
           ngx.sleep(1)
        end

        local function writer(req_socket)
           -- send in a slow manner with a low timeout, so that the timeout handler will be
           local bytes, err = req_socket:send("slow!!!")
           if err ~= "timeout" then
              return error("Did not get timeout in the sending thread!")
           end
        end

        local req_socket, err = ngx.req.socket(true)
        if req_socket == nil then
           return error("Unable to get request socket:" .. (err or "nil"))
        end

        local writer_thread = ngx.thread.spawn(writer, req_socket)
        local reader_thread = ngx.thread.spawn(reader, req_socket)

        ngx.thread.wait(writer_thread)
        ngx.thread.wait(reader_thread)
        print("The two threads finished")
    }

--- no_error_log
[error]
--- error_log: The two threads finished
--- wait: 0.1
--- log_stream_response
--- stream_response_like chomp
^received \d+ bytes of response data\.$
--- timeout: 10



=== TEST 2: normal downstream cosocket used in two different threads. See issue #481
--- stream_server_config
    lua_socket_read_timeout 1ms;
    lua_socket_send_timeout 300ms;
    lua_socket_log_errors off;

    content_by_lua_block {
        local function reader(req_socket)
           -- First we receive in a blocking fashion so that ctx->downstream_co_ctx will be changed
           local data, err, partial = req_socket:receive(1)
           if err ~= "timeout" then
              ngx.log(ngx.ERR, "Did not get timeout in the receiving thread!")
              return
           end

           -- Now, sleep so that coctx->data is changed to sleep handler
           ngx.sleep(0.3)
        end

        local function writer(req_socket)
           -- send in a slow manner with a low timeout, so that the timeout handler will be
           print("sleep 0.3")
           ngx.sleep(0.1)
           print("say slow")
           ngx.say("slow!!!")
           print("flushing")
           local ok, err = ngx.flush(true)
           if not ok then
               print("flushing failed: ", err)
           end
        end

        local req_socket, err = ngx.req.socket()
        if req_socket == nil then
           return error("Unable to get request socket:" .. (err or "nil"))
        end

        local writer_thread = ngx.thread.spawn(writer, req_socket)
        local reader_thread = ngx.thread.spawn(reader, req_socket)

        ngx.thread.wait(writer_thread)
        ngx.thread.wait(reader_thread)
        print("The two threads finished")
    }

--- no_error_log
[error]
--- error_log: The two threads finished
--- wait: 0.1
--- log_stream_response
--- stream_response
received 4 bytes of response data.
--- timeout: 3