Commit 27d45ee0 authored by Adrian Sutton's avatar Adrian Sutton

op-challenger: Cache the completed status of the game

parent 5ba697a2
...@@ -30,6 +30,8 @@ type GamePlayer struct { ...@@ -30,6 +30,8 @@ type GamePlayer struct {
caller GameInfo caller GameInfo
logger log.Logger logger log.Logger
cleanup func() error cleanup func() error
completed bool
} }
func NewGamePlayer( func NewGamePlayer(
...@@ -100,13 +102,9 @@ func NewGamePlayer( ...@@ -100,13 +102,9 @@ func NewGamePlayer(
} }
func (g *GamePlayer) ProgressGame(ctx context.Context) bool { func (g *GamePlayer) ProgressGame(ctx context.Context) bool {
status, err := g.caller.GetGameStatus(ctx) if g.completed {
if err != nil {
g.logger.Warn("Unable to retrieve game status", "err", err)
return false
}
if status != types.GameStatusInProgress {
// Game is already complete so don't try to perform further actions. // Game is already complete so don't try to perform further actions.
g.logger.Trace("Skipping completed game")
return true return true
} }
g.logger.Trace("Checking if actions are required") g.logger.Trace("Checking if actions are required")
...@@ -117,7 +115,8 @@ func (g *GamePlayer) ProgressGame(ctx context.Context) bool { ...@@ -117,7 +115,8 @@ func (g *GamePlayer) ProgressGame(ctx context.Context) bool {
g.logger.Warn("Unable to retrieve game status", "err", err) g.logger.Warn("Unable to retrieve game status", "err", err)
} else { } else {
g.logGameStatus(ctx, status) g.logGameStatus(ctx, status)
return status != types.GameStatusInProgress g.completed = status != types.GameStatusInProgress
return g.completed
} }
return false return false
} }
......
...@@ -93,8 +93,7 @@ func TestProgressGame_LogGameStatus(t *testing.T) { ...@@ -93,8 +93,7 @@ func TestProgressGame_LogGameStatus(t *testing.T) {
test := test test := test
t.Run(test.name, func(t *testing.T) { t.Run(test.name, func(t *testing.T) {
handler, game, gameState := setupProgressGameTest(t, test.agreeWithOutput) handler, game, gameState := setupProgressGameTest(t, test.agreeWithOutput)
gameState.preStatus = types.GameStatusInProgress gameState.status = test.status
gameState.postStatus = test.status
done := game.ProgressGame(context.Background()) done := game.ProgressGame(context.Background())
require.Equal(t, 1, gameState.callCount, "should perform next actions") require.Equal(t, 1, gameState.callCount, "should perform next actions")
...@@ -109,13 +108,17 @@ func TestProgressGame_LogGameStatus(t *testing.T) { ...@@ -109,13 +108,17 @@ func TestProgressGame_LogGameStatus(t *testing.T) {
func TestDoNotActOnCompleteGame(t *testing.T) { func TestDoNotActOnCompleteGame(t *testing.T) {
for _, status := range []types.GameStatus{types.GameStatusChallengerWon, types.GameStatusDefenderWon} { for _, status := range []types.GameStatus{types.GameStatusChallengerWon, types.GameStatusDefenderWon} {
t.Run(status.String(), func(t *testing.T) { t.Run(status.String(), func(t *testing.T) {
handler, game, gameState := setupProgressGameTest(t, true) _, game, gameState := setupProgressGameTest(t, true)
gameState.preStatus = status gameState.status = status
done := game.ProgressGame(context.Background()) done := game.ProgressGame(context.Background())
require.Equal(t, 0, gameState.callCount, "should not perform actions") require.Equal(t, 1, gameState.callCount, "acts the first time")
require.True(t, done, "should be done") require.True(t, done, "should be done")
require.Empty(t, handler.Logs, "should not log game status")
// Should not act when it knows the game is already complete
done = game.ProgressGame(context.Background())
require.Equal(t, 1, gameState.callCount, "does not act after game is complete")
require.True(t, done, "should still be done")
}) })
} }
} }
...@@ -137,8 +140,7 @@ func setupProgressGameTest(t *testing.T, agreeWithProposedRoot bool) (*testlog.C ...@@ -137,8 +140,7 @@ func setupProgressGameTest(t *testing.T, agreeWithProposedRoot bool) (*testlog.C
} }
type stubGameState struct { type stubGameState struct {
preStatus types.GameStatus status types.GameStatus
postStatus types.GameStatus
claimCount uint64 claimCount uint64
callCount int callCount int
actErr error actErr error
...@@ -151,10 +153,7 @@ func (s *stubGameState) Act(ctx context.Context) error { ...@@ -151,10 +153,7 @@ func (s *stubGameState) Act(ctx context.Context) error {
} }
func (s *stubGameState) GetGameStatus(ctx context.Context) (types.GameStatus, error) { func (s *stubGameState) GetGameStatus(ctx context.Context) (types.GameStatus, error) {
if s.callCount == 0 { return s.status, nil
return s.preStatus, nil
}
return s.postStatus, nil
} }
func (s *stubGameState) GetClaimCount(ctx context.Context) (uint64, error) { func (s *stubGameState) GetClaimCount(ctx context.Context) (uint64, error) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment