2
0
mirror of https://github.com/elisescu/tty-share synced 2024-11-03 23:15:42 +00:00
tty-share/testing/wrapped_double_net_conn.go

38 lines
628 B
Go
Raw Normal View History

2017-10-14 11:13:47 +00:00
package testing
import (
"net"
"sync"
"github.com/elisescu/tty-share/common"
)
func NewDoubleNetConn(debug bool) (client net.Conn, server net.Conn) {
var wg sync.WaitGroup
wg.Add(1)
var err error
listener, err := net.Listen("tcp", "localhost:0")
defer listener.Close()
if err != nil {
panic(err.Error())
}
go func() {
server, err = listener.Accept()
if err != nil {
panic(err.Error())
}
wg.Done()
}()
client, err = net.Dial("tcp", listener.Addr().String())
if err != nil {
panic(err.Error())
}
wg.Wait()
return common.NewWrappedConn(client, debug), common.NewWrappedConn(server, debug)
}