Skip to content

Commit

Permalink
Add part 2 for net testing
Browse files Browse the repository at this point in the history
  • Loading branch information
Will Gough committed Jul 16, 2018
1 parent f7c8c99 commit eef00a0
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 26 deletions.
53 changes: 53 additions & 0 deletions 2018/July/net-testing/net.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bufio"
"errors"
"fmt"
"log"
"net"
"strings"
)
Expand All @@ -24,6 +25,9 @@ func NewServer(protocol, addr string) (Server, error) {
addr: addr,
}, nil
case "udp":
return &UDPServer{
addr: addr,
}, nil
}
return nil, errors.New("Invalid protocol given")
}
Expand Down Expand Up @@ -97,3 +101,52 @@ func (t *TCPServer) handleConnection(conn net.Conn) {
rw.Flush()
}
}

// UDPServer holds the necessary structure for our
// UDP server.
type UDPServer struct {
addr string
server *net.UDPConn
}

// Run starts the UDP server.
func (u *UDPServer) Run() (err error) {
laddr, err := net.ResolveUDPAddr("udp", u.addr)
if err != nil {
return errors.New("could not resolve UDP addr")
}

u.server, err = net.ListenUDP("udp", laddr)
if err != nil {
return errors.New("could not listen on UDP")
}

return u.handleConnections()
}

func (u *UDPServer) handleConnections() error {
var err error
for {
buf := make([]byte, 2048)
n, conn, err := u.server.ReadFromUDP(buf)
if err != nil {
log.Println(err)
break
}
if conn == nil {
continue
}

go u.handleConnection(conn, buf[:n])
}
return err
}

func (u *UDPServer) handleConnection(addr *net.UDPAddr, cmd []byte) {
u.server.WriteToUDP([]byte(fmt.Sprintf("Request recieved: %s", cmd)), addr)
}

// Close ensures that the UDPServer is shut down gracefully.
func (u *UDPServer) Close() error {
return u.server.Close()
}
80 changes: 54 additions & 26 deletions 2018/July/net-testing/net_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,33 +7,59 @@ import (
"testing"
)

var srv Server
var tcp, udp Server

func init() {
// Start the new server
srv, err := NewServer("tcp", ":1123")
tcp, err := NewServer("tcp", ":1123")
if err != nil {
log.Println("error starting TCP server")
return
}

// Run the server in goroutine to stop blocking
udp, err := NewServer("udp", ":6250")
if err != nil {
log.Println("error starting UDP server")
return
}

// Run the servers in goroutines to stop blocking
go func() {
srv.Run()
tcp.Run()
}()
go func() {
udp.Run()
}()
}

func TestNETServer_Running(t *testing.T) {
// Simply check that the server is up and can
// accept connections.
conn, err := net.Dial("tcp", ":1123")
if err != nil {
t.Error("could not connect to server: ", err)
servers := []struct {
protocol string
addr string
}{
{"tcp", ":1123"},
{"udp", ":6250"},
}
for _, serv := range servers {
conn, err := net.Dial(serv.protocol, serv.addr)
if err != nil {
t.Error("could not connect to server: ", err)
}
defer conn.Close()
}
defer conn.Close()
}

func TestNETServer_Request(t *testing.T) {
servers := []struct {
protocol string
addr string
}{
{"tcp", ":1123"},
{"udp", ":6250"},
}

tt := []struct {
test string
payload []byte
Expand All @@ -43,26 +69,28 @@ func TestNETServer_Request(t *testing.T) {
{"Sending another simple request works", []byte("goodbye world\n"), []byte("Request received: goodbye world")},
}

for _, tc := range tt {
t.Run(tc.test, func(t *testing.T) {
conn, err := net.Dial("tcp", ":1123")
if err != nil {
t.Error("could not connect to TCP server: ", err)
}
defer conn.Close()
for _, serv := range servers {
for _, tc := range tt {
t.Run(tc.test, func(t *testing.T) {
conn, err := net.Dial(serv.protocol, serv.addr)
if err != nil {
t.Error("could not connect to server: ", err)
}
defer conn.Close()

if _, err := conn.Write(tc.payload); err != nil {
t.Error("could not write payload to TCP server:", err)
}
if _, err := conn.Write(tc.payload); err != nil {
t.Error("could not write payload to server:", err)
}

out := make([]byte, 1024)
if _, err := conn.Read(out); err == nil {
if bytes.Compare(out, tc.want) == 0 {
t.Error("response did match expected output")
out := make([]byte, 1024)
if _, err := conn.Read(out); err == nil {
if bytes.Compare(out, tc.want) == 0 {
t.Error("response did match expected output")
}
} else {
t.Error("could not read from connection")
}
} else {
t.Error("could not read from connection")
}
})
})
}
}
}

0 comments on commit eef00a0

Please sign in to comment.