packer-cn/packer/rpc/client_test.go

54 lines
880 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) {
l, err := net.Listen("tcp", "127.0.0.1:0")
2013-12-09 17:44:26 -05:00
if err != nil {
t.Fatalf("err: %s", err)
}
2013-12-09 19:22:11 -05:00
var serverConn net.Conn
errChan := make(chan error)
2013-12-09 17:44:26 -05:00
go func() {
defer close(errChan)
2013-12-09 19:22:11 -05:00
defer l.Close()
var err error
serverConn, err = l.Accept()
2013-12-09 17:44:26 -05:00
if err != nil {
errChan <- err
return
2013-12-09 17:44:26 -05:00
}
}()
clientConn, err := net.Dial("tcp", l.Addr().String())
if err != nil {
t.Fatalf("err: %s", err)
}
err = <-errChan
if err != nil {
t.Fatalf("err: %s", err)
}
2013-12-09 19:22:11 -05:00
return clientConn, serverConn
}
func testClientServer(t *testing.T) (*Client, *Server) {
clientConn, serverConn := testConn(t)
server, _ := NewServer(serverConn)
2013-12-09 19:22:11 -05:00
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
}