Previously, missing engine IDs were treated as evidence of a non-debug
build. An endpoint failure could therefore skip a debug test, while
merge tests could pass after ignoring invalid responses from some
locations or servers. The response-based guards were introduced in
c4389399.
Check --with-debug independently before starting nginx. Require a
successful HTTP response and a complete numeric engine ID from every
endpoint, or a complete numeric stream response, before comparing
contexts. Let startup and response failures fail the tests instead
of reporting unsupported functionality.
select STDERR; $| = 1;
select STDOUT; $| = 1;
-my $t = Test::Nginx->new()->has(qw/http proxy rewrite/)
- ->write_file_expand('nginx.conf', <<'EOF');
+my $t = Test::Nginx->new()->has(qw/http proxy rewrite/);
+
+plan(skip_all => 'ngx.engine_id requires --with-debug')
+ unless $t->has_module('--with-debug');
+
+$t->write_file_expand('nginx.conf', <<'EOF');
%%TEST_GLOBALS%%
EOF
-$t->try_run('no njs available');
+$t->run()->plan(5);
###############################################################################
-my ($mainid) = http_get('/test_foo') =~ /MAIN-TEST:(\d+)/s;
-
-plan(skip_all => 'ngx.engine_id requires --with-debug')
- unless defined $mainid;
-
-$t->plan(5);
-
-my ($barid) = http_get('/test_bar') =~ /MAIN-TEST:(\d+)/s;
+my $mainid = engine_id('/test_foo', 'MAIN-TEST');
+my $barid = engine_id('/test_bar', 'MAIN-TEST');
ok($barid == $mainid, 'same context for main.js');
-my ($libid) = http_get('/test_lib') =~ /LIB-TEST:(\d+)/s;
+my $libid = engine_id('/test_lib', 'LIB-TEST');
ok($libid != $mainid, 'different context for lib.js');
-my ($funid) = http_get('/test_fun') =~ /FUN-TEST:(\d+)/s;
+my $funid = engine_id('/test_fun', 'FUN-TEST');
ok($funid != $mainid && $funid != $libid,
'different context for fun.js');
-my ($pfunid) = http_get('/proxy/test_fun') =~ /FUN-TEST:(\d+)/s;
+my $pfunid = engine_id('/proxy/test_fun', 'FUN-TEST');
ok($pfunid != $funid && $pfunid != $mainid && $pfunid != $libid,
'different context for fun.js in proxy');
-my ($varid) = http_get('/test_var') =~ /P-TEST:(\d+)/s;
+my $varid = engine_id('/test_var', 'P-TEST');
ok($varid == $mainid, 'variable from main.js');
###############################################################################
+
+sub engine_id {
+ my ($uri, $prefix) = @_;
+
+ my $response = http_get($uri);
+ my ($headers, $body) = split(/\x0d\x0a\x0d\x0a/, $response // '', 2);
+ my ($id) = ($body // '') =~ /\A\Q$prefix\E:(\d+)\z/;
+
+ die "invalid engine ID response from $uri\n"
+ unless defined $id && $headers =~ /\AHTTP\/1\.[01] 200 /;
+
+ return $id;
+}
select STDERR; $| = 1;
select STDOUT; $| = 1;
-my $t = Test::Nginx->new()->has(qw/http/)
- ->write_file_expand('nginx.conf', <<'EOF');
+my $t = Test::Nginx->new()->has(qw/http/);
+
+plan(skip_all => 'ngx.engine_id requires --with-debug')
+ unless $t->has_module('--with-debug');
+
+$t->write_file_expand('nginx.conf', <<'EOF');
%%TEST_GLOBALS%%
EOF
-$t->try_run('no njs available');
+$t->run()->plan(1);
###############################################################################
my %ids;
for my $uri ('/a', '/b', '/c', '/d') {
- http_get($uri) =~ /\x0d\x0a\x0d\x0a(\d+)/ms;
- $ids{$1} = 1 if defined $1;
-}
+ my $response = http_get($uri);
+ my ($headers, $id) = split(/\x0d\x0a\x0d\x0a/, $response // '', 2);
-plan(skip_all => 'ngx.engine_id requires --with-debug')
- unless scalar keys %ids;
+ die "invalid engine ID response from $uri\n"
+ unless defined $id && $headers =~ /\AHTTP\/1\.[01] 200 /
+ && $id =~ /\A\d+\z/;
-$t->plan(1);
+ $ids{$id} = 1;
+}
is(scalar keys %ids, 1, 'http js block imported once');
select STDERR; $| = 1;
select STDOUT; $| = 1;
-my $t = Test::Nginx->new()->has(qw/http/)
- ->write_file_expand('nginx.conf', <<'EOF');
+my $t = Test::Nginx->new()->has(qw/http/);
+
+plan(skip_all => 'ngx.engine_id requires --with-debug')
+ unless $t->has_module('--with-debug');
+
+$t->write_file_expand('nginx.conf', <<'EOF');
%%TEST_GLOBALS%%
EOF
-$t->try_run('no njs available');
+$t->run()->plan(1);
###############################################################################
my %ids;
for my $port (port(8080), port(8081), port(8082), port(8083)) {
- http("GET / HTTP/1.0\nHost: localhost\n\n",
- PeerAddr => "127.0.0.1:$port")
- =~ /\x0d\x0a\x0d\x0a(\d+)/ms;
- $ids{$1} = 1 if defined $1;
-}
+ my $response = http("GET / HTTP/1.0\nHost: localhost\n\n",
+ PeerAddr => "127.0.0.1:$port");
+ my ($headers, $id) = split(/\x0d\x0a\x0d\x0a/, $response // '', 2);
-plan(skip_all => 'ngx.engine_id requires --with-debug')
- unless scalar keys %ids;
+ die "invalid engine ID response from port $port\n"
+ unless defined $id && $headers =~ /\AHTTP\/1\.[01] 200 /
+ && $id =~ /\A\d+\z/;
-$t->plan(1);
+ $ids{$id} = 1;
+}
is(scalar keys %ids, 1, 'http js block imported once');
select STDERR; $| = 1;
select STDOUT; $| = 1;
-my $t = Test::Nginx->new()->has(qw/stream stream_return/)
- ->write_file_expand('nginx.conf', <<'EOF');
+my $t = Test::Nginx->new()->has(qw/stream stream_return/);
+
+plan(skip_all => 'ngx.engine_id requires --with-debug')
+ unless $t->has_module('--with-debug');
+
+$t->write_file_expand('nginx.conf', <<'EOF');
%%TEST_GLOBALS%%
EOF
-$t->try_run('no njs available');
+$t->run()->plan(3);
###############################################################################
-my ($id_82) = stream('127.0.0.1:' . port(8082))->read() =~ /(\d+)/;
-plan(skip_all => 'ngx.engine_id requires --with-debug')
- unless defined $id_82;
-
-$t->plan(3);
+my $id_82 = engine_id(8082);
is(stream('127.0.0.1:' . port(8081))->read(), 'P-TEST', 'foo.bar.p');
-my ($id_83) = stream('127.0.0.1:' . port(8083))->read() =~ /(\d+)/;
-my ($id_84) = stream('127.0.0.1:' . port(8084))->read() =~ /(\d+)/;
+my $id_83 = engine_id(8083);
+my $id_84 = engine_id(8084);
ok($id_82 == $id_83, 'same context for stream level import');
ok($id_84 != $id_82, 'different context for server level import');
###############################################################################
+
+sub engine_id {
+ my ($port) = @_;
+
+ my $id = stream('127.0.0.1:' . port($port))->read();
+
+ die "invalid engine ID response from port $port\n"
+ unless defined $id && $id =~ /\A\d+\z/;
+
+ return $id;
+}