diff --git a/internal/multiplex/switchboard.go b/internal/multiplex/switchboard.go index 802c58d..fcff9f3 100644 --- a/internal/multiplex/switchboard.go +++ b/internal/multiplex/switchboard.go @@ -161,9 +161,7 @@ func (sb *switchboard) deplex(connId uint32, conn net.Conn) { sb.valve.AddRx(int64(n)) if err != nil { log.Debugf("a connection for session %v has closed: %v", sb.session.id, err) - //sb.close("a connection has dropped unexpectedly") - //return - sb.conns.Delete(connId) + sb.close("a connection has dropped unexpectedly") return } diff --git a/internal/multiplex/switchboard_test.go b/internal/multiplex/switchboard_test.go index 8ec8883..f0b9959 100644 --- a/internal/multiplex/switchboard_test.go +++ b/internal/multiplex/switchboard_test.go @@ -4,23 +4,14 @@ import ( "github.com/cbeuw/Cloak/internal/util" "github.com/cbeuw/connutil" "math/rand" - "net" "testing" "time" ) func TestSwitchboard_Send(t *testing.T) { - getHole := func() net.Conn { - l, _ := net.Listen("tcp", "127.0.0.1:0") - go func() { - net.Dial("tcp", l.Addr().String()) - }() - hole, _ := l.Accept() - return hole - } doTest := func(seshConfig SessionConfig) { sesh := MakeSession(0, seshConfig) - hole0 := getHole() + hole0 := connutil.Discard() sesh.sb.addConn(hole0) connId, _, err := sesh.sb.pickRandConn() if err != nil { @@ -35,7 +26,7 @@ func TestSwitchboard_Send(t *testing.T) { return } - hole1 := getHole() + hole1 := connutil.Discard() sesh.sb.addConn(hole1) connId, _, err = sesh.sb.pickRandConn() if err != nil { @@ -161,35 +152,19 @@ func TestSwitchboard_TxCredit(t *testing.T) { func TestSwitchboard_CloseOnOneDisconn(t *testing.T) { sesh := setupSesh(false) - l, _ := net.Listen("tcp", "127.0.0.1:0") - addRemoteConn := func(close chan struct{}) { - conn, _ := net.Dial("tcp", l.Addr().String()) - for { - conn.Write([]byte{0x00}) - <-close - conn.Close() - } - } + conn0client, conn0server := connutil.AsyncPipe() + sesh.AddConnection(conn0client) - close0 := make(chan struct{}) - go addRemoteConn(close0) - conn0, _ := l.Accept() - sesh.AddConnection(conn0) - - close1 := make(chan struct{}) - go addRemoteConn(close1) - conn1, _ := l.Accept() - sesh.AddConnection(conn1) - - close0 <- struct{}{} - - time.Sleep(100 * time.Millisecond) + conn1client, _ := connutil.AsyncPipe() + sesh.AddConnection(conn1client) + conn0server.Close() + time.Sleep(500 * time.Millisecond) if !sesh.IsClosed() { t.Error("session not closed after one conn is disconnected") return } - if _, err := conn1.Write([]byte{0x00}); err == nil { + if _, err := conn1client.Write([]byte{0x00}); err == nil { t.Error("the other conn is still connected") return }