github.com/askholme/packer@v0.7.2-0.20140924152349-70d9566a6852/packer/rpc/client_test.go (about) 1 package rpc 2 3 import ( 4 "net" 5 "testing" 6 ) 7 8 func testConn(t *testing.T) (net.Conn, net.Conn) { 9 l, err := net.Listen("tcp", "127.0.0.1:0") 10 if err != nil { 11 t.Fatalf("err: %s", err) 12 } 13 14 var serverConn net.Conn 15 doneCh := make(chan struct{}) 16 go func() { 17 defer close(doneCh) 18 defer l.Close() 19 var err error 20 serverConn, err = l.Accept() 21 if err != nil { 22 t.Fatalf("err: %s", err) 23 } 24 }() 25 26 clientConn, err := net.Dial("tcp", l.Addr().String()) 27 if err != nil { 28 t.Fatalf("err: %s", err) 29 } 30 <-doneCh 31 32 return clientConn, serverConn 33 } 34 35 func testClientServer(t *testing.T) (*Client, *Server) { 36 clientConn, serverConn := testConn(t) 37 38 server := NewServer(serverConn) 39 go server.Serve() 40 41 client, err := NewClient(clientConn) 42 if err != nil { 43 server.Close() 44 t.Fatalf("err: %s", err) 45 } 46 47 return client, server 48 }