Skip to content

Commit

Permalink
Wired resume timer all the way from the Controller's POST down to the…
Browse files Browse the repository at this point in the history
… CountdownTimer's resume()

Closes issue #210
  • Loading branch information
tedyoung committed Apr 9, 2024
1 parent 0e14990 commit 6f6cbac
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ public ResponseEntity<Void> pauseTimer(@PathVariable("ensembleId") Long id) {

@PostMapping("/resume-timer/{ensembleId}")
public ResponseEntity<Void> resumeTimer(@PathVariable("ensembleId") Long id) {
ensembleTimerHolder.resumeTimerFor(EnsembleId.of(id));
return ResponseEntity.noContent().build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,14 @@ public void pauseTimerFor(EnsembleId ensembleId) {
broadcaster.sendCurrentTimer(ensembleTimer);
}

public void resumeTimerFor(EnsembleId ensembleId) {
EnsembleTimer ensembleTimer = timerFor(ensembleId);

ensembleTimer.resume();

broadcaster.sendCurrentTimer(ensembleTimer);
}

static class SingleEntryHashMap<K, V> extends HashMap<K, V> {
@Override
public V put(K key, V value) {
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/com/jitterted/mobreg/domain/EnsembleTimer.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@ public void pause() {
turnTimer.pause();
}

public void resume() {
turnTimer.resume();
}

private void requireFinished() {
if (turnTimer.state() != FINISHED) {
throw new IllegalStateException("Can't Rotate when timer state is %s".formatted(turnTimer.state()));
Expand Down Expand Up @@ -115,4 +119,5 @@ public boolean equals(Object o) {
public int hashCode() {
return ensembleId.hashCode();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,18 @@ void pauseTimerPausesTheSpecifiedEnsembleTimer() {
.isEqualByComparingTo(CountdownTimer.TimerState.PAUSED);
}

@Test
void resumePausedTimerResumesTheSpecifiedTimer() {
Fixture fixture = createEnsembleAndTimerWithIdOf(672);
fixture.ensembleTimerController().startTimer(672L);
fixture.ensembleTimerController().pauseTimer(672L);

fixture.ensembleTimerController().resumeTimer(672L);

assertThat(fixture.ensembleTimer().state())
.isEqualByComparingTo(CountdownTimer.TimerState.RUNNING);
}

// --- FIXTURES ---

public static Fixture createEnsembleAndTimerWithIdOf(int ensembleId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,21 @@ void onTimerPauseBroadcastsPauseState() {
fixture.mockBroadcaster().verifyTimerStateSent();
}

@Test
void onTimerResumeBroadcastsRunningState() {
BroadcastFixture fixture = createBroadcasterWithStartedEnsembleTimer(
218,
CountdownTimer.TimerState.RUNNING,
TimeRemaining.from(Duration.ofMinutes(2), Duration.ofMinutes(4)));
fixture.tickAfterStart(Duration.ofMinutes(2));
fixture.ensembleTimerHolder().pauseTimerFor(EnsembleId.of(218));
fixture.mockBroadcaster().reset();

fixture.ensembleTimerHolder().resumeTimerFor(EnsembleId.of(218));

fixture.mockBroadcaster().verifyTimerStateSent();
}

@Test
void onTickWhenFinishedBroadcastsTimerFinished() {
BroadcastFixture fixture = createBroadcasterWithStartedEnsembleTimer(
Expand Down Expand Up @@ -288,7 +303,7 @@ void reset() {

private void verifyTimerStateSent() {
assertThat(wasCalled)
.as("Expected sendCurrentTimer() to have been called on the Broadcaster")
.as("Expected sendCurrentTimer(%s) to have been called on the Broadcaster", expectedTimerState)
.isTrue();
assertAll(
"Timer State",
Expand Down
43 changes: 34 additions & 9 deletions src/test/java/com/jitterted/mobreg/domain/CountdownTimerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,18 @@ void timeRemainingIsOnlyReducedDuringTimerRunning() {
assertThat(countdownTimer.timeRemaining().seconds())
.as("Expected 38 seconds left on the timer")
.isEqualTo(50 - 10 - 1 - 1);
}

@Test
@Disabled
void pauseAlreadyPausedTimerIsNoOp() {
// proves that pause() is idempotent
}

@Test
@Disabled
void resumeRunningTimerIsNoOp() {
// proves that resume() is idempotent
}

@Nested
Expand Down Expand Up @@ -125,28 +136,42 @@ void tickWhenFinishedThrowsException() {
.formatted(finishedAtPlus20Millis));
}

private Fixture createFinishedTimer() {
CountdownTimer countdownTimer = new CountdownTimer(EnsembleTimer.DEFAULT_TIMER_DURATION);
Instant timerStartedAt = Instant.now();
countdownTimer.startAt(timerStartedAt);
countdownTimer.tick(timerStartedAt.plus(Duration.ofMinutes(4).plusMillis(1)));
return new Fixture(countdownTimer, timerStartedAt);
@Test
@Disabled("Waiting for CountdownTimer to know its state")
void exceptionThrownOnPauseWhenTimerNotStarted() {
// implement me
}

private record Fixture(CountdownTimer countdownTimer, Instant timerStartedAt) {
@Test
@Disabled("Waiting for CountdownTimer to know its state")
void exceptionThrownOnPauseWhenTimerFinished() {
// implement me
}

@Test
@Disabled("Waiting for CountdownTimer to know its state")
void exceptionThrownOnPauseWhenTimerNotStarted() {
void exceptionThrownOnResumeWhenTimerNotStarted() {
// implement me
}

@Test
@Disabled("Waiting for CountdownTimer to know its state")
void exceptionThrownOnPauseWhenTimerFinished() {
void exceptionThrownOnResumeWhenTimerFinished() {
// implement me
}



private Fixture createFinishedTimer() {
CountdownTimer countdownTimer = new CountdownTimer(EnsembleTimer.DEFAULT_TIMER_DURATION);
Instant timerStartedAt = Instant.now();
countdownTimer.startAt(timerStartedAt);
countdownTimer.tick(timerStartedAt.plus(Duration.ofMinutes(4).plusMillis(1)));
return new Fixture(countdownTimer, timerStartedAt);
}
private record Fixture(CountdownTimer countdownTimer, Instant timerStartedAt) {

}
}

}
12 changes: 12 additions & 0 deletions src/test/java/com/jitterted/mobreg/domain/EnsembleTimerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,18 @@ void isPausedWhenTimerPaused() {
Duration.ofMinutes(4)));
}

@Test
void isResumedWhenTimerResumed() {
EnsembleTimer ensembleTimer = EnsembleTimerFactory.createTimer();
ensembleTimer.startTimerAt(Instant.now());
ensembleTimer.pause();

ensembleTimer.resume();

assertThat(ensembleTimer.state())
.isEqualByComparingTo(CountdownTimer.TimerState.RUNNING);
}

@Test
void isFinishedWhenTickTimeAtEndTime() {
EnsembleTimer ensembleTimer = EnsembleTimerFactory.createTimerWith4MinuteDuration();
Expand Down

0 comments on commit 6f6cbac

Please sign in to comment.