mirror of https://github.com/dirtbags/moth.git
State tests
This commit is contained in:
parent
316f44edae
commit
f8556dd2e5
|
@ -355,7 +355,7 @@ func (s *State) reopenEventLog() error {
|
||||||
log.Print(err)
|
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 {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -378,7 +378,7 @@ func (s *State) Maintain(updateInterval time.Duration) {
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case msg := <-s.eventStream:
|
case msg := <-s.eventStream:
|
||||||
fmt.Println(s.eventWriter, time.Now().Unix(), msg)
|
fmt.Fprintln(s.eventWriter, time.Now().Unix(), msg)
|
||||||
s.eventWriter.Sync()
|
s.eventWriter.Sync()
|
||||||
case <-ticker.C:
|
case <-ticker.C:
|
||||||
s.refresh()
|
s.refresh()
|
||||||
|
|
|
@ -3,6 +3,7 @@ package main
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"os"
|
"os"
|
||||||
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
@ -88,12 +89,53 @@ func TestStateEvents(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestStateMaintainer(t *testing.T) {
|
func TestStateMaintainer(t *testing.T) {
|
||||||
|
updateInterval := 10 * time.Millisecond
|
||||||
|
|
||||||
s := NewTestState()
|
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!")
|
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))
|
t.Error("Wrong event log length:", len(eventLog))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -19,17 +19,17 @@ type T struct {
|
||||||
// List is a collection of award events.
|
// List is a collection of award events.
|
||||||
type List []T
|
type List []T
|
||||||
|
|
||||||
// Len implements sort.Interface.
|
// Len returns the length of the awards list.
|
||||||
func (awards List) Len() int {
|
func (awards List) Len() int {
|
||||||
return len(awards)
|
return len(awards)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Less implements sort.Interface.
|
// Less returns true if i was awarded before j.
|
||||||
func (awards List) Less(i, j int) bool {
|
func (awards List) Less(i, j int) bool {
|
||||||
return awards[i].When < awards[j].When
|
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) {
|
func (awards List) Swap(i, j int) {
|
||||||
tmp := awards[i]
|
tmp := awards[i]
|
||||||
awards[i] = awards[j]
|
awards[i] = awards[j]
|
||||||
|
|
Loading…
Reference in New Issue