Skip to content

Commit

Permalink
ActorSystemResource now can receive an actor system name.
Browse files Browse the repository at this point in the history
This is required as some method names interfere with actor ref paths which could break some test cases.

Furthermore, fixed unit tests.

Signed-off-by: Juergen Fickel <juergen.fickel@bosch.io>
  • Loading branch information
Juergen Fickel committed Oct 17, 2022
1 parent 28c019c commit fffe8d4
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,10 @@ public abstract class AbstractPublisherActorTest {
protected static final String DEVICE_ID = "ditto:thing";

@Rule
public final ActorSystemResource actorSystemResource = ActorSystemResource.newInstance(CONFIG);
public final ActorSystemResource actorSystemResource = ActorSystemResource.newInstance(
getClass().getSimpleName(),
CONFIG
);

protected ActorSystem actorSystem;
protected TestProbe proxyActorTestProbe;
Expand Down Expand Up @@ -104,27 +107,25 @@ public void testPublishMessage() throws Exception {

@Test
public void testAutoAck() throws Exception {
new TestKit(actorSystem) {{

final TestProbe probe = new TestProbe(actorSystem);
setupMocks(probe);
final OutboundSignal.MultiMapped multiMapped = OutboundSignalFactory.newMultiMappedOutboundSignal(
List.of(getMockOutboundSignalWithAutoAck("please-verify",
DittoHeaderDefinition.DITTO_ACKREGATOR_ADDRESS.getKey(), getRef().path().toSerializationFormat())),
getRef()
);

final Props props = getPublisherActorProps();
final ActorRef publisherActor = childActorOf(props);

publisherCreated(this, publisherActor);

verifyAcknowledgements(() -> {
publisherActor.tell(multiMapped, getRef());
return expectMsgClass(Acknowledgements.class);
});
}};

setupMocks(actorSystemResource.newTestProbe());
final var sender = actorSystemResource.newTestKit();
final var multiMapped = OutboundSignalFactory.newMultiMappedOutboundSignal(
List.of(
getMockOutboundSignalWithAutoAck("please-verify",
DittoHeaderDefinition.DITTO_ACKREGATOR_ADDRESS.getKey(),
sender.getRef().path().toSerializationFormat())
),
sender.getRef()
);

final var publisherActor = sender.childActorOf(getPublisherActorProps());

publisherCreated(sender, publisherActor);

verifyAcknowledgements(() -> {
publisherActor.tell(multiMapped, sender.getRef());
return sender.expectMsgClass(Acknowledgements.class);
});
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,12 @@
import org.eclipse.ditto.connectivity.model.signals.events.ConnectivityEvent;
import org.eclipse.ditto.connectivity.service.messaging.persistence.ConnectionMongoSnapshotAdapter;
import org.eclipse.ditto.internal.utils.akka.PingCommand;
import org.eclipse.ditto.internal.utils.tracing.DittoTracingInitResource;
import org.eclipse.ditto.json.JsonValue;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.ClassRule;
import org.junit.Test;

import com.typesafe.config.ConfigValueFactory;
Expand All @@ -58,6 +60,10 @@
*/
public final class ConnectionPersistenceActorRecoveryTest extends WithMockServers {

@ClassRule
public static final DittoTracingInitResource DITTO_TRACING_INIT_RESOURCE =
DittoTracingInitResource.disableDittoTracing();

private static final String PERSISTENCE_ID_PREFIX = "connection:";
private static final String JOURNAL_PLUGIN_ID = "akka-contrib-mongodb-persistence-connection-journal";
private static final String SNAPSHOT_PLUGIN_ID = "akka-contrib-mongodb-persistence-connection-snapshots";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,10 @@ public final class OutboundMappingProcessorActorTest {
private static final Connection CONNECTION = createTestConnection();

@Rule
public final ActorSystemResource actorSystemResource = ActorSystemResource.newInstance(TestConstants.CONFIG);
public final ActorSystemResource actorSystemResource = ActorSystemResource.newInstance(
getClass().getSimpleName(),
TestConstants.CONFIG
);

private ProtocolAdapterProvider protocolAdapterProvider;
private TestProbe clientActorProbe;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,14 @@

import static org.eclipse.ditto.base.model.common.ConditionChecker.argumentNotEmpty;
import static org.eclipse.ditto.base.model.common.ConditionChecker.argumentNotNull;
import static org.eclipse.ditto.base.model.common.ConditionChecker.checkNotNull;

import java.text.MessageFormat;
import java.util.concurrent.TimeUnit;

import javax.annotation.Nullable;
import javax.annotation.concurrent.NotThreadSafe;

import org.eclipse.ditto.base.model.common.ConditionChecker;
import org.junit.rules.ExternalResource;
import org.junit.runner.Description;
import org.junit.runners.model.Statement;
Expand All @@ -47,9 +48,9 @@ public final class ActorSystemResource extends ExternalResource {
private ActorSystem actorSystem;
private Materializer materializer;

private ActorSystemResource(final Config config) {
private ActorSystemResource(@Nullable final CharSequence actorSystemName, final Config config) {
this.config = config;
actorSystemName = null;
this.actorSystemName = null != actorSystemName ? actorSystemName.toString() : null;
actorSystem = null;
materializer = null;
}
Expand All @@ -60,7 +61,21 @@ private ActorSystemResource(final Config config) {
* @return the instance.
*/
public static ActorSystemResource newInstance() {
return new ActorSystemResource(ConfigFactory.empty());
return new ActorSystemResource(null, ConfigFactory.empty());
}

/**
* Returns a new instance of {@code ActorSystemResource}.
*
* @param actorSystemName the name of the actor system.
* @return the instance.
* @throws NullPointerException if {@code actorSystemName} is {@code null}.
*/
public static ActorSystemResource newInstance(final CharSequence actorSystemName) {
return new ActorSystemResource(
checkNotNull(actorSystemName, "actorSystemName"),
ConfigFactory.empty()
);
}

/**
Expand All @@ -71,7 +86,21 @@ public static ActorSystemResource newInstance() {
* @throws NullPointerException if {@code config} is {@code null}.
*/
public static ActorSystemResource newInstance(final Config config) {
return new ActorSystemResource(ConditionChecker.checkNotNull(config, "config"));
return new ActorSystemResource(null, checkNotNull(config, "config"));
}

/**
* Returns a new instance of {@code ActorSystemResource}.
*
* @param config the config to be used for creating the {@code ActorSystem}.
* @return the instance.
* @throws NullPointerException if {@code config} is {@code null}.
*/
public static ActorSystemResource newInstance(final CharSequence actorSystemName, final Config config) {
return new ActorSystemResource(
checkNotNull(actorSystemName, "actorSystemName"),
checkNotNull(config, "config")
);
}

@Override
Expand All @@ -81,16 +110,19 @@ public Statement apply(final Statement base, final Description description) {
return super.apply(base, description);
}

private static String getActorSystemName(final Description description) {
private String getActorSystemName(final Description description) {
final String result;
final var className = description.getTestClass().getSimpleName();
final var methodName = description.getMethodName();
if (null != methodName) {
result = MessageFormat.format("{0}_{1}", className, methodName);
if (null != actorSystemName) {
result = actorSystemName;
} else {
result = className;
final var className = description.getTestClass().getSimpleName();
final var methodName = description.getMethodName();
if (null != methodName) {
result = MessageFormat.format("{0}_{1}", className, methodName);
} else {
result = className;
}
}

return result;
}

Expand Down Expand Up @@ -133,22 +165,22 @@ public TestProbe newTestProbe(final String name) {

public ActorRef newActor(final Props props) {
final var actorSystem = getActorSystem();
ConditionChecker.checkNotNull(props, "props");
checkNotNull(props, "props");

return actorSystem.actorOf(props);
}

public ActorRef newActor(final Props props, final CharSequence actorName) {
final var actorSystem = getActorSystem();
ConditionChecker.checkNotNull(props, "props");
checkNotNull(props, "props");
argumentNotEmpty(actorName, "actorName");

return actorSystem.actorOf(props, actorName.toString());
}

public void stopActor(final ActorRef actorRef) {
final var actorSystem = getActorSystem();
actorSystem.stop(ConditionChecker.checkNotNull(actorRef, "actorRef"));
actorSystem.stop(checkNotNull(actorRef, "actorRef"));
}

@Override
Expand Down

0 comments on commit fffe8d4

Please sign in to comment.