|
| 1 | +#!/usr/bin/perl |
| 2 | + |
| 3 | +# (C) Test for infinite loop protection in microtask queue processing |
| 4 | + |
| 5 | +# Tests for proper handling of infinite microtask loops similar to Node.js protection. |
| 6 | + |
| 7 | +############################################################################### |
| 8 | + |
| 9 | +use warnings; |
| 10 | +use strict; |
| 11 | + |
| 12 | +use Test::More; |
| 13 | + |
| 14 | +BEGIN { use FindBin; chdir($FindBin::Bin); } |
| 15 | + |
| 16 | +use lib 'lib'; |
| 17 | +use Test::Nginx; |
| 18 | + |
| 19 | +############################################################################### |
| 20 | + |
| 21 | +select STDERR; $| = 1; |
| 22 | +select STDOUT; $| = 1; |
| 23 | + |
| 24 | +my $t = Test::Nginx->new()->has(qw/http rewrite/) |
| 25 | + ->write_file_expand('nginx.conf', <<'EOF'); |
| 26 | +
|
| 27 | +%%TEST_GLOBALS%% |
| 28 | +
|
| 29 | +daemon off; |
| 30 | +
|
| 31 | +events { |
| 32 | +} |
| 33 | +
|
| 34 | +http { |
| 35 | + %%TEST_GLOBALS_HTTP%% |
| 36 | +
|
| 37 | + js_import test.js; |
| 38 | +
|
| 39 | + server { |
| 40 | + listen 127.0.0.1:8080; |
| 41 | + server_name localhost; |
| 42 | +
|
| 43 | + location /njs { |
| 44 | + js_content test.njs; |
| 45 | + } |
| 46 | +
|
| 47 | + location /infinite_microtasks { |
| 48 | + js_content test.infinite_microtasks; |
| 49 | + } |
| 50 | +
|
| 51 | + location /recursive_promises { |
| 52 | + js_content test.recursive_promises; |
| 53 | + } |
| 54 | +
|
| 55 | + location /normal_promise_chain { |
| 56 | + js_content test.normal_promise_chain; |
| 57 | + } |
| 58 | +
|
| 59 | + } |
| 60 | +} |
| 61 | +
|
| 62 | +EOF |
| 63 | + |
| 64 | +$t->write_file('test.js', <<'EOF'); |
| 65 | + function test_njs(r) { |
| 66 | + r.return(200, njs.version); |
| 67 | + } |
| 68 | +
|
| 69 | + function infinite_microtasks(r) { |
| 70 | + // Create an infinite microtask loop - should trigger protection limit |
| 71 | + function infiniteLoop() { |
| 72 | + return Promise.resolve().then(() => { |
| 73 | + // Recursively create more microtasks |
| 74 | + return infiniteLoop(); |
| 75 | + }); |
| 76 | + } |
| 77 | +
|
| 78 | + return infiniteLoop(); |
| 79 | + } |
| 80 | +
|
| 81 | + function recursive_promises(r) { |
| 82 | + // Another variation of infinite microtask generation |
| 83 | + let count = 0; |
| 84 | + function createPromise() { |
| 85 | + return new Promise((resolve) => { |
| 86 | + resolve(); |
| 87 | + }).then(() => { |
| 88 | + count++; |
| 89 | + // Keep creating more promises indefinitely |
| 90 | + return createPromise(); |
| 91 | + }); |
| 92 | + } |
| 93 | +
|
| 94 | + return createPromise(); |
| 95 | + } |
| 96 | +
|
| 97 | + function normal_promise_chain(r) { |
| 98 | + // Normal promise chain that should complete without hitting the limit |
| 99 | + let result = Promise.resolve(1); |
| 100 | +
|
| 101 | + // Create a reasonable chain of 50 promises (well under the NGX_MAX_JOB_ITERATIONS limit) |
| 102 | + for (let i = 0; i < 50; i++) { |
| 103 | + result = result.then((value) => value + 1); |
| 104 | + } |
| 105 | +
|
| 106 | + return result.then((finalValue) => { |
| 107 | + r.return(200, "completed with value: " + finalValue); |
| 108 | + }); |
| 109 | + } |
| 110 | +
|
| 111 | + export default {njs: test_njs, infinite_microtasks, recursive_promises, normal_promise_chain}; |
| 112 | +
|
| 113 | +EOF |
| 114 | + |
| 115 | +$t->try_run('no njs available')->plan(5); |
| 116 | + |
| 117 | +############################################################################### |
| 118 | + |
| 119 | +# Test basic functionality |
| 120 | +like(http_get('/njs'), qr/\d+\.\d+\.\d+/, 'njs version'); |
| 121 | + |
| 122 | +# Test normal promise chain (should work - under the limit) |
| 123 | +like(http_get('/normal_promise_chain'), qr/completed with value: 51/, 'normal promise chain completes'); |
| 124 | + |
| 125 | +# Test infinite microtasks scenario - should trigger protection limit |
| 126 | +my $infinite_response = http_get('/infinite_microtasks'); |
| 127 | +like($infinite_response, qr/HTTP\/1\.[01] 500|Internal Server Error/, 'infinite microtasks causes error'); |
| 128 | + |
| 129 | +# Test recursive promises scenario - should also trigger protection limit |
| 130 | +my $recursive_response = http_get('/recursive_promises'); |
| 131 | +like($recursive_response, qr/HTTP\/1\.[01] 500|Internal Server Error/, 'recursive promises causes error'); |
| 132 | + |
| 133 | +$t->stop(); |
| 134 | + |
| 135 | +# Check error log for the specific infinite loop protection messages |
| 136 | +my $error_log = $t->read_file('error.log'); |
| 137 | + |
| 138 | +# Should have error messages for job queue limit exceeded |
| 139 | +ok(index($error_log, 'js job queue processing exceeded') > 0, |
| 140 | + 'job queue limit exceeded message logged'); |
| 141 | + |
| 142 | +############################################################################### |
0 commit comments