From 948bd5e9965da403d358767ce982c65b3af971ae Mon Sep 17 00:00:00 2001
From: "jan.nijtmans" <nijtmans@users.sourceforge.net>
Date: Wed, 12 Apr 2017 09:23:23 +0000
Subject: Fix sporadically errors in zlib-8.x and socket tests, cherry-picked
 from "fix-1997007" branch. Credit to "sebres"!

---
 tests/socket.test | 17 ++++++++++++++++-
 tests/zlib.test   | 14 +++++++-------
 2 files changed, 23 insertions(+), 8 deletions(-)

diff --git a/tests/socket.test b/tests/socket.test
index d43c41c..a3e5704 100644
--- a/tests/socket.test
+++ b/tests/socket.test
@@ -69,7 +69,22 @@ testConstraint exec [llength [info commands exec]]
 
 # Produce a random port number in the Dynamic/Private range
 # from 49152 through 65535.
-proc randport {} { expr {int(rand()*16383+49152)} }
+proc randport {} {
+    # firstly try dynamic port via server-socket(0):
+    set port 0x7fffffff
+    catch {
+	set port [lindex [fconfigure [set s [socket -server {} 0]] -sockname] 2]
+	close $s
+    }
+    while {[catch {
+	close [socket -server {} $port]
+    } msg]} {
+	if {[incr i] > 1000} {return -code error "too many iterations to get free random port: $msg"}
+	# try random port:
+	set port [expr {int(rand()*16383+49152)}]
+    }
+    return $port
+}
 
 # Test the latency of tcp connections over the loopback interface. Some OSes
 # (e.g. NetBSD) seem to use the Nagle algorithm and delayed ACKs, so it takes
diff --git a/tests/zlib.test b/tests/zlib.test
index 63bac7e..9f06eb1 100644
--- a/tests/zlib.test
+++ b/tests/zlib.test
@@ -330,7 +330,7 @@ test zlib-8.9 {transformation and fconfigure} -setup {
     set strm [zlib stream decompress]
 } -constraints zlib -body {
     zlib push compress $outSide -dictionary $spdyDict
-    fconfigure $outSide -blocking 0 -translation binary -buffering none
+    fconfigure $outSide -blocking 1 -translation binary -buffering none
     fconfigure $inSide -blocking 1 -translation binary
     puts -nonewline $outSide $spdyHeaders
     set result [fconfigure $outSide -checksum]
@@ -347,7 +347,7 @@ test zlib-8.10 {transformation and fconfigure} -setup {
     lassign [chan pipe] inSide outSide
 } -constraints {zlib recentZlib} -body {
     zlib push deflate $outSide -dictionary $spdyDict
-    fconfigure $outSide -blocking 0 -translation binary -buffering none
+    fconfigure $outSide -blocking 1 -translation binary -buffering none
     fconfigure $inSide -blocking 1 -translation binary
     puts -nonewline $outSide $spdyHeaders
     chan pop $outSide
@@ -369,7 +369,7 @@ test zlib-8.11 {transformation and fconfigure} -setup {
     set strm [zlib stream inflate]
 } -constraints zlib -body {
     zlib push deflate $outSide -dictionary $spdyDict
-    fconfigure $outSide -blocking 0 -translation binary -buffering none
+    fconfigure $outSide -blocking 1 -translation binary -buffering none
     fconfigure $inSide -blocking 1 -translation binary
     puts -nonewline $outSide $spdyHeaders
     chan pop $outSide
@@ -387,7 +387,7 @@ test zlib-8.12 {transformation and fconfigure} -setup {
 } -constraints zlib -body {
     $strm put -dictionary $spdyDict -finalize $spdyHeaders
     zlib push decompress $inSide
-    fconfigure $outSide -blocking 0 -translation binary
+    fconfigure $outSide -blocking 1 -translation binary
     fconfigure $inSide -translation binary -dictionary $spdyDict
     puts -nonewline $outSide [$strm get]
     close $outSide
@@ -404,7 +404,7 @@ test zlib-8.13 {transformation and fconfigure} -setup {
 } -constraints zlib -body {
     $strm put -dictionary $spdyDict -finalize $spdyHeaders
     zlib push decompress $inSide -dictionary $spdyDict
-    fconfigure $outSide -blocking 0 -translation binary
+    fconfigure $outSide -blocking 1 -translation binary
     fconfigure $inSide -translation binary
     puts -nonewline $outSide [$strm get]
     close $outSide
@@ -421,7 +421,7 @@ test zlib-8.14 {transformation and fconfigure} -setup {
 } -constraints zlib -body {
     $strm put -finalize -dictionary $spdyDict $spdyHeaders
     zlib push inflate $inSide
-    fconfigure $outSide -blocking 0 -buffering none -translation binary
+    fconfigure $outSide -blocking 1 -buffering none -translation binary
     fconfigure $inSide -translation binary -dictionary $spdyDict
     puts -nonewline $outSide [$strm get]
     close $outSide
@@ -437,7 +437,7 @@ test zlib-8.15 {transformation and fconfigure} -setup {
 } -constraints zlib -body {
     $strm put -finalize -dictionary $spdyDict $spdyHeaders
     zlib push inflate $inSide -dictionary $spdyDict
-    fconfigure $outSide -blocking 0 -buffering none -translation binary
+    fconfigure $outSide -blocking 1 -buffering none -translation binary
     fconfigure $inSide -translation binary
     puts -nonewline $outSide [$strm get]
     close $outSide
-- 
cgit v0.12

