From acb1d8debf23f3dbd9199d1276a86ada71750196 Mon Sep 17 00:00:00 2001
From: antirez <antirez@gmail.com>
Date: Wed, 21 Jan 2015 16:46:51 +0100
Subject: [PATCH] Cluster test: wait for port to unbound in kill_instance.

Otherwise kill_instance + restart_instance in short succession will
still find the port busy and will fail.
---
 tests/instances.tcl | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)

diff --git a/tests/instances.tcl b/tests/instances.tcl
index a68b79d11..7d87cdf59 100644
--- a/tests/instances.tcl
+++ b/tests/instances.tcl
@@ -370,15 +370,31 @@ proc get_instance_id_by_port {type port} {
 # The instance can be restarted with restart-instance.
 proc kill_instance {type id} {
     set pid [get_instance_attrib $type $id pid]
+    set port [get_instance_attrib $type $id port]
+
     if {$pid == -1} {
         error "You tried to kill $type $id twice."
     }
+
     exec kill -9 $pid
     set_instance_attrib $type $id pid -1
     set_instance_attrib $type $id link you_tried_to_talk_with_killed_instance
 
     # Remove the PID from the list of pids to kill at exit.
     set ::pids [lsearch -all -inline -not -exact $::pids $pid]
+
+    # Wait for the port it was using to be available again, so that's not
+    # an issue to start a new server ASAP with the same port.
+    set retry 10
+    while {[incr retry -1]} {
+        set port_is_free [catch {set s [socket 127.0.01 $port]}]
+        if {$port_is_free} break
+        catch {close $s}
+        after 1000
+    }
+    if {$retry == 0} {
+        error "Port $port does not return available after killing instance."
+    }
 }
 
 # Return true of the instance of the specified type/id is killed.