-
Notifications
You must be signed in to change notification settings - Fork 454
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract common test helpers to own files
Proxy test file is overused of multiple logics. Make it a bit cleaner, moved `WithTCPServer` to testhelper. Tried to move `WithTCPProxy`, but current structure does not allow it. If move it under package testhelper, it introduces import packages loop. For now extract it to separate helper: `toxiproxy_test.go`. In future it would nice to move business logic code under folder `pkg`.
- Loading branch information
Showing
8 changed files
with
165 additions
and
121 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package testhelper | ||
|
||
import ( | ||
"io/ioutil" | ||
"net" | ||
"testing" | ||
) | ||
|
||
func NewTCPServer() (*TCPServer, error) { | ||
result := &TCPServer{ | ||
addr: "localhost:0", | ||
response: make(chan []byte, 1), | ||
} | ||
err := result.Run() | ||
if err != nil { | ||
return nil, err | ||
} | ||
return result, nil | ||
} | ||
|
||
type TCPServer struct { | ||
addr string | ||
server net.Listener | ||
response chan []byte | ||
} | ||
|
||
func (server *TCPServer) Run() (err error) { | ||
server.server, err = net.Listen("tcp", server.addr) | ||
if err != nil { | ||
return | ||
} | ||
server.addr = server.server.Addr().String() | ||
return | ||
} | ||
|
||
func (server *TCPServer) handle_connection() (err error) { | ||
conn, err := server.server.Accept() | ||
if err != nil { | ||
return | ||
} | ||
defer conn.Close() | ||
|
||
val, err := ioutil.ReadAll(conn) | ||
if err != nil { | ||
return | ||
} | ||
|
||
server.response <- val | ||
return | ||
} | ||
|
||
func (server *TCPServer) Close() (err error) { | ||
return server.server.Close() | ||
} | ||
|
||
func WithTCPServer(t *testing.T, block func(string, chan []byte)) { | ||
server, err := NewTCPServer() | ||
if err != nil { | ||
t.Fatal("Failed to create TCP server", err) | ||
} | ||
go func(t *testing.T, server *TCPServer) { | ||
err := server.handle_connection() | ||
if err != nil { | ||
t.Error("Failed to handle connection", err) | ||
} | ||
}(t, server) | ||
block(server.addr, server.response) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package testhelper_test | ||
|
||
import ( | ||
"bytes" | ||
"net" | ||
"testing" | ||
|
||
"github.com/Shopify/toxiproxy/v2/testhelper" | ||
) | ||
|
||
func TestSimpleServer(t *testing.T) { | ||
testhelper.WithTCPServer(t, func(addr string, response chan []byte) { | ||
conn, err := net.Dial("tcp", addr) | ||
if err != nil { | ||
t.Error("Unable to dial TCP server", err) | ||
} | ||
|
||
msg := []byte("hello world") | ||
|
||
_, err = conn.Write(msg) | ||
if err != nil { | ||
t.Error("Failed writing to TCP server", err) | ||
} | ||
|
||
err = conn.Close() | ||
if err != nil { | ||
t.Error("Failed to close TCP connection", err) | ||
} | ||
|
||
resp := <-response | ||
if !bytes.Equal(resp, msg) { | ||
t.Error("Server didn't read bytes from client") | ||
} | ||
}) | ||
} |
File renamed without changes.
8 changes: 5 additions & 3 deletions
8
testhelper/testhelper_test.go → testhelper/timeout_after_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package toxiproxy_test | ||
|
||
import ( | ||
"net" | ||
"testing" | ||
|
||
"github.com/Shopify/toxiproxy/v2" | ||
"github.com/Shopify/toxiproxy/v2/testhelper" | ||
) | ||
|
||
func NewTestProxy(name, upstream string) *toxiproxy.Proxy { | ||
proxy := toxiproxy.NewProxy() | ||
|
||
proxy.Name = name | ||
proxy.Listen = "localhost:0" | ||
proxy.Upstream = upstream | ||
|
||
return proxy | ||
} | ||
|
||
func WithTCPProxy( | ||
t *testing.T, | ||
f func(proxy net.Conn, response chan []byte, proxyServer *toxiproxy.Proxy), | ||
) { | ||
testhelper.WithTCPServer(t, func(upstream string, response chan []byte) { | ||
proxy := NewTestProxy("test", upstream) | ||
proxy.Start() | ||
|
||
conn := AssertProxyUp(t, proxy.Listen, true) | ||
|
||
f(conn, response, proxy) | ||
|
||
proxy.Stop() | ||
}) | ||
} | ||
|
||
func AssertProxyUp(t *testing.T, addr string, up bool) net.Conn { | ||
conn, err := net.Dial("tcp", addr) | ||
if err != nil && up { | ||
t.Error("Expected proxy to be up:", err) | ||
} else if err == nil && !up { | ||
t.Error("Expected proxy to be down") | ||
} | ||
return conn | ||
} |