File: 156-slow-network.t

package info (click to toggle)
libnginx-mod-http-lua 1%3A0.10.28-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 6,456 kB
  • sloc: ansic: 42,658; perl: 480; sh: 303; python: 23; makefile: 11
file content (139 lines) | stat: -rw-r--r-- 3,174 bytes parent folder | download
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
139
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';
    delete($ENV{TEST_NGINX_USE_HTTP2});
}

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

repeat_each(2);

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

add_block_preprocessor(sub {
    my $block = shift;

    if (!defined $block->error_log) {
        $block->set_value("no_error_log", "[error]");
    }

    if (!defined $block->request) {
        $block->set_value("request", "GET /t");
    }

});


log_level("debug");
no_long_string();
#no_diff();
run_tests();

__DATA__

=== TEST 1: receiveany returns anything once socket receives
--- config
    server_tokens off;
    location = /t {
        set $port $TEST_NGINX_SERVER_PORT;
        content_by_lua_block {
            local sock = ngx.socket.tcp()
            sock:settimeout(500)
            assert(sock:connect("127.0.0.1", ngx.var.port))
            local req = {
                'GET /foo HTTP/1.0\r\n',
                'Host: localhost\r\n',
                'Connection: close\r\n\r\n',
            }
            local ok, err = sock:send(req)
            if not ok then
                ngx.say("send request failed: ", err)
                return
            end


            -- skip http header
            while true do
                local data, err, _ = sock:receive('*l')
                if err then
                    ngx.say('unexpected error occurs when receiving http head: ' .. err)
                    return
                end
                if #data == 0 then -- read last line of head
                    break
                end
            end

            -- receive http body
            while true do
                local data, err = sock:receiveany(1024)
                if err then
                    if err ~= 'closed' then
                        ngx.say('unexpected err: ', err)
                    end
                    break
                end
                ngx.say(data)
            end

            sock:close()
        }
    }

    location = /foo {
        content_by_lua_block {
            local resp = {
                '1',
                'hello',
            }

            local length = 0
            for _, v in ipairs(resp) do
                length = length + #v
            end

            -- flush http header
            ngx.header['Content-Length'] = length
            ngx.flush(true)
            ngx.sleep(0.01)

            -- send http body bytes by bytes
            for _, v in ipairs(resp) do
                ngx.print(v)
                ngx.flush(true)
                ngx.sleep(0.01)
            end
        }
    }

--- response_body
1
h
e
l
l
o
--- grep_error_log eval
qr/lua tcp socket read any/
--- grep_error_log_out
lua tcp socket read any
lua tcp socket read any
lua tcp socket read any
lua tcp socket read any
lua tcp socket read any
lua tcp socket read any
lua tcp socket read any