+ @SuppressWarnings("checkstyle:IllegalCatch")
+ private void verifyActorReady(final ActorRef actorRef) {
+ // Sometimes we see messages go to dead letters soon after creation - it seems the actor isn't quite
+ // in a state yet to receive messages or isn't actually created yet. This seems to happen with
+ // actorSelection so, to alleviate it, we use an actorSelection and send an Identify message with
+ // retries to ensure it's ready.
+
+ Timeout timeout = new Timeout(100, TimeUnit.MILLISECONDS);
+ Throwable lastError = null;
+ Stopwatch sw = Stopwatch.createStarted();
+ while (sw.elapsed(TimeUnit.SECONDS) <= 10) {
+ try {
+ ActorSelection actorSelection = system.actorSelection(actorRef.path().toString());
+ Future<Object> future = Patterns.ask(actorSelection, new Identify(""), timeout);
+ ActorIdentity reply = (ActorIdentity)Await.result(future, timeout.duration());
+ assertTrue("Identify returned non-present", reply.getActorRef().isPresent());
+ return;
+ } catch (Exception | AssertionError e) {
+ Uninterruptibles.sleepUninterruptibly(100, TimeUnit.MILLISECONDS);
+ lastError = e;
+ }
+ }
+
+ throw new RuntimeException(lastError);
+ }
+