From 2b750ea49f39c0ac113fd60cee52c7f150a6da60 Mon Sep 17 00:00:00 2001 From: Jae Kwon Date: Tue, 6 Dec 2016 01:13:03 -0800 Subject: [PATCH] Make Connect2Switches blocking --- switch.go | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/switch.go b/switch.go index d029854e7..f39ed092e 100644 --- a/switch.go +++ b/switch.go @@ -483,13 +483,23 @@ func MakeConnectedSwitches(n int, initSwitch func(int, *Switch) *Switch, connect } // Will connect switches i and j via net.Pipe() +// Blocks until a conection is established. // NOTE: caller ensures i and j are within bounds func Connect2Switches(switches []*Switch, i, j int) { switchI := switches[i] switchJ := switches[j] c1, c2 := net.Pipe() - go switchI.AddPeerWithConnection(c1, false) // AddPeer is blocking, requires handshake. - go switchJ.AddPeerWithConnection(c2, true) + doneCh := make(chan struct{}) + go func() { + switchI.AddPeerWithConnection(c1, false) // AddPeer is blocking, requires handshake. + doneCh <- struct{}{} + }() + go func() { + switchJ.AddPeerWithConnection(c2, true) + doneCh <- struct{}{} + }() + <-doneCh + <-doneCh } func StartSwitches(switches []*Switch) error {