Skip to content

Commit 8963420

Browse files
committed
Tests: splitting js_periodic tests into multiple files.
1 parent 1b45195 commit 8963420

6 files changed

+580
-189
lines changed

nginx/t/js_periodic.t

+3-89
Original file line numberDiff line numberDiff line change
@@ -59,46 +59,18 @@ http {
5959
js_periodic test.tick interval=30ms jitter=1ms;
6060
js_periodic test.timer interval=1s worker_affinity=all;
6161
js_periodic test.overrun interval=30ms;
62-
js_periodic test.file interval=1s;
63-
js_periodic test.fetch interval=40ms;
64-
js_periodic test.multiple_fetches interval=1s;
6562
js_periodic test.affinity interval=50ms worker_affinity=0101;
6663
js_periodic test.vars interval=10s;
6764
68-
js_periodic test.fetch_exception interval=1s;
6965
js_periodic test.tick_exception interval=1s;
7066
js_periodic test.timer_exception interval=1s;
7167
js_periodic test.timeout_exception interval=30ms;
7268
}
7369
74-
location /engine {
75-
js_content test.engine;
76-
}
77-
78-
location /fetch_ok {
79-
return 200 'ok';
80-
}
81-
82-
location /fetch_foo {
83-
return 200 'foo';
84-
}
85-
8670
location /test_affinity {
8771
js_content test.test_affinity;
8872
}
8973
90-
location /test_fetch {
91-
js_content test.test_fetch;
92-
}
93-
94-
location /test_file {
95-
js_content test.test_file;
96-
}
97-
98-
location /test_multiple_fetches {
99-
js_content test.test_multiple_fetches;
100-
}
101-
10274
location /test_tick {
10375
js_content test.test_tick;
10476
}
@@ -119,51 +91,15 @@ http {
11991
12092
EOF
12193

122-
my $p0 = port(8080);
123-
12494
$t->write_file('test.js', <<EOF);
125-
import fs from 'fs';
126-
127-
function engine(r) {
128-
r.return(200, njs.engine);
129-
}
130-
13195
function affinity() {
13296
ngx.shared.workers.set(ngx.worker_id, 1);
13397
}
13498
135-
async function fetch() {
136-
let reply = await ngx.fetch('http://127.0.0.1:$p0/fetch_ok');
137-
let body = await reply.text();
138-
139-
let v = ngx.shared.strings.get('fetch') || '';
140-
ngx.shared.strings.set('fetch', v + body);
141-
}
142-
14399
function js_set() {
144100
return 'JS-SET';
145101
}
146102
147-
async function multiple_fetches() {
148-
let reply = await ngx.fetch('http://127.0.0.1:$p0/fetch_ok');
149-
let reply2 = await ngx.fetch('http://127.0.0.1:$p0/fetch_foo');
150-
let body = await reply.text();
151-
let body2 = await reply2.text();
152-
153-
ngx.shared.strings.set('multiple_fetches', body + '\@' + body2);
154-
}
155-
156-
async function fetch_exception() {
157-
let reply = await ngx.fetch('garbage');
158-
}
159-
160-
async function file() {
161-
let fh = await fs.promises.open(ngx.conf_prefix + 'file', 'a+');
162-
163-
await fh.write('abc');
164-
await fh.close();
165-
}
166-
167103
async function overrun() {
168104
setTimeout(() => {}, 100000);
169105
}
@@ -214,20 +150,6 @@ $t->write_file('test.js', <<EOF);
214150
r.return(200, `[\${ngx.shared.workers.keys().toSorted()}]`);
215151
}
216152
217-
function test_fetch(r) {
218-
r.return(200, ngx.shared.strings.get('fetch').startsWith('okok'));
219-
}
220-
221-
function test_file(r) {
222-
r.return(200,
223-
fs.readFileSync(ngx.conf_prefix + 'file').toString() == 'abc');
224-
}
225-
226-
function test_multiple_fetches(r) {
227-
r.return(200, ngx.shared.strings.get('multiple_fetches')
228-
.startsWith('ok\@foo'));
229-
}
230-
231153
function test_tick(r) {
232154
r.return(200, ngx.shared.nums.get('tick') >= 3);
233155
}
@@ -244,19 +166,14 @@ $t->write_file('test.js', <<EOF);
244166
r.return(200, ngx.shared.strings.get('vars'));
245167
}
246168
247-
export default { affinity, fetch, fetch_exception, file, js_set,
248-
multiple_fetches, overrun, vars, test_affinity, test_fetch,
249-
test_file, test_multiple_fetches, test_tick,
169+
export default { affinity, js_set, overrun, vars, test_affinity, test_tick,
250170
test_timeout_exception, test_timer, test_vars, tick,
251-
tick_exception, timer, timer_exception,
252-
timeout_exception, engine };
171+
tick_exception, timer, timer_exception, timeout_exception };
253172
EOF
254173

