Skip to content

Commit

Permalink
Use iteration safe removal from map
Browse files Browse the repository at this point in the history
  • Loading branch information
a-a-hofmann committed Oct 9, 2019
1 parent 45e0255 commit 640ebdb
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,10 @@ public long count() {
}

public void removeMachinesOlderThan(Instant threshold) {
for (String machineKey : machines.keySet()) {
StateMachine machine = machines.get(machineKey);
machines.entrySet().removeIf(entry -> {
StateMachine machine = entry.getValue();
Instant completionTime = (Instant) machine.getExtendedState().getVariables().get(EvalMachineFactory.EXTENDED_VAR_COMPLETION_TIME);
if (completionTime.isBefore(threshold)) {
machines.remove(machineKey);
}
}
return completionTime.isBefore(threshold);
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ public void cleanUp() throws Exception {
StateMachine<EvalMachine.States, EvalMachine.Events> m1 = EvalMachineFactory.initSMForSubmission("123");
StateMachine<EvalMachine.States, EvalMachine.Events> m2 = EvalMachineFactory.initSMForSubmission("345");

m1.getExtendedState().getVariables().put(EvalMachineFactory.EXTENDED_VAR_COMPLETION_TIME, Instant.now().minus(30, ChronoUnit.MINUTES));
m2.getExtendedState().getVariables().put(EvalMachineFactory.EXTENDED_VAR_COMPLETION_TIME, Instant.now().minus(1, ChronoUnit.MINUTES));
m1.getExtendedState().getVariables().put(EvalMachineFactory.EXTENDED_VAR_COMPLETION_TIME, Instant.now().minus(1, ChronoUnit.MINUTES));
m2.getExtendedState().getVariables().put(EvalMachineFactory.EXTENDED_VAR_COMPLETION_TIME, Instant.now().minus(30, ChronoUnit.MINUTES));

EvalMachineRepoService repo = new EvalMachineRepoService();
repo.store(id1, m1);
Expand All @@ -36,8 +36,8 @@ public void cleanUp() throws Exception {
Instant fiveMinutesAgo = Instant.now().minus(5, ChronoUnit.MINUTES);
repo.removeMachinesOlderThan(fiveMinutesAgo);

Assertions.assertThat(repo.get(id1)).isNull();
Assertions.assertThat(repo.get(id2)).isNotNull();
Assertions.assertThat(repo.get(id1)).isNotNull();
Assertions.assertThat(repo.get(id2)).isNull();
}

@Test
Expand Down

0 comments on commit 640ebdb

Please sign in to comment.