Skip to content

Commit

Permalink
Review: Improved concurrency semantics in test
Browse files Browse the repository at this point in the history
  • Loading branch information
olaf-otto committed Jun 26, 2024
1 parent 0e4f79b commit 0011399
Showing 1 changed file with 14 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,7 @@
*/
package io.neba.core.resourcemodels.registration;

import java.lang.reflect.Field;
import java.util.Dictionary;
import java.util.Hashtable;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.TimeUnit;
import io.neba.core.Eventual;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
Expand All @@ -29,26 +25,21 @@
import org.osgi.service.event.Event;
import org.slf4j.Logger;

import java.lang.reflect.Field;
import java.util.Dictionary;
import java.util.Hashtable;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.TimeUnit;

import static java.util.concurrent.TimeUnit.SECONDS;
import static org.apache.commons.lang3.reflect.FieldUtils.getField;
import static org.apache.commons.lang3.reflect.FieldUtils.writeField;
import static org.mockito.Mockito.anyLong;
import static org.mockito.Mockito.atLeast;
import static org.mockito.Mockito.atMost;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.eq;
import static org.mockito.Mockito.isA;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.*;

/**
* @author Olaf Otto
*/
@RunWith(MockitoJUnitRunner.class)
public class MappableTypeHierarchyChangeListenerTest {
public class MappableTypeHierarchyChangeListenerTest implements Eventual {
@Mock
private ModelRegistry modelRegistry;

Expand All @@ -70,19 +61,17 @@ public void testChangeListenerShutsDownExecutorUponDeactivation() throws Excepti
deactivate();

withChangeOn("/apps/testapp/components/test");
sleep();

verifyModelRegistryCacheIsNotCleared();
eventually(this::verifyModelRegistryCacheIsNotCleared);
}

@Test
public void testActivateListenerClearsModelRegistryUponEvent() throws Exception {
activate();

withChangeOn("/apps/testapp/components/test");
sleep();

verifyModelRegistryCacheIsCleared();
eventually(this::verifyModelRegistryCacheIsCleared);
}

/**
Expand All @@ -98,9 +87,8 @@ public void testOnlyOneEventIsAccepted() throws Exception {
withChangeOn("/apps/testapp/components/test");

activate();
sleep();

verifyModelRegistryCacheIsCleared();
eventually(this::verifyModelRegistryCacheIsCleared);
}

@Test
Expand All @@ -110,11 +98,9 @@ public void testLoggingOfInvalidatingChangeWhenLogLevelIsTrace() throws Exceptio
withTraceLogging();
withChangeOn("/apps/testapp/components/test");

sleep();

verifyLoggerTraces(
eventually(() -> verifyLoggerTraces(
"Invalidating the resource model registry lookup cache due to changes to {}.",
"/apps/testapp/components/test");
"/apps/testapp/components/test"));
}

@Test
Expand All @@ -124,9 +110,7 @@ public void testLoggingOfInterruptionWithoutShutdown() throws Exception {

withChangeOn("/some/path");

sleep();

verifyLoggerDebugs("The type hierarchy change listener got interrupted, but was not shut down.");
eventually(() -> verifyLoggerDebugs("The type hierarchy change listener got interrupted, but was not shut down."));
}

private void verifyLoggerDebugs(String message) {
Expand All @@ -152,10 +136,6 @@ private void withTraceLogging() {
doReturn(true).when(this.logger).isTraceEnabled();
}

private void verifyModelRegistryCacheIsClearedAtMost(int times) {
verify(this.modelRegistry, atMost(times)).clearLookupCaches();
}

private void verifyModelRegistryCacheIsCleared() {
verify(this.modelRegistry).clearLookupCaches();
}
Expand All @@ -168,10 +148,6 @@ private void verifyModelRegistryCacheIsNotCleared() {
verify(this.modelRegistry, never()).clearLookupCaches();
}

private void sleep() throws InterruptedException {
Thread.sleep(SECONDS.toMillis(2));
}

private void withChangeOn(String path) {
Dictionary<String, Object> properties = new Hashtable<>();
properties.put("path", path);
Expand Down

0 comments on commit 0011399

Please sign in to comment.