TUN-1577: decompose carrier.StartServer to make TestStartServer less flappy
This commit is contained in:
parent
102b364cc9
commit
9a43a92b1c
|
@ -38,8 +38,8 @@ func StartClient(logger *logrus.Logger, originURL string, stream io.ReadWriter,
|
||||||
return serveStream(logger, originURL, stream, headers)
|
return serveStream(logger, originURL, stream, headers)
|
||||||
}
|
}
|
||||||
|
|
||||||
// StartServer will setup a server on a specified port and copy data over a WebSocket connection
|
// StartServer will setup a listener on a specified address/port and then
|
||||||
// to the edge (originURL)
|
// forward connections to the origin by calling `Serve()`.
|
||||||
func StartServer(logger *logrus.Logger, address, originURL string, shutdownC <-chan struct{}, headers http.Header) error {
|
func StartServer(logger *logrus.Logger, address, originURL string, shutdownC <-chan struct{}, headers http.Header) error {
|
||||||
listener, err := net.Listen("tcp", address)
|
listener, err := net.Listen("tcp", address)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -47,6 +47,14 @@ func StartServer(logger *logrus.Logger, address, originURL string, shutdownC <-c
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
logger.Info("Started listening on ", address)
|
logger.Info("Started listening on ", address)
|
||||||
|
return Serve(logger, listener, originURL, shutdownC, headers)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Serve accepts incoming connections on the specified net.Listener.
|
||||||
|
// Each connection is handled in a new goroutine: its data is copied over a
|
||||||
|
// WebSocket connection to the edge (originURL).
|
||||||
|
// `Serve` always closes `listener`.
|
||||||
|
func Serve(logger *logrus.Logger, listener net.Listener, originURL string, shutdownC <-chan struct{}, headers http.Header) error {
|
||||||
defer listener.Close()
|
defer listener.Close()
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
|
@ -62,7 +70,7 @@ func StartServer(logger *logrus.Logger, address, originURL string, shutdownC <-c
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// serveConnection handles connections for the StartServer call
|
// serveConnection handles connections for the Serve() call
|
||||||
func serveConnection(logger *logrus.Logger, c net.Conn, originURL string, headers http.Header) {
|
func serveConnection(logger *logrus.Logger, c net.Conn, originURL string, headers http.Header) {
|
||||||
defer c.Close()
|
defer c.Close()
|
||||||
serveStream(logger, originURL, c, headers)
|
serveStream(logger, originURL, c, headers)
|
||||||
|
|
|
@ -58,7 +58,10 @@ func TestStartClient(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestStartServer(t *testing.T) {
|
func TestStartServer(t *testing.T) {
|
||||||
listenerAddress := "localhost:1117"
|
listener, err := net.Listen("tcp", "localhost:")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Error starting listener: %v", err)
|
||||||
|
}
|
||||||
message := "Good morning Austin! Time for another sunny day in the great state of Texas."
|
message := "Good morning Austin! Time for another sunny day in the great state of Texas."
|
||||||
logger := logrus.New()
|
logger := logrus.New()
|
||||||
shutdownC := make(chan struct{})
|
shutdownC := make(chan struct{})
|
||||||
|
@ -66,16 +69,13 @@ func TestStartServer(t *testing.T) {
|
||||||
defer ts.Close()
|
defer ts.Close()
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
err := StartServer(logger, listenerAddress, "http://"+ts.Listener.Addr().String(), shutdownC, nil)
|
err := Serve(logger, listener, "http://"+ts.Listener.Addr().String(), shutdownC, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("Error starting server: %v", err)
|
t.Fatalf("Error running server: %v", err)
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
conn, err := net.Dial("tcp", listenerAddress)
|
conn, err := net.Dial("tcp", listener.Addr().String())
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("Error connecting to server: %v", err)
|
|
||||||
}
|
|
||||||
conn.Write([]byte(message))
|
conn.Write([]byte(message))
|
||||||
|
|
||||||
readBuffer := make([]byte, len(message))
|
readBuffer := make([]byte, len(message))
|
||||||
|
|
Loading…
Reference in New Issue