255174
$t->try_run('no js_periodic');
256175

257-
plan(skip_all => 'not yet') if http_get('/engine') =~ /QuickJS$/m;
258-
259-
$t->plan(9);
176+
$t->plan(6);
260177

261178
###############################################################################
262179

@@ -265,9 +182,6 @@ select undef, undef, undef, 0.1;
265182
like(http_get('/test_affinity'), qr/\[1,3]/, 'affinity test');
266183
like(http_get('/test_tick'), qr/true/, '3x tick test');
267184
like(http_get('/test_timer'), qr/true/, 'timer test');
268-
like(http_get('/test_file'), qr/true/, 'file test');
269-
like(http_get('/test_fetch'), qr/true/, 'periodic fetch test');
270-
like(http_get('/test_multiple_fetches'), qr/true/, 'multiple fetch test');
271185

272186
like(http_get('/test_timeout_exception'), qr/true/, 'timeout exception test');
273187
like(http_get('/test_vars'), qr/JS-VAR\|JS-SET\|MAP-VAR/, 'vars test');

nginx/t/js_periodic_fetch.t

+138
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
#!/usr/bin/perl
2+
3+
# (C) Dmitry Volyntsev
4+
# (C) Nginx, Inc.
5+
6+
# Tests for js_periodic directive.
7+
8+
###############################################################################
9+
10+
use warnings;
11+
use strict;
12+
13+
use Test::More;
14+
use Socket qw/ CRLF /;
15+
16+
BEGIN { use FindBin; chdir($FindBin::Bin); }
17+
18+
use lib 'lib';
19+
use Test::Nginx;
20+
21+
###############################################################################
22+
23+
select STDERR; $| = 1;
24+
select STDOUT; $| = 1;
25+
26+
my $t = Test::Nginx->new()->has(qw/http rewrite/)
27+
->write_file_expand('nginx.conf', <<'EOF');
28+
29+
%%TEST_GLOBALS%%
30+
31+
daemon off;
32+
worker_processes 4;
33+
34+
events {
35+
}
36+
37+
worker_shutdown_timeout 100ms;
38+
39+
http {
40+
%%TEST_GLOBALS_HTTP%%
41+
42+
js_import test.js;
43+
44+
js_shared_dict_zone zone=strings:32k;
45+
46+
server {
47+
listen 127.0.0.1:8080;
48+
server_name localhost;
49+
50+
location @periodic {
51+
js_periodic test.fetch interval=40ms;
52+
js_periodic test.multiple_fetches interval=1s;
53+
54+
js_periodic test.fetch_exception interval=1s;
55+
}
56+
57+
location /engine {
58+
js_content test.engine;
59+
}
60+
61+
location /fetch_ok {
62+
return 200 'ok';
63+
}
64+
65+
location /fetch_foo {
66+
return 200 'foo';
67+
}
68+
69+
location /test_fetch {
70+
js_content test.test_fetch;
71+
}
72+
73+
location /test_multiple_fetches {
74+
js_content test.test_multiple_fetches;
75+
}
76+
}
77+
}
78+
79+
EOF
80+
81+
my $p0 = port(8080);
82+
83+
$t->write_file('test.js', <<EOF);
84+
function engine(r) {
85+
r.return(200, njs.engine);
86+
}
87+
88+
async function fetch() {
89+
let reply = await ngx.fetch('http://127.0.0.1:$p0/fetch_ok');
90+
let body = await reply.text();
91+
92+
let v = ngx.shared.strings.get('fetch') || '';
93+
ngx.shared.strings.set('fetch', v + body);
94+
}
95+
96+
async function multiple_fetches() {
97+
let reply = await ngx.fetch('http://127.0.0.1:$p0/fetch_ok');
98+
let reply2 = await ngx.fetch('http://127.0.0.1:$p0/fetch_foo');
99+
let body = await reply.text();
100+
let body2 = await reply2.text();
101+
102+
ngx.shared.strings.set('multiple_fetches', body + '\@' + body2);
103+
}
104+
105+
async function fetch_exception() {
106+
let reply = await ngx.fetch('garbage');
107+
}
108+
109+
function test_fetch(r) {
110+
r.return(200, ngx.shared.strings.get('fetch').startsWith('okok'));
111+
}
112+
113+
function test_multiple_fetches(r) {
114+
r.return(200, ngx.shared.strings.get('multiple_fetches')
115+
.startsWith('ok\@foo'));
116+
}
117+
118+
export default { fetch, fetch_exception, multiple_fetches, test_fetch,
119+
test_multiple_fetches, engine };
120+
EOF
121+
122+
$t->try_run('no js_periodic with fetch');
123+
124+
plan(skip_all => 'not yet') if http_get('/engine') =~ /QuickJS$/m;
125+
126+
$t->plan(3);
127+
128+
###############################################################################
129+
130+
select undef, undef, undef, 0.1;
131+
132+
like(http_get('/test_fetch'), qr/true/, 'periodic fetch test');
133+
like(http_get('/test_multiple_fetches'), qr/true/, 'multiple fetch test');
134+
135+
$t->stop();
136+
137+
unlike($t->read_file('error.log'), qr/\[error\].*should not be seen/,
138+
'check for not discadred events');

