packer-cn/packer/rpc/client_test.go

49 lines
814 B
Go
Raw Normal View History

package rpc
import (
2013-12-09 17:44:26 -05:00
"net"
"testing"
)
2013-12-09 19:22:11 -05:00
func testConn(t *testing.T) (net.Conn, net.Conn) {
2013-12-09 17:44:26 -05:00
l, err := net.Listen("tcp", ":0")
if err != nil {
t.Fatalf("err: %s", err)
}
2013-12-09 19:22:11 -05:00
var serverConn net.Conn
doneCh := make(chan struct{})
2013-12-09 17:44:26 -05:00
go func() {
2013-12-09 19:22:11 -05:00
defer close(doneCh)
defer l.Close()
var err error
serverConn, err = l.Accept()
2013-12-09 17:44:26 -05:00
if err != nil {
t.Fatalf("err: %s", err)
}
}()
clientConn, err := net.Dial("tcp", l.Addr().String())
if err != nil {
t.Fatalf("err: %s", err)
}
2013-12-09 19:22:11 -05:00
<-doneCh
return clientConn, serverConn
}
func testClientServer(t *testing.T) (*Client, *Server) {
clientConn, serverConn := testConn(t)
server := NewServer(serverConn)
go server.Serve()
2013-12-09 17:44:26 -05:00
client, err := NewClient(clientConn)
if err != nil {
2013-12-09 19:22:11 -05:00
server.Close()
2013-12-09 17:44:26 -05:00
t.Fatalf("err: %s", err)
}
2013-12-09 19:22:11 -05:00
return client, server
}