Fix test instabilities by refactoring to make address dynamic and readable

This commit is contained in:
Marc Di Luzio 2020-06-06 11:52:12 +01:00
parent bc366583a4
commit 1d2087e2b9
7 changed files with 82 additions and 31 deletions

View file

@ -5,6 +5,7 @@ import (
"encoding/json"
"fmt"
"log"
"net"
"net/http"
"sync"
"time"
@ -27,12 +28,14 @@ const (
// Server contains the relevant data to run a game server
type Server struct {
port int
address string
accountant *accounts.Accountant
world *game.World
server *http.Server
listener net.Listener
server *http.Server
router *mux.Router
persistence int
@ -43,10 +46,10 @@ type Server struct {
// ServerOption defines a server creation option
type ServerOption func(s *Server)
// OptionPort sets the server port for hosting
func OptionPort(port int) ServerOption {
// OptionAddress sets the server address for hosting
func OptionAddress(address string) ServerOption {
return func(s *Server) {
s.port = port
s.address = address
}
}
@ -64,7 +67,7 @@ func NewServer(opts ...ServerOption) *Server {
// Set up the default server
s := &Server{
port: 8080,
address: "",
persistence: EphemeralData,
router: router,
}
@ -75,7 +78,7 @@ func NewServer(opts ...ServerOption) *Server {
}
// Set up the server object
s.server = &http.Server{Addr: fmt.Sprintf(":%d", s.port), Handler: router}
s.server = &http.Server{Addr: s.address, Handler: s.router}
// Create the accountant
s.accountant = accounts.NewAccountant()
@ -85,7 +88,10 @@ func NewServer(opts ...ServerOption) *Server {
}
// Initialise sets up internal state ready to serve
func (s *Server) Initialise() error {
func (s *Server) Initialise() (err error) {
// Add to our sync
s.sync.Add(1)
// Load the accounts if requested
if s.persistence == PersistentData {
@ -99,19 +105,26 @@ func (s *Server) Initialise() error {
s.router.HandleFunc(route.path, s.wrapHandler(route.method, route.handler))
}
// Add to our sync
s.sync.Add(1)
// Start the listen
if s.listener, err = net.Listen("tcp", s.server.Addr); err != nil {
return err
}
s.address = s.listener.Addr().String()
return nil
}
// Addr will return the server address set after the listen
func (s Server) Addr() string {
return s.address
}
// Run executes the server
func (s *Server) Run() {
defer s.sync.Done()
// Listen and serve the http requests
fmt.Printf("Serving HTTP on port %d\n", s.port)
if err := s.server.ListenAndServe(); err != nil && err != http.ErrServerClosed {
// Serve the http requests
if err := s.server.Serve(s.listener); err != nil && err != http.ErrServerClosed {
log.Fatal(err)
}
}

View file

@ -11,12 +11,12 @@ func TestNewServer(t *testing.T) {
}
}
func TestNewServer_OptionPort(t *testing.T) {
server := NewServer(OptionPort(1234))
func TestNewServer_OptionAddress(t *testing.T) {
server := NewServer(OptionAddress(":1234"))
if server == nil {
t.Error("Failed to create server")
} else if server.port != 1234 {
t.Error("Failed to set server port")
} else if server.address != ":1234" {
t.Error("Failed to set server address")
}
}
@ -36,6 +36,7 @@ func TestServer_Run(t *testing.T) {
} else if err := server.Initialise(); err != nil {
t.Error(err)
}
go server.Run()
if err := server.Close(); err != nil {
@ -50,6 +51,7 @@ func TestServer_RunPersistentData(t *testing.T) {
} else if err := server.Initialise(); err != nil {
t.Error(err)
}
go server.Run()
if err := server.Close(); err != nil {