nginx/t/js_periodic_file.t

+91
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
#!/usr/bin/perl
2+
3+
# (C) Dmitry Volyntsev
4+
# (C) Nginx, Inc.
5+
6+
# Tests for js_periodic directive.
7+
8+
###############################################################################
9+
10+
use warnings;
11+
use strict;
12+
13+
use Test::More;
14+
use Socket qw/ CRLF /;
15+
16+
BEGIN { use FindBin; chdir($FindBin::Bin); }
17+
18+
use lib 'lib';
19+
use Test::Nginx;
20+
21+
###############################################################################
22+
23+
select STDERR; $| = 1;
24+
select STDOUT; $| = 1;
25+
26+
my $t = Test::Nginx->new()->has(qw/http rewrite/)
27+
->write_file_expand('nginx.conf', <<'EOF');
28+
29+
%%TEST_GLOBALS%%
30+
31+
daemon off;
32+
worker_processes 4;
33+
34+
events {
35+
}
36+
37+
worker_shutdown_timeout 100ms;
38+
39+
http {
40+
%%TEST_GLOBALS_HTTP%%
41+
42+
js_import test.js;
43+
44+
server {
45+
listen 127.0.0.1:8080;
46+
server_name localhost;
47+
48+
location @periodic {
49+
js_periodic test.file interval=1s;
50+
}
51+
52+
location /test_file {
53+
js_content test.test_file;
54+
}
55+
}
56+
}
57+
58+
EOF
59+
60+
$t->write_file('test.js', <<EOF);
61+
import fs from 'fs';
62+
63+
async function file() {
64+
let fh = await fs.promises.open(ngx.conf_prefix + 'file', 'a+');
65+
66+
await fh.write('abc');
67+
await fh.close();
68+
}
69+
70+
function test_file(r) {
71+
r.return(200,
72+
fs.readFileSync(ngx.conf_prefix + 'file').toString() == 'abc');
73+
}
74+
75+
export default { file, test_file };
76+
EOF
77+
78+
$t->try_run('no js_periodic with fs support');
79+
80+
$t->plan(2);
81+
82+
###############################################################################
83+
84+
select undef, undef, undef, 0.1;
85+
86+
like(http_get('/test_file'), qr/true/, 'file test');
87+
88+
$t->stop();
89+
90+
unlike($t->read_file('error.log'), qr/\[error\].*should not be seen/,
91+
'check for not discadred events');

0 commit comments

Comments
 (0)