Fix tests with a proper start-up and shutdown procedure with a sync
This commit is contained in:
parent
179dd3f984
commit
d104c90445
4 changed files with 30 additions and 18 deletions
|
@ -5,14 +5,11 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"github.com/gorilla/mux"
|
|
||||||
"github.com/mdiluz/rove/pkg/accounts"
|
"github.com/mdiluz/rove/pkg/accounts"
|
||||||
)
|
)
|
||||||
|
|
||||||
// NewRouter sets up the server mux
|
// NewRouter sets up the server mux
|
||||||
func (s *Server) SetUpRouter() {
|
func (s *Server) SetUpRouter() {
|
||||||
s.router = mux.NewRouter().StrictSlash(true)
|
|
||||||
|
|
||||||
// Set up the handlers
|
// Set up the handlers
|
||||||
s.router.HandleFunc("/status", s.HandleStatus)
|
s.router.HandleFunc("/status", s.HandleStatus)
|
||||||
s.router.HandleFunc("/register", s.HandleRegister)
|
s.router.HandleFunc("/register", s.HandleRegister)
|
||||||
|
|
|
@ -13,8 +13,6 @@ func TestHandleStatus(t *testing.T) {
|
||||||
response := httptest.NewRecorder()
|
response := httptest.NewRecorder()
|
||||||
|
|
||||||
s := NewServer()
|
s := NewServer()
|
||||||
s.Initialise()
|
|
||||||
|
|
||||||
s.HandleStatus(response, request)
|
s.HandleStatus(response, request)
|
||||||
|
|
||||||
var status StatusResponse
|
var status StatusResponse
|
||||||
|
@ -36,8 +34,6 @@ func TestHandleRegister(t *testing.T) {
|
||||||
response := httptest.NewRecorder()
|
response := httptest.NewRecorder()
|
||||||
|
|
||||||
s := NewServer()
|
s := NewServer()
|
||||||
s.Initialise()
|
|
||||||
|
|
||||||
s.HandleRegister(response, request)
|
s.HandleRegister(response, request)
|
||||||
|
|
||||||
var status RegisterResponse
|
var status RegisterResponse
|
||||||
|
|
|
@ -1,9 +1,12 @@
|
||||||
package server
|
package server
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"sync"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/gorilla/mux"
|
"github.com/gorilla/mux"
|
||||||
"github.com/mdiluz/rove/pkg/accounts"
|
"github.com/mdiluz/rove/pkg/accounts"
|
||||||
|
@ -25,9 +28,12 @@ type Server struct {
|
||||||
accountant *accounts.Accountant
|
accountant *accounts.Accountant
|
||||||
world *game.World
|
world *game.World
|
||||||
|
|
||||||
|
server *http.Server
|
||||||
router *mux.Router
|
router *mux.Router
|
||||||
|
|
||||||
persistence int
|
persistence int
|
||||||
|
|
||||||
|
sync sync.WaitGroup
|
||||||
}
|
}
|
||||||
|
|
||||||
// ServerOption defines a server creation option
|
// ServerOption defines a server creation option
|
||||||
|
@ -50,12 +56,16 @@ func OptionPersistentData() ServerOption {
|
||||||
// NewServer sets up a new server
|
// NewServer sets up a new server
|
||||||
func NewServer(opts ...ServerOption) *Server {
|
func NewServer(opts ...ServerOption) *Server {
|
||||||
|
|
||||||
|
router := mux.NewRouter().StrictSlash(true)
|
||||||
|
|
||||||
// Set up the default server
|
// Set up the default server
|
||||||
s := &Server{
|
s := &Server{
|
||||||
port: 8080,
|
port: 8080,
|
||||||
accountant: accounts.NewAccountant(),
|
accountant: accounts.NewAccountant(),
|
||||||
world: game.NewWorld(),
|
world: game.NewWorld(),
|
||||||
persistence: EphemeralData,
|
persistence: EphemeralData,
|
||||||
|
router: router,
|
||||||
|
server: &http.Server{Addr: ":8080", Handler: router},
|
||||||
}
|
}
|
||||||
|
|
||||||
// Apply all options
|
// Apply all options
|
||||||
|
@ -83,20 +93,34 @@ func (s *Server) Initialise() error {
|
||||||
s.SetUpRouter()
|
s.SetUpRouter()
|
||||||
fmt.Printf("Routes Created\n")
|
fmt.Printf("Routes Created\n")
|
||||||
|
|
||||||
|
// Add to our sync
|
||||||
|
s.sync.Add(1)
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Run executes the server
|
// Run executes the server
|
||||||
func (s *Server) Run() {
|
func (s *Server) Run() {
|
||||||
|
defer s.sync.Done()
|
||||||
|
|
||||||
// Listen and serve the http requests
|
// Listen and serve the http requests
|
||||||
fmt.Println("Serving HTTP")
|
fmt.Println("Serving HTTP")
|
||||||
if err := http.ListenAndServe(fmt.Sprintf(":%d", s.port), s.router); err != nil {
|
if err := s.server.ListenAndServe(); err != nil && err != http.ErrServerClosed {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Close closes up the server
|
// Close closes up the server
|
||||||
func (s *Server) Close() error {
|
func (s *Server) Close() error {
|
||||||
|
// Try and shut down the http server
|
||||||
|
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||||
|
defer cancel()
|
||||||
|
if err := s.server.Shutdown(ctx); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Wait until the server is shut down
|
||||||
|
s.sync.Wait()
|
||||||
|
|
||||||
// Save the accounts if requested
|
// Save the accounts if requested
|
||||||
if s.persistence == PersistentData {
|
if s.persistence == PersistentData {
|
||||||
|
|
|
@ -4,18 +4,13 @@ cd "$(dirname "$0")"
|
||||||
cd ..
|
cd ..
|
||||||
set -x
|
set -x
|
||||||
|
|
||||||
# Test the build
|
# Build and start the service
|
||||||
go build -v .
|
|
||||||
|
|
||||||
# Run unit tests
|
|
||||||
go test -v ./... -cover
|
|
||||||
|
|
||||||
# Verify docker build
|
|
||||||
docker build .
|
|
||||||
|
|
||||||
# Run the integration tests with docker-compose
|
|
||||||
docker-compose up --build --detach
|
docker-compose up --build --detach
|
||||||
|
|
||||||
|
# Run tests, including integration tests
|
||||||
go test -v ./... -tags integration -cover -coverprofile=/tmp/c.out
|
go test -v ./... -tags integration -cover -coverprofile=/tmp/c.out
|
||||||
|
|
||||||
|
# Take down the service
|
||||||
docker-compose down
|
docker-compose down
|
||||||
|
|
||||||
# Convert the coverage data to html
|
# Convert the coverage data to html
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue