Skip to content

Commit

Permalink
add tests for Signal warning log
Browse files Browse the repository at this point in the history
  • Loading branch information
taefi committed Dec 2, 2024
1 parent 8b71cf1 commit 65fc76c
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@

public abstract class Signal<T> {

private static final Logger LOGGER = LoggerFactory.getLogger(Signal.class);

private final ReentrantLock lock = new ReentrantLock();

private final UUID id = UUID.randomUUID();
Expand Down Expand Up @@ -76,7 +74,7 @@ public Flux<ObjectNode> subscribe() {
.onBackpressureBuffer();

return sink.asFlux().doOnSubscribe(ignore -> {
LOGGER.debug("New Flux subscription...");
getLogger().debug("New Flux subscription...");
lock.lock();
try {
var snapshot = createSnapshotEvent();
Expand All @@ -88,7 +86,7 @@ public Flux<ObjectNode> subscribe() {
}).doFinally(ignore -> {
lock.lock();
try {
LOGGER.debug("Unsubscribing from Signal...");
getLogger().debug("Unsubscribing from Signal...");
subscribers.remove(sink);
} finally {
lock.unlock();
Expand Down Expand Up @@ -134,7 +132,7 @@ private void notifySubscribers(ObjectNode processedEvent) {
return;
}
if (StateEvent.isRejected(processedEvent)) {
LOGGER.warn(
getLogger().warn(
"Operation with id '{}' is rejected with validator message: '{}'",
StateEvent.extractId(processedEvent),
StateEvent.extractValidationError(processedEvent));
Expand All @@ -143,7 +141,7 @@ private void notifySubscribers(ObjectNode processedEvent) {
subscribers.removeIf(sink -> {
boolean failure = sink.tryEmitNext(processedEvent).isFailure();
if (failure) {
LOGGER.debug("Failed push");
getLogger().debug("Failed push");
}
return failure;
});
Expand Down Expand Up @@ -210,4 +208,8 @@ public int hashCode() {
public static void setMapper(ObjectMapper mapper) {
StateEvent.setMapper(mapper);
}

private Logger getLogger() {
return LoggerFactory.getLogger(Signal.class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@

import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.mockito.MockedStatic;
import org.mockito.Mockito;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import reactor.core.publisher.Flux;

Expand All @@ -28,6 +31,10 @@
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertThrows;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mockStatic;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

public class ValueSignalTest {

Expand Down Expand Up @@ -480,6 +487,28 @@ public void readonlyInstance_doesNotAllowAnyModifications() {
assertEquals("Foo", readonlySignal.getValue());
}

@Test
public void submit_eventThatIsRejected_logsTheValidationError() {
Logger logger = Mockito.mock(Logger.class);
try (MockedStatic<LoggerFactory> mockedLoggerFactory = mockStatic(
LoggerFactory.class)) {

mockedLoggerFactory
.when(() -> LoggerFactory.getLogger(Signal.class))
.thenReturn(logger);

var signal = new ValueSignal<>("Foo", String.class)
.withOperationValidator(op -> ValidationResult
.reject("No changes allowed"));
var operation = createSetEvent("Bar");
signal.submit(operation);

verify(logger, times(1)).warn(
"Operation with id '{}' is rejected with validator message: '{}'",
StateEvent.extractId(operation), "No changes allowed");
}
}

private <T> ObjectNode createSetEvent(T value) {
var setEvent = new StateEvent<>(UUID.randomUUID().toString(),
StateEvent.EventType.SET, value);
Expand Down

0 comments on commit 65fc76c

Please sign in to comment.