Skip to content

Commit

Permalink
Changed the SingleEntryHashMap inside the EnsembleTimerHolder to clea…
Browse files Browse the repository at this point in the history
…r out the old entry when a new one is being added via put.
  • Loading branch information
tedyoung committed Dec 18, 2024
1 parent d062355 commit 2d13535
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -191,9 +191,8 @@ public boolean hasTimer() {
static class SingleEntryHashMap<K, V> extends HashMap<K, V> {
@Override
public V put(K key, V value) {
if (this.size() == 1 && !this.containsKey(key)) {
throw new IllegalStateException("A SingleEntryHashMap cannot have more than one entry, has entry for %s, attempting to add entry for %s"
.formatted(keySet().iterator().next(), key));
if (this.size() == 1) {
this.clear();
}
return super.put(key, value);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,12 @@ void createTimerFailsWhenTimerExistsForAnotherEnsemble() {
EnsembleTimerHolder ensembleTimerHolder = EnsembleTimerHolder.createNull(builder.ensembleRepository(), builder.memberRepository());
ensembleTimerHolder.createTimerFor(ensembleWithTimerCreatedFirst.getId(), new NoOpShuffler());

assertThatIllegalStateException().isThrownBy(
() -> ensembleTimerHolder
.createTimerFor(ensembleWithTimerCreatedSecond.getId(), new NoOpShuffler()));
EnsembleTimer timerForSecondEnsemble = ensembleTimerHolder.createTimerFor(ensembleWithTimerCreatedSecond.getId(), new NoOpShuffler());

assertThat(timerForSecondEnsemble.ensembleId().id())
.isEqualTo(ensembleWithTimerCreatedSecondId);
assertThatIllegalStateException()
.isThrownBy(() -> ensembleTimerHolder.timerFor(ensembleWithTimerCreatedFirst.getId()));
}
}

Expand Down

0 comments on commit 2d13535

Please sign in to comment.