Stop running goroutines in unit tests

This commit is contained in:
Neale Pickett 2022-05-10 19:36:36 -06:00
parent dfc31eb9f3
commit 85f5b96a40
2 changed files with 6 additions and 12 deletions

View File

@ -74,7 +74,7 @@ func TestHttpd(t *testing.T) {
t.Error("Register failed", r.Body.String()) t.Error("Register failed", r.Body.String())
} }
time.Sleep(TestMaintenanceInterval) stateProvider.refresh()
if r := hs.TestRequest("/state", nil); r.Result().StatusCode != 200 { if r := hs.TestRequest("/state", nil); r.Result().StatusCode != 200 {
t.Error(r.Result()) t.Error(r.Result())
@ -131,7 +131,6 @@ func TestHttpd(t *testing.T) {
t.Error("Unexpected body", r.Body.String()) t.Error("Unexpected body", r.Body.String())
} }
time.Sleep(TestMaintenanceInterval)
stateProvider.refresh() stateProvider.refresh()
if r := hs.TestRequest("/content/pategory/2/puzzle.json", nil); r.Result().StatusCode != 200 { if r := hs.TestRequest("/content/pategory/2/puzzle.json", nil); r.Result().StatusCode != 200 {
@ -149,7 +148,7 @@ func TestHttpd(t *testing.T) {
log.Print(v) log.Print(v)
} }
t.Errorf("Points log wrong length. Wanted 1, got %v", state.PointsLog) t.Errorf("Points log wrong length. Wanted 1, got %v (length %d)", state.PointsLog, len(state.PointsLog))
} else if len(state.Puzzles["pategory"]) != 2 { } else if len(state.Puzzles["pategory"]) != 2 {
t.Error("Didn't unlock next puzzle") t.Error("Didn't unlock next puzzle")
} }

View File

@ -3,12 +3,10 @@ package main
import ( import (
"io/ioutil" "io/ioutil"
"testing" "testing"
"time"
"github.com/spf13/afero" "github.com/spf13/afero"
) )
const TestMaintenanceInterval = time.Millisecond * 1
const TestTeamID = "teamID" const TestTeamID = "teamID"
// NewTestServer creates a new MothServer with NewTestMothballs and some initial state. // NewTestServer creates a new MothServer with NewTestMothballs and some initial state.
@ -17,17 +15,14 @@ const TestTeamID = "teamID"
func NewTestServer() *MothServer { func NewTestServer() *MothServer {
puzzles := NewTestMothballs() puzzles := NewTestMothballs()
puzzles.refresh() puzzles.refresh()
go puzzles.Maintain(TestMaintenanceInterval)
state := NewTestState() state := NewTestState()
afero.WriteFile(state, "teamids.txt", []byte("teamID\n"), 0644) afero.WriteFile(state, "teamids.txt", []byte("teamID\n"), 0644)
afero.WriteFile(state, "messages.html", []byte("messages.html"), 0644) afero.WriteFile(state, "messages.html", []byte("messages.html"), 0644)
state.refresh() state.refresh()
go state.Maintain(TestMaintenanceInterval)
theme := NewTestTheme() theme := NewTestTheme()
afero.WriteFile(theme.Fs, "/index.html", []byte("index.html"), 0644) afero.WriteFile(theme.Fs, "/index.html", []byte("index.html"), 0644)
go theme.Maintain(TestMaintenanceInterval)
return NewMothServer(Configuration{}, theme, state, puzzles) return NewMothServer(Configuration{}, theme, state, puzzles)
} }
@ -54,6 +49,7 @@ func TestProdServer(t *testing.T) {
teamID := TestTeamID teamID := TestTeamID
server := NewTestServer() server := NewTestServer()
state := server.State.(*State)
handler := server.NewHandler(participantID, teamID) handler := server.NewHandler(participantID, teamID)
anonHandler := server.NewHandler("badParticipantId", "badTeamId") anonHandler := server.NewHandler("badParticipantId", "badTeamId")
@ -85,8 +81,7 @@ func TestProdServer(t *testing.T) {
t.Error("index.html wrong contents", contents) t.Error("index.html wrong contents", contents)
} }
// Wait for refresh to pick everything up state.refresh()
time.Sleep(TestMaintenanceInterval)
{ {
es := handler.ExportState() es := handler.ExportState()
@ -139,7 +134,7 @@ func TestProdServer(t *testing.T) {
t.Error("Right answer marked wrong", err) t.Error("Right answer marked wrong", err)
} }
time.Sleep(TestMaintenanceInterval) state.refresh()
{ {
es := handler.ExportState() es := handler.ExportState()
@ -168,7 +163,7 @@ func TestProdServer(t *testing.T) {
t.Error("Right answer marked wrong:", err) t.Error("Right answer marked wrong:", err)
} }
time.Sleep(TestMaintenanceInterval) state.refresh()
{ {
es := anonHandler.ExportState() es := anonHandler.ExportState()