Add failFollowup

Signed-off-by: TheMode <themode@outlook.fr>
This commit is contained in:
TheMode 2022-01-31 12:35:41 +01:00
parent 5b699e0375
commit 59fc49d137
2 changed files with 17 additions and 10 deletions

View File

@ -12,6 +12,7 @@ import java.util.concurrent.CopyOnWriteArrayList;
import java.util.function.Consumer; import java.util.function.Consumer;
import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
final class EnvImpl implements Env { final class EnvImpl implements Env {
private final ServerProcess process; private final ServerProcess process;
@ -80,7 +81,17 @@ final class EnvImpl implements Env {
} }
@Override @Override
public void setHandler(@NotNull Consumer<E> handler) { public void followup(@NotNull Consumer<E> handler) {
updateHandler(handler);
}
@Override
public void failFollowup() {
updateHandler(e -> fail("Event " + e.getClass().getSimpleName() + " was not expected"));
}
void updateHandler(@NotNull Consumer<E> handler) {
check();
this.initialized = true; this.initialized = true;
this.called = false; this.called = false;
this.handler = e -> { this.handler = e -> {
@ -89,12 +100,6 @@ final class EnvImpl implements Env {
}; };
} }
@Override
public void followup(@NotNull Consumer<E> handler) {
check();
setHandler(handler);
}
void check() { void check() {
assertTrue(!initialized || called, "Last listener has not been called: " + eventType.getSimpleName()); assertTrue(!initialized || called, "Last listener has not been called: " + eventType.getSimpleName());
} }

View File

@ -6,11 +6,13 @@ import org.jetbrains.annotations.NotNull;
import java.util.function.Consumer; import java.util.function.Consumer;
public interface FlexibleListener<E extends Event> { public interface FlexibleListener<E extends Event> {
void setHandler(@NotNull Consumer<E> handler);
/** /**
* Updates the handler. Fails if the previous followup has not been called. * Updates the handler. Fails if the previous followup has not been called.
*/ */
void followup(@NotNull Consumer<E> handler); void followup(@NotNull Consumer<E> handler);
/**
* Fails if an event is received. Valid until the next followup call.
*/
void failFollowup();
} }