Skip to content

Commit

Permalink
fixed ConnectionActorTest
Browse files Browse the repository at this point in the history
Signed-off-by: Thomas Jaeckle <thomas.jaeckle@bosch-si.com>
  • Loading branch information
thjaeckle committed Feb 14, 2019
1 parent 059a529 commit 66ffc4f
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
Expand Up @@ -58,9 +58,11 @@
import akka.actor.AbstractActor;
import akka.actor.ActorRef;
import akka.actor.ActorSystem;
import akka.actor.PoisonPill;
import akka.actor.Props;
import akka.actor.Status;
import akka.cluster.pubsub.DistributedPubSub;
import akka.cluster.sharding.ShardRegion;
import akka.japi.Creator;
import akka.testkit.TestProbe;
import akka.testkit.javadsl.TestKit;
Expand Down Expand Up @@ -448,6 +450,9 @@ public void exceptionDuringClientActorPropsCreation() {
final Exception exception = parent.expectMsgClass(ConnectionConfigurationInvalidException.class);
assertThat(exception).hasMessageContaining("validation failed...");

parent.expectMsgClass(ShardRegion.Passivate.class);
parent.send(parent.getLastSender(), PoisonPill.getInstance());

// expect the connection actor is terminated
expectTerminated(connectionActorRef);
}};
Expand Down Expand Up @@ -479,6 +484,9 @@ public void exceptionDueToCustomValidator() {
parent.expectMsgClass(ConnectionUnavailableException.class);
assertThat(exception).hasMessageContaining("not valid");

parent.expectMsgClass(ShardRegion.Passivate.class);
parent.send(parent.getLastSender(), PoisonPill.getInstance());

// expect the connection actor is terminated
expectTerminated(connectionActorRef);
}};
Expand Down
Expand Up @@ -82,7 +82,9 @@
import akka.actor.ActorRef;
import akka.actor.ActorSystem;
import akka.actor.InvalidActorNameException;
import akka.actor.PoisonPill;
import akka.actor.Props;
import akka.cluster.sharding.ShardRegion;
import akka.event.DiagnosticLoggingAdapter;
import akka.event.Logging;
import akka.japi.Creator;
Expand Down Expand Up @@ -417,15 +419,17 @@ static ActorRef createConnectionSupervisorActor(final String connectionId, final
final Duration minBackoff = Duration.ofSeconds(1);
final Duration maxBackoff = Duration.ofSeconds(5);
final Double randomFactor = 1.0;

final Props props = ConnectionSupervisorActor.props(minBackoff, maxBackoff, randomFactor, pubSubMediator,
conciergeForwarder, clientActorPropsFactory, null);
final Props shardRegionMockProps = Props.create(ShardRegionMockActor.class, props, connectionId);

final int maxAttemps = 5;
final long backoffMs = 1000L;

for (int attempt = 1; ; ++attempt) {
try {
return actorSystem.actorOf(props, connectionId);
return actorSystem.actorOf(shardRegionMockProps, "shardRegionMock-" + connectionId);
} catch (final InvalidActorNameException invalidActorNameException) {
if (attempt >= maxAttemps) {
throw invalidActorNameException;
Expand All @@ -436,6 +440,26 @@ static ActorRef createConnectionSupervisorActor(final String connectionId, final
}
}

static class ShardRegionMockActor extends AbstractActor {

private final ActorRef child;

private ShardRegionMockActor(final Props childActorProps, final String childName) {
child = getContext().actorOf(childActorProps, childName);
}

@Override
public Receive createReceive() {
return receiveBuilder()
.match(ShardRegion.Passivate.class, passivate -> {
getSender().tell(PoisonPill.getInstance(), getSelf());
getContext().stop(getSelf());
})
.matchAny(m -> child.forward(m, getContext()))
.build();
}
}

public static ThingModifiedEvent thingModified(final Collection<String> readSubjects) {
final DittoHeaders dittoHeaders = DittoHeaders.newBuilder().readSubjects(readSubjects).build();
return ThingModified.of(Things.THING, 1, dittoHeaders);
Expand Down

0 comments on commit 66ffc4f

Please sign in to comment.