#17717 handle OnComplete in ActorSubscriber doc samples
This commit is contained in:
parent
0e60020c58
commit
1e85e1720c
2 changed files with 43 additions and 20 deletions
|
|
@ -26,8 +26,10 @@ import docs.AbstractJavaTest;
|
|||
import org.junit.AfterClass;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
import scala.concurrent.duration.Duration;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
|
|
@ -173,10 +175,18 @@ public class ActorSubscriberDocTest extends AbstractJavaTest {
|
|||
|
||||
router.route(WorkerPoolProtocol.work(msg.id), self());
|
||||
}).
|
||||
match(ActorSubscriberMessage.onCompleteInstance().getClass(), complete -> {
|
||||
if (queue.isEmpty()) {
|
||||
context().stop(self());
|
||||
}
|
||||
}).
|
||||
match(WorkerPoolProtocol.Reply.class, reply -> {
|
||||
int id = reply.id;
|
||||
queue.get(id).tell(WorkerPoolProtocol.done(id), self());
|
||||
queue.remove(id);
|
||||
if (canceled() && queue.isEmpty()) {
|
||||
context().stop(self());
|
||||
}
|
||||
}).
|
||||
build());
|
||||
}
|
||||
|
|
@ -207,11 +217,13 @@ public class ActorSubscriberDocTest extends AbstractJavaTest {
|
|||
data.add(i);
|
||||
}
|
||||
|
||||
Source.from(data)
|
||||
final ActorRef worker = Source.from(data)
|
||||
.map(i -> WorkerPoolProtocol.msg(i, replyTo))
|
||||
.runWith(Sink.<WorkerPoolProtocol.Msg>actorSubscriber(WorkerPool.props()), mat);
|
||||
//#actor-subscriber-usage
|
||||
|
||||
watch(worker);
|
||||
|
||||
List<Object> got = Arrays.asList(receiveN(N));
|
||||
Collections.sort(got, new Comparator<Object>() {
|
||||
@Override
|
||||
|
|
@ -226,6 +238,7 @@ public class ActorSubscriberDocTest extends AbstractJavaTest {
|
|||
assertEquals(String.format("Expected %d, but got %s", i, got.get(i)), WorkerPoolProtocol.done(i), got.get(i));
|
||||
}
|
||||
assertEquals(String.format("Expected 117 messages but got %d", i), i, 117);
|
||||
expectTerminated(Duration.create(10, TimeUnit.SECONDS), worker);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
|
|
|||
|
|
@ -16,6 +16,7 @@ import akka.stream.actor.MaxInFlightRequestStrategy
|
|||
import akka.stream.scaladsl.Sink
|
||||
import akka.stream.scaladsl.Source
|
||||
import akka.testkit.AkkaSpec
|
||||
import scala.concurrent.duration._
|
||||
|
||||
object ActorSubscriberDocSpec {
|
||||
//#worker-pool
|
||||
|
|
@ -54,6 +55,13 @@ object ActorSubscriberDocSpec {
|
|||
case Reply(id) =>
|
||||
queue(id) ! Done(id)
|
||||
queue -= id
|
||||
if (canceled && queue.isEmpty) {
|
||||
context.stop(self)
|
||||
}
|
||||
case OnComplete =>
|
||||
if (queue.isEmpty) {
|
||||
context.stop(self)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -79,11 +87,13 @@ class ActorSubscriberDocSpec extends AkkaSpec {
|
|||
|
||||
//#actor-subscriber-usage
|
||||
val N = 117
|
||||
Source(1 to N).map(WorkerPool.Msg(_, replyTo))
|
||||
val worker = Source(1 to N).map(WorkerPool.Msg(_, replyTo))
|
||||
.runWith(Sink.actorSubscriber(WorkerPool.props))
|
||||
//#actor-subscriber-usage
|
||||
|
||||
watch(worker)
|
||||
receiveN(N).toSet should be((1 to N).map(WorkerPool.Done).toSet)
|
||||
expectTerminated(worker, 10.seconds)
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue