comparison js_async.t @ 1304:25de201c8a0d

Tests: added njs async tests.
author Dmitry Volyntsev <xeioex@nginx.com>
date Wed, 21 Mar 2018 17:30:44 +0300
parents
children f168fc46c7a4
comparison
equal deleted inserted replaced
1303:42577a840a7d 1304:25de201c8a0d
1 #!/usr/bin/perl
2
3 # (C) Dmitry Volyntsev
4 # (C) Nginx, Inc.
5
6 # Async tests for http JavaScript module.
7
8 ###############################################################################
9
10 use warnings;
11 use strict;
12
13 use Test::More;
14
15 BEGIN { use FindBin; chdir($FindBin::Bin); }
16
17 use lib 'lib';
18 use Test::Nginx;
19
20 ###############################################################################
21
22 select STDERR; $| = 1;
23 select STDOUT; $| = 1;
24
25 my $t = Test::Nginx->new()->has(qw/http rewrite/)
26 ->write_file_expand('nginx.conf', <<'EOF');
27
28 %%TEST_GLOBALS%%
29
30 daemon off;
31
32 events {
33 }
34
35 http {
36 %%TEST_GLOBALS_HTTP%%
37
38 js_set $test_async set_timeout;
39 js_set $context_var context_var;
40
41 js_include test.js;
42
43 server {
44 listen 127.0.0.1:8080;
45 server_name localhost;
46
47 location /async_var {
48 return 200 $test_async;
49 }
50
51 location /shared_ctx {
52 add_header H $context_var;
53 js_content shared_ctx;
54 }
55
56 location /set_timeout {
57 js_content set_timeout;
58 }
59
60 location /set_timeout_many {
61 js_content set_timeout_many;
62 }
63
64 location /set_timeout_data {
65 postpone_output 0;
66 js_content set_timeout_data;
67 }
68
69 location /limit_rate {
70 postpone_output 0;
71 sendfile_max_chunk 5;
72 js_content limit_rate;
73 }
74 }
75 }
76
77 EOF
78
79 $t->write_file('test.js', <<EOF);
80 function set_timeout(req, res) {
81 var timerId = setTimeout(timeout_cb_r, 5, req, res, 0);
82 clearTimeout(timerId);
83 setTimeout(timeout_cb_r, 5, req, res, 0)
84 }
85
86 function set_timeout_data(req, res) {
87 setTimeout(timeout_cb_data, 5, req, res, 0);
88 }
89
90 function set_timeout_many(req, res) {
91 for (var i = 0; i < 5; i++) {
92 setTimeout(timeout_cb_empty, 5, req, i);
93 }
94
95 setTimeout(timeout_cb_reply, 10, res);
96 }
97
98 function timeout_cb_r(req, res, cnt) {
99 if (cnt == 10) {
100 res.status = 200;
101 res.contentType = 'foo';
102 res.sendHeader();
103 res.finish();
104
105 } else {
106 setTimeout(timeout_cb_r, 5, req, res, ++cnt);
107 }
108 }
109
110 function timeout_cb_empty(req, arg) {
111 req.log("timeout_cb_empty" + arg);
112 }
113
114 function timeout_cb_reply(res) {
115 res.status = 200;
116 res.contentType = 'reply';
117 res.sendHeader();
118 res.finish();
119 }
120
121 function timeout_cb_data(req, res, counter) {
122 if (counter == 0) {
123 req.log("timeout_cb_data: init");
124 res.status = 200;
125 res.sendHeader();
126 setTimeout(timeout_cb_data, 5, req, res, ++counter);
127
128 } else if (counter == 10) {
129 req.log("timeout_cb_data: finish");
130 res.finish();
131
132 } else {
133 res.send("" + counter);
134 setTimeout(timeout_cb_data, 5, req, res, ++counter);
135 }
136 }
137
138 var js_;
139 function context_var() {
140 return js_;
141 }
142
143 function shared_ctx(req, res) {
144 js_ = req.variables.arg_a;
145
146 res.status = 200;
147 res.sendHeader();
148 res.finish();
149 }
150
151 function limit_rate_cb(res) {
152 res.finish();
153 }
154
155 function limit_rate(req, res) {
156 res.status = 200;
157 res.sendHeader();
158 res.send("AAAAA".repeat(10))
159 setTimeout(limit_rate_cb, 1000, res);
160 }
161
162 EOF
163
164 $t->try_run('no njs available')->plan(7);
165
166 ###############################################################################
167
168 like(http_get('/set_timeout'), qr/Content-Type: foo/, 'setTimeout');
169 like(http_get('/set_timeout_many'), qr/Content-Type: reply/, 'setTimeout many');
170 like(http_get('/set_timeout_data'), qr/123456789/, 'setTimeout data');
171 like(http_get('/shared_ctx?a=xxx'), qr/H: xxx/, 'shared context');
172 like(http_get('/limit_rate'), qr/A{50}/, 'limit_rate');
173
174 http_get('/async_var');
175
176 $t->stop();
177
178 ok(index($t->read_file('error.log'), 'pending events') > 0,
179 'pending js events');
180 ok(index($t->read_file('error.log'), 'async operation inside') > 0,
181 'async op in var handler');
182
183 ###############################################################################