Use connutil in test

This commit is contained in:
Andy Wang 2020-04-08 16:41:39 +01:00
parent dc2b1124cb
commit d3bc3b5a13
2 changed files with 10 additions and 37 deletions

View File

@ -161,9 +161,7 @@ func (sb *switchboard) deplex(connId uint32, conn net.Conn) {
sb.valve.AddRx(int64(n)) sb.valve.AddRx(int64(n))
if err != nil { if err != nil {
log.Debugf("a connection for session %v has closed: %v", sb.session.id, err) log.Debugf("a connection for session %v has closed: %v", sb.session.id, err)
//sb.close("a connection has dropped unexpectedly") sb.close("a connection has dropped unexpectedly")
//return
sb.conns.Delete(connId)
return return
} }

View File

@ -4,23 +4,14 @@ import (
"github.com/cbeuw/Cloak/internal/util" "github.com/cbeuw/Cloak/internal/util"
"github.com/cbeuw/connutil" "github.com/cbeuw/connutil"
"math/rand" "math/rand"
"net"
"testing" "testing"
"time" "time"
) )
func TestSwitchboard_Send(t *testing.T) { 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) { doTest := func(seshConfig SessionConfig) {
sesh := MakeSession(0, seshConfig) sesh := MakeSession(0, seshConfig)
hole0 := getHole() hole0 := connutil.Discard()
sesh.sb.addConn(hole0) sesh.sb.addConn(hole0)
connId, _, err := sesh.sb.pickRandConn() connId, _, err := sesh.sb.pickRandConn()
if err != nil { if err != nil {
@ -35,7 +26,7 @@ func TestSwitchboard_Send(t *testing.T) {
return return
} }
hole1 := getHole() hole1 := connutil.Discard()
sesh.sb.addConn(hole1) sesh.sb.addConn(hole1)
connId, _, err = sesh.sb.pickRandConn() connId, _, err = sesh.sb.pickRandConn()
if err != nil { if err != nil {
@ -161,35 +152,19 @@ func TestSwitchboard_TxCredit(t *testing.T) {
func TestSwitchboard_CloseOnOneDisconn(t *testing.T) { func TestSwitchboard_CloseOnOneDisconn(t *testing.T) {
sesh := setupSesh(false) sesh := setupSesh(false)
l, _ := net.Listen("tcp", "127.0.0.1:0") conn0client, conn0server := connutil.AsyncPipe()
addRemoteConn := func(close chan struct{}) { sesh.AddConnection(conn0client)
conn, _ := net.Dial("tcp", l.Addr().String())
for {
conn.Write([]byte{0x00})
<-close
conn.Close()
}
}
close0 := make(chan struct{}) conn1client, _ := connutil.AsyncPipe()
go addRemoteConn(close0) sesh.AddConnection(conn1client)
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)
conn0server.Close()
time.Sleep(500 * time.Millisecond)
if !sesh.IsClosed() { if !sesh.IsClosed() {
t.Error("session not closed after one conn is disconnected") t.Error("session not closed after one conn is disconnected")
return 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") t.Error("the other conn is still connected")
return return
} }