Fixed an issue with point removal

This commit is contained in:
John Donaldson 2022-05-18 07:44:01 -07:00
parent a81e3aa62f
commit bb92c82dd6
2 changed files with 22 additions and 17 deletions

View File

@ -29,6 +29,7 @@ const RFC3339Space = "2006-01-02 15:04:05Z07:00"
// ErrAlreadyRegistered means a team cannot be registered because it was registered previously.
var ErrAlreadyRegistered = errors.New("team ID has already been registered")
var NoMatchingPointEntry = errors.New("Unable to find matching point entry")
// State defines the current state of a MOTH instance.
// We use the filesystem for synchronization between threads.
@ -452,12 +453,13 @@ func (s *State) flushPointsLog(newPoints award.List) error {
s.pointsLogFileLock.Lock()
defer s.pointsLogFileLock.Unlock()
logf, err := s.OpenFile("points.log", os.O_CREATE|os.O_WRONLY, 0644)
logf, err := s.OpenFile("points.log", os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0644)
defer logf.Close()
if err != nil {
return fmt.Errorf("Can't write to points log: %s", err)
}
for _, pointEntry := range newPoints {
fmt.Fprintln(logf, pointEntry.String())
}
@ -481,7 +483,7 @@ func (s *State) RemovePoints(teamID string, cat string, points int) error {
}
if (! removed) {
return fmt.Errorf("Unable to find matching point entry")
return NoMatchingPointEntry
}
err := s.flushPointsLog(newPoints)
@ -511,7 +513,7 @@ func (s *State) RemovePointsAtTime(teamID string, cat string, points int, when i
}
if (! removed) {
return fmt.Errorf("Unable to find matching point entry")
return NoMatchingPointEntry
}
err := s.flushPointsLog(newPoints)

View File

@ -146,33 +146,34 @@ func TestStatePointsRemoval(t *testing.T) {
points1 := 100
points2 := points1 + 1
// Add points into our log
if err := s.AwardPoints(team, category, points1); err != nil {
t.Logf("Received unexpected error: %s", err)
t.Logf("Received unexpected error when awarding points: %s", err)
t.Fail()
}
s.refresh()
pointsLogLength := len(s.PointsLog())
if pointsLogLength != 1 {
t.Logf("Expected 1 point in the log, got %d", pointsLogLength)
t.Logf("Expected 1 point in the log after awarding, got %d", pointsLogLength)
t.Fail()
}
if err := s.AwardPoints(team, category, points2); err != nil {
t.Logf("Received unexpected error: %s", err)
t.Logf("Received unexpected error when awarding points: %s", err)
t.Fail()
}
s.refresh()
pointsLogLength = len(s.PointsLog())
if pointsLogLength != 2 {
t.Logf("Expected 2 point in the log, got %d", pointsLogLength)
t.Logf("Expected 2 points in the log after awarding, got %d", pointsLogLength)
t.Fail()
}
// Remove a point
if err := s.RemovePoints(team, category, points1); err != nil {
t.Logf("Received unexpected error: %s", err)
t.Logf("Received unexpected error when removing points1: %s", err)
t.Fail()
}
s.refresh()
@ -180,17 +181,18 @@ func TestStatePointsRemoval(t *testing.T) {
pointsLog := s.PointsLog()
pointsLogLength = len(pointsLog)
if pointsLogLength != 1 {
t.Logf("Expected 1 point in the log, got %d", pointsLogLength)
t.Logf("Expected 1 point in the log after removal, got %d", pointsLogLength)
t.Fail()
}
if ((pointsLog[0].TeamID == team) && (pointsLog[0].Category == category) && (pointsLog[0].Points == points2)) {
t.Logf("Found unexpected points log entry: %s", pointsLog[0])
if ((pointsLog[0].TeamID != team) || (pointsLog[0].Category != category) || (pointsLog[0].Points != points2)) {
t.Logf("Found unexpected points log entry after removal: %s", pointsLog[0])
t.Fail()
}
if err := s.RemovePoints(team, category, points1); err != nil {
t.Logf("Received unexpected error: %s", err)
// Remove a duplicate point
if err := s.RemovePoints(team, category, points1); err != NoMatchingPointEntry {
t.Logf("Expected to receive NoMatchingPointEntry, received error '%s', instead", err)
t.Fail()
}
s.refresh()
@ -198,12 +200,13 @@ func TestStatePointsRemoval(t *testing.T) {
pointsLog = s.PointsLog()
pointsLogLength = len(pointsLog)
if pointsLogLength != 1 {
t.Logf("Expected 1 point in the log, got %d", pointsLogLength)
t.Logf("Expected 1 point in the log after duplicate removal, got %d", pointsLogLength)
t.Fail()
}
// Remove the second point
if err := s.RemovePoints(team, category, points2); err != nil {
t.Logf("Received unexpected error: %s", err)
t.Logf("Received unexpected error when removing points2: %s", err)
t.Fail()
}
s.refresh()
@ -211,7 +214,7 @@ func TestStatePointsRemoval(t *testing.T) {
pointsLog = s.PointsLog()
pointsLogLength = len(pointsLog)
if pointsLogLength != 0 {
t.Logf("Expected 0 point in the log, got %d", pointsLogLength)
t.Logf("Expected 0 point in the log after last removal, got %d", pointsLogLength)
t.Fail()
}
}