[PATCH] Tests: stream_udp_proxy_requests adjusted

Sergey Kandaurov pluknet at nginx.com
Wed Jul 17 13:37:30 UTC 2024


On Mon, Jul 15, 2024 at 03:01:15PM +0300, Oksana Deeva wrote:
> # HG changeset patch
> # User Oksana Deeva <o.deeva at wbsrv.ru>
> # Date 1721044584 -10800
> #      Mon Jul 15 14:56:24 2024 +0300
> # Node ID 50fc19b32628b1c5a2a49c127b013e15e4083dd6
> # Parent  0e9c1a8aa1d49e57b211e2c8ece94e00bf032ed7
> Tests: stream_udp_proxy_requests adjusted.
> 
> The test occasionally failed due to the fact that the order of packets
> could change. Now the order of packets will be ignored.
> 
> diff -r 0e9c1a8aa1d4 -r 50fc19b32628 stream_udp_proxy_requests.t
> --- a/stream_udp_proxy_requests.t	Fri Jul 12 01:19:15 2024 +0400
> +++ b/stream_udp_proxy_requests.t	Mon Jul 15 14:56:24 2024 +0300
> @@ -160,14 +160,23 @@
>  
>  $s = dgram('127.0.0.1:' . port(8985));
>  $s->write('1') for 1 .. 5;
> -$b = join ' ', map { $s->read() } (1 .. 10);
> +
> +my @parts = map { $s->read() } (1 .. 10);
> +
> +my $res = {};
> +for (my $i = 0; $i < scalar @parts; $i++) {
> +	my $part = $parts[$i];
>  
> -SKIP: {
> -skip 'session could early terminate', 1 unless $ENV{TEST_NGINX_UNSAFE};
> +	if ($i % 2 == 0) {
> +		$res->{$part} //= 0;
> +	} else {
> +		$res->{$parts[$i-1]} += $part;

Due to use of the relative order, this won't solve
a perfectly out-of-order sequence.

Rather, it may be fixed by introducing sequence numbers:

# HG changeset patch
# User Sergey Kandaurov <pluknet at nginx.com>
# Date 1721223063 -14400
#      Wed Jul 17 17:31:03 2024 +0400
# Node ID 64d10bdcce5e4a8b6294c26fdcf4ff9f9a42cac2
# Parent  b5ddbcf2fbb4d26712b5825ef660ebea30eacd8d
Tests: stream_udp_proxy_requests adjusted.

The test occasionally failed due to the fact that the order of packets
could change.  The fix is to introduce sequence numbers.

Inspired by Oksana Deeva.

diff --git a/stream_udp_proxy_requests.t b/stream_udp_proxy_requests.t
--- a/stream_udp_proxy_requests.t
+++ b/stream_udp_proxy_requests.t
@@ -76,7 +76,7 @@ stream {
 
     server {
         listen           127.0.0.1:%%PORT_8985_UDP%% udp;
-        proxy_pass       127.0.0.1:%%PORT_8990_UDP%%;
+        proxy_pass       127.0.0.1:%%PORT_8991_UDP%%;
 
         proxy_requests   2;
         proxy_responses  2;
@@ -150,24 +150,16 @@ is($s->read(), '1', 'requests unset foll
 # expects all packets proxied from backend, the last (uneven) session succeed
 
 $s = dgram('127.0.0.1:' . port(8984));
-$s->write('2') for 1 .. 5;
-my $b = join ' ', map { $s->read() } (1 .. 15);
-like($b, qr/^(\d+ 1 2) \1 (?!\1)(\d+ 1 2) \2 (?!\2)\d+ 1 2$/, 'slow backend');
+like(many($s, '2', 5, 15),
+	qr/^(\d+ 1 2) \1 (?!\1)(\d+ 1 2) \2 (?!\2)\d+ 1 2$/, 'slow backend');
 
 # proxy_requests 2, proxy_responses 2
 # client sends 5 packets, each responded with 2 packets
 # expects all packets proxied from backend, the last (uneven) session succeed
 
 $s = dgram('127.0.0.1:' . port(8985));
-$s->write('1') for 1 .. 5;
-$b = join ' ', map { $s->read() } (1 .. 10);
-
-SKIP: {
-skip 'session could early terminate', 1 unless $ENV{TEST_NGINX_UNSAFE};
-
-like($b, qr/^(\d+ 1) \1 (?!\1)(\d+ 1) \2 (?!\2)\d+ 1$/, 'requests - responses');
-
-}
+like(many($s, '1', 5, 10),
+	qr/^(\d+ 1) \1 (?!\1)(\d+ 1) \2 (?!\2)\d+ 1$/, 'requests - responses');
 
 $t->stop();
 
@@ -185,6 +177,17 @@ EOF
 
 ###############################################################################
 
+sub many {
+	my ($s, $buf, $wcount, $rcount) = @_;
+
+	$s->write($buf) for 1 .. $wcount;
+	join ' ', map { $_->[1] }
+		sort { $a->[0] <=> $b->[0] }
+		map { [ $s->read() =~ /^(\d+) (.+)/ ] } 1 .. $rcount;
+}
+
+###############################################################################
+
 sub udp_daemon {
 	my ($t, $port) = @_;
 
@@ -200,14 +203,14 @@ sub udp_daemon {
 	open my $fh, '>', $t->testdir() . "/$port";
 	close $fh;
 
-	my $slp = 1 if $port == port(8991);
+	my ($slp, $i) = (1, 1) if $port == port(8991);
 
 	while (1) {
 		$server->recv(my $buffer, 65536);
 		sleep 1, $slp = 0 if $slp;
 
-		$server->send($server->peerport());
-		$server->send($_) for (1 .. $buffer);
+		$server->send(($i ? "${\($i++)} " : "") . $server->peerport());
+		$server->send(($i ? "${\($i++)} " : "") . $_) for (1..$buffer);
 	}
 }
 

> +	}
> +}
>  
> -like($b, qr/^(\d+ 1) \1 (?!\1)(\d+ 1) \2 (?!\2)\d+ 1$/, 'requests - responses');
> +$b = join ' ', sort values %$res;
>  
> -}
> +is($b, '1 2 2', 'requests - responses');
>  
>  $t->stop();
>  


More information about the nginx-devel mailing list