State tests

This commit is contained in:
Neale Pickett 2020-08-19 15:38:13 -06:00
parent 316f44edae
commit f8556dd2e5
3 changed files with 50 additions and 8 deletions

View File

@ -355,7 +355,7 @@ func (s *State) reopenEventLog() error {
log.Print(err)
}
}
eventWriter, err := s.OpenFile("events.log", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
eventWriter, err := s.OpenFile("event.log", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
return err
}
@ -378,7 +378,7 @@ func (s *State) Maintain(updateInterval time.Duration) {
for {
select {
case msg := <-s.eventStream:
fmt.Println(s.eventWriter, time.Now().Unix(), msg)
fmt.Fprintln(s.eventWriter, time.Now().Unix(), msg)
s.eventWriter.Sync()
case <-ticker.C:
s.refresh()

View File

@ -3,6 +3,7 @@ package main
import (
"bytes"
"os"
"strings"
"testing"
"time"
@ -88,12 +89,53 @@ func TestStateEvents(t *testing.T) {
}
func TestStateMaintainer(t *testing.T) {
updateInterval := 10 * time.Millisecond
s := NewTestState()
go s.Maintain(2 * time.Second)
go s.Maintain(updateInterval)
if _, err := s.Stat("initialized"); err != nil {
t.Error(err)
}
teamIDLines, err := afero.ReadFile(s, "teamids.txt")
if err != nil {
t.Error(err)
}
teamIDList := strings.Split(string(teamIDLines), "\n")
if len(teamIDList) != 101 {
t.Error("TeamIDList length is", len(teamIDList))
}
teamID := teamIDList[0]
if len(teamID) < 6 {
t.Error("Team ID too short:", teamID)
}
s.LogEvent("Hello!")
eventLog, _ := afero.ReadFile(s.Fs, "event.log")
if len(eventLog) != 12 {
if len(s.PointsLog()) != 0 {
t.Error("Points log is not empty")
}
if err := s.SetTeamName(teamID, "The Patricks"); err != nil {
t.Error(err)
}
if err := s.AwardPoints(teamID, "pategory", 31337); err != nil {
t.Error(err)
}
time.Sleep(updateInterval)
pl := s.PointsLog()
if len(pl) != 1 {
t.Error("Points log should have one entry")
}
if (pl[0].Category != "pategory") || (pl[0].TeamID != teamID) {
t.Error("Wrong points event was recorded")
}
time.Sleep(updateInterval)
eventLog, err := afero.ReadFile(s.Fs, "event.log")
if err != nil {
t.Error(err)
} else if len(eventLog) != 18 {
t.Error("Wrong event log length:", len(eventLog))
}
}

View File

@ -19,17 +19,17 @@ type T struct {
// List is a collection of award events.
type List []T
// Len implements sort.Interface.
// Len returns the length of the awards list.
func (awards List) Len() int {
return len(awards)
}
// Less implements sort.Interface.
// Less returns true if i was awarded before j.
func (awards List) Less(i, j int) bool {
return awards[i].When < awards[j].When
}
// Swap implements sort.Interface.
// Swap exchanges the awards in positions i and j.
func (awards List) Swap(i, j int) {
tmp := awards[i]
awards[i] = awards[j]