comparison js_fetch_timeout.t @ 1754:f1a097719877

Tests: added js fetch timeout directive tests.
author Dmitry Volyntsev <xeioex@nginx.com>
date Thu, 28 Apr 2022 16:27:38 -0700
parents
children
comparison
equal deleted inserted replaced
1753:0c50a00e6733 1754:f1a097719877
1 #!/usr/bin/perl
2
3 # (C) Dmitry Volyntsev
4 # (C) Nginx, Inc.
5
6 # Tests for http njs module, fetch method timeout.
7
8 ###############################################################################
9
10 use warnings;
11 use strict;
12
13 use Test::More;
14
15 use Socket qw/ CRLF /;
16
17 BEGIN { use FindBin; chdir($FindBin::Bin); }
18
19 use lib 'lib';
20 use Test::Nginx;
21
22 ###############################################################################
23
24 select STDERR; $| = 1;
25 select STDOUT; $| = 1;
26
27 my $t = Test::Nginx->new()->has(qw/http/)
28 ->write_file_expand('nginx.conf', <<'EOF');
29
30 %%TEST_GLOBALS%%
31
32 daemon off;
33
34 events {
35 }
36
37 http {
38 %%TEST_GLOBALS_HTTP%%
39
40 js_import test.js;
41
42 server {
43 listen 127.0.0.1:8080;
44 server_name localhost;
45
46 location /njs {
47 js_content test.njs;
48 }
49
50 location /normal_timeout {
51 js_content test.timeout_test;
52 }
53
54 location /short_timeout {
55 js_fetch_timeout 200ms;
56 js_content test.timeout_test;
57 }
58 }
59
60 server {
61 listen 127.0.0.1:8081;
62 server_name localhost;
63
64 location /normal_reply {
65 js_content test.normal_reply;
66 }
67
68 location /delayed_reply {
69 js_content test.delayed_reply;
70 }
71 }
72 }
73
74 EOF
75
76 my $p1 = port(8081);
77
78 $t->write_file('test.js', <<EOF);
79 function test_njs(r) {
80 r.return(200, njs.version);
81 }
82
83 async function timeout_test(r) {
84 let rs = await Promise.allSettled([
85 'http://127.0.0.1:$p1/normal_reply',
86 'http://127.0.0.1:$p1/delayed_reply',
87 ].map(v => ngx.fetch(v)));
88
89 let bs = rs.map(v => ({s: v.status, v: v.value ? v.value.headers.X
90 : v.reason}));
91
92 r.return(200, njs.dump(bs));
93 }
94
95 function normal_reply(r) {
96 r.headersOut.X = 'N';
97 r.return(200);
98 }
99
100 function delayed_reply(r) {
101 r.headersOut.X = 'D';
102 setTimeout((r) => { r.return(200); }, 250, r, 0);
103 }
104
105 export default {njs: test_njs, timeout_test, normal_reply, delayed_reply};
106 EOF
107
108 $t->try_run('no js_fetch_timeout')->plan(2);
109
110 ###############################################################################
111
112 like(http_get('/normal_timeout'),
113 qr/\[\{s:'fulfilled',v:'N'},\{s:'fulfilled',v:'D'}]$/s,
114 'normal timeout');
115 like(http_get('/short_timeout'),
116 qr/\[\{s:'fulfilled',v:'N'},\{s:'rejected',v:Error: read timed out}]$/s,
117 'short timeout');
118
119 ###############################################################################