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())
}
time.Sleep(TestMaintenanceInterval)
stateProvider.refresh()
if r := hs.TestRequest("/state", nil); r.Result().StatusCode != 200 {
t.Error(r.Result())
@ -131,7 +131,6 @@ func TestHttpd(t *testing.T) {
t.Error("Unexpected body", r.Body.String())
}
time.Sleep(TestMaintenanceInterval)
stateProvider.refresh()
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)
}
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 {
t.Error("Didn't unlock next puzzle")
}

View File

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