From 92e8004705bdac2ccb75c8be7c72f072d755a268 Mon Sep 17 00:00:00 2001 From: Yossi Gottlieb Date: Sun, 11 Jul 2021 09:54:07 +0300 Subject: [PATCH] Pre-test bind-source-addr before running test. (#9214) This attempts to catch any non-standard configuration where the test may fail and produce a false positive. --- tests/unit/networking.tcl | 36 +++++++++++++++++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/tests/unit/networking.tcl b/tests/unit/networking.tcl index 81e7b10e9..49faa0548 100644 --- a/tests/unit/networking.tcl +++ b/tests/unit/networking.tcl @@ -37,8 +37,40 @@ test {CONFIG SET bind address} { } } {} {external:skip} +# Attempt to connect to host using a client bound to bindaddr, +# and return a non-zero value if successful within specified +# millisecond timeout, or zero otherwise. +proc test_loopback {host bindaddr timeout} { + if {[exec uname] != {Linux}} { + return 0 + } + + after $timeout set ::test_loopback_state timeout + if {[catch { + set server_sock [socket -server accept 0] + set port [lindex [fconfigure $server_sock -sockname] 2] } err]} { + return 0 + } + + proc accept {channel clientaddr clientport} { + set ::test_loopback_state "connected" + close $channel + } + + if {[catch {set client_sock [socket -async -myaddr $bindaddr $host $port]} err]} { + puts "test_loopback: Client connect failed: $err" + } else { + close $client_sock + } + + vwait ::test_loopback_state + close $server_sock + + return [expr {$::test_loopback_state == {connected}}] +} + test {CONFIG SET bind-source-addr} { - if {[exec uname] == {Linux}} { + if {[test_loopback 127.0.0.1 127.0.0.2 1000]} { start_server {} { start_server {} { set replica [srv 0 client] @@ -58,6 +90,8 @@ test {CONFIG SET bind-source-addr} { assert_match {*ip=127.0.0.2*} [s -1 slave0] } } + } else { + if {$::verbose} { puts "Skipping bind-source-addr test." } } } {} {external:skip}