Exemplo n.º 1
0
        public override IPublisher <int?> CreatePublisher(long elements)
        {
            var actorRef =
                System.ActorOf(Props.Create(() => new TestPublisher(elements)).WithDispatcher("akka.test.stream-dispatcher"));

            return(ActorPublisher.Create <int?>(actorRef));
        }
Exemplo n.º 2
0
        public void ActorPublisher_should_signal_error()
        {
            var probe    = CreateTestProbe();
            var actorRef = Sys.ActorOf(TestPublisher.Props(probe.Ref));
            var s        = this.CreateManualProbe <string>();

            ActorPublisher.Create <string>(actorRef).Subscribe(s);
            actorRef.Tell(new Err("wrong"));
            s.ExpectSubscription();
            s.ExpectError().Message.Should().Be("wrong");
        }
Exemplo n.º 3
0
        public void ActorPublisher_should_signal_immediate_onComplete()
        {
            var probe    = CreateTestProbe();
            var actorRef = Sys.ActorOf(TestPublisher.Props(probe.Ref));

            actorRef.Tell(Complete.Instance);
            var s = this.CreateManualProbe <string>();

            ActorPublisher.Create <string>(actorRef).Subscribe(s);
            s.ExpectSubscriptionAndComplete();
        }
Exemplo n.º 4
0
        public void ActorPublisher_should_signal_onComplete_when_actor_is_stopped()
        {
            var probe    = CreateTestProbe();
            var actorRef = Sys.ActorOf(TestPublisher.Props(probe.Ref));
            var s        = this.CreateManualProbe <string>();

            ActorPublisher.Create <string>(actorRef).Subscribe(s);
            s.ExpectSubscription();
            actorRef.Tell(PoisonPill.Instance);
            s.ExpectComplete();
        }
Exemplo n.º 5
0
        public void ActorPublisher_should_work_in_a_GraphDsl()
        {
            var materializer = Sys.Materializer();
            var probe1       = CreateTestProbe();
            var probe2       = CreateTestProbe();

            var senderRef1 = ActorOf(Sender.Props);
            var source1    = Source.FromPublisher(ActorPublisher.Create <int>(senderRef1))
                             .MapMaterializedValue(_ => senderRef1);

            var sink1 = Sink.FromSubscriber(ActorSubscriber.Create <string>(ActorOf(Receiver.Props(probe1.Ref))))
                        .MapMaterializedValue(_ => probe1.Ref);
            var sink2 = Sink.ActorSubscriber <string>(Receiver.Props(probe2.Ref))
                        .MapMaterializedValue(_ => probe2.Ref);
            var senderRef2 = RunnableGraph.FromGraph(GraphDsl.Create(
                                                         Source.ActorPublisher <int>(Sender.Props),
                                                         (builder, source2) =>
            {
                var merge = builder.Add(new Merge <int, int>(2));
                var bcast = builder.Add(new Broadcast <string>(2));

                builder.From(source1).To(merge.In(0));
                builder.From(source2.Outlet).To(merge.In(1));

                builder.From(merge.Out).Via(Flow.Create <int>().Select(i => i.ToString())).To(bcast.In);

                builder.From(bcast.Out(0)).Via(Flow.Create <string>().Select(s => s + "mark")).To(sink1);
                builder.From(bcast.Out(1)).To(sink2);

                return(ClosedShape.Instance);
            })).Run(materializer);

            // the scala test is wrong
            const int noOfMessages = 10;

            for (var i = 0; i < noOfMessages; i++)
            {
                senderRef1.Tell(i);
                senderRef2.Tell(i + noOfMessages);
            }

            var probe1Messages = new List <string>(noOfMessages * 2);
            var probe2Messages = new List <string>(noOfMessages * 2);

            for (var i = 0; i < noOfMessages * 2; i++)
            {
                probe1Messages.Add(probe1.ExpectMsg <string>());
                probe2Messages.Add(probe2.ExpectMsg <string>());
            }
            probe1Messages.Should().BeEquivalentTo(Enumerable.Range(0, noOfMessages * 2).Select(i => i + "mark"));
            probe2Messages.Should().BeEquivalentTo(Enumerable.Range(0, noOfMessages * 2).Select(i => i.ToString()));
        }
Exemplo n.º 6
0
        public void ActorPublisher_should_be_able_to_define_a_subscription_timeout_which_is_cancelled_by_the_first_incoming_Subscriber()
        {
            var timeout = TimeSpan.FromMilliseconds(500);
            var sub     = this.CreateManualProbe <int>();

            var pub = ActorPublisher.Create <int>(ActorOf(TimeoutingPublisher.Props(TestActor, timeout)));

            // subscribe right away, should cancel subscription-timeout
            pub.Subscribe(sub);
            sub.ExpectSubscription();

            ExpectNoMsg(TimeSpan.FromSeconds(1));
        }
Exemplo n.º 7
0
        public void ActorPublisher_should_only_allow_one_subscriber()
        {
            var probe    = CreateTestProbe();
            var actorRef = Sys.ActorOf(TestPublisher.Props(probe.Ref));
            var s        = this.CreateManualProbe <string>();

            ActorPublisher.Create <string>(actorRef).Subscribe(s);
            s.ExpectSubscription();
            var s2 = this.CreateManualProbe <string>();

            ActorPublisher.Create <string>(actorRef).Subscribe(s2);
            s2.ExpectSubscriptionAndError().Should().BeOfType <IllegalStateException>();
        }
Exemplo n.º 8
0
        public void ActorPublisher_should_signal_onComplete()
        {
            var probe    = CreateTestProbe();
            var actorRef = Sys.ActorOf(TestPublisher.Props(probe.Ref));
            var s        = this.CreateProbe <string>();

            ActorPublisher.Create <string>(actorRef).Subscribe(s);
            s.Request(3);
            actorRef.Tell(new Produce("elem-1"));
            actorRef.Tell(Complete.Instance);
            s.ExpectNext("elem-1");
            s.ExpectComplete();
        }
Exemplo n.º 9
0
        public void ActorPublisher_should_terminate_after_signalling_OnErrorThenStop()
        {
            var probe    = CreateTestProbe();
            var actorRef = Sys.ActorOf(TestPublisher.Props(probe.Ref));
            var s        = this.CreateManualProbe <string>();

            ActorPublisher.Create <string>(actorRef).Subscribe(s);
            s.ExpectSubscription();
            probe.Watch(actorRef);
            actorRef.Tell(new ErrThenStop("wrong"));
            s.ExpectError().Message.Should().Be("wrong");
            probe.ExpectTerminated(actorRef, TimeSpan.FromSeconds(3));
        }
Exemplo n.º 10
0
        public void ActorPublisher_should_not_terminate_after_signaling_onError()
        {
            var probe    = CreateTestProbe();
            var actorRef = Sys.ActorOf(TestPublisher.Props(probe.Ref));
            var s        = this.CreateManualSubscriberProbe <string>();

            ActorPublisher.Create <string>(actorRef).Subscribe(s);
            s.ExpectSubscription();
            probe.Watch(actorRef);
            actorRef.Tell(new Err("wrong"));
            s.ExpectError().Message.Should().Be("wrong");
            probe.ExpectNoMsg(TimeSpan.FromMilliseconds(200));
        }
Exemplo n.º 11
0
        public void ActorPublisher_should_accumulate_demand()
        {
            var probe    = CreateTestProbe();
            var actorRef = Sys.ActorOf(TestPublisher.Props(probe.Ref));
            var p        = ActorPublisher.Create <string>(actorRef);
            var s        = this.CreateProbe <string>();

            p.Subscribe(s);
            s.Request(2);
            probe.ExpectMsg <TotalDemand>().Elements.Should().Be(2);
            s.Request(3);
            probe.ExpectMsg <TotalDemand>().Elements.Should().Be(5);
            s.Cancel();
        }
Exemplo n.º 12
0
        public void ActorPublisher_should_drop_onNext_elements_after_cancel()
        {
            var probe    = CreateTestProbe();
            var actorRef = Sys.ActorOf(TestPublisher.Props(probe.Ref));
            var p        = ActorPublisher.Create <string>(actorRef);
            var s        = this.CreateProbe <string>();

            p.Subscribe(s);
            s.Request(2);
            actorRef.Tell(new Produce("elem-1"));
            s.Cancel();
            actorRef.Tell(new Produce("elem-2"));
            s.ExpectNext("elem-1");
            s.ExpectNoMsg(TimeSpan.FromMilliseconds(300));
        }
Exemplo n.º 13
0
        public void ActorPublisher_should_terminate_after_signalling_onCompleteThenStop()
        {
            var probe    = CreateTestProbe();
            var actorRef = Sys.ActorOf(TestPublisher.Props(probe.Ref));
            var s        = this.CreateProbe <string>();

            ActorPublisher.Create <string>(actorRef).Subscribe(s);
            var sub = s.ExpectSubscription();

            sub.Request(3);
            probe.ExpectMsg <TotalDemand>().Elements.Should().Be(3);
            probe.Watch(actorRef);
            actorRef.Tell(new Produce("elem-1"));
            actorRef.Tell(CompleteThenStop.Instance);
            s.ExpectNext("elem-1");
            s.ExpectComplete();
            probe.ExpectTerminated(actorRef, TimeSpan.FromSeconds(3));
        }
Exemplo n.º 14
0
        public void ActorPublisher_should_remember_requested_after_restart()
        {
            var probe    = CreateTestProbe();
            var actorRef = Sys.ActorOf(TestPublisher.Props(probe.Ref));
            var p        = ActorPublisher.Create <string>(actorRef);
            var s        = this.CreateProbe <string>();

            p.Subscribe(s);
            s.Request(3);
            probe.ExpectMsg <TotalDemand>().Elements.Should().Be(3);
            actorRef.Tell(new Produce("elem-1"));
            actorRef.Tell(Boom.Instance);
            actorRef.Tell(new Produce("elem-2"));
            s.ExpectNext("elem-1");
            s.ExpectNext("elem-2");
            s.Request(5);
            probe.ExpectMsg <TotalDemand>().Elements.Should().Be(6);
            s.Cancel();
        }
Exemplo n.º 15
0
        public void ActorPublisher_should_be_able_to_define_a_subscription_timeout_after_which_it_should_shut_down()
        {
            var materializer = Sys.Materializer();

            this.AssertAllStagesStopped(() =>
            {
                var timeout = TimeSpan.FromMilliseconds(150);
                var a       = ActorOf(TimeoutingPublisher.Props(TestActor, timeout));
                var pub     = ActorPublisher.Create <int>(a);

                // don't subscribe for `timeout` millis, so it will shut itself down
                ExpectMsg("timed-out");

                // now subscribers will already be rejected, while the actor could perform some clean-up
                var sub = this.CreateManualProbe <int>();
                pub.Subscribe(sub);
                sub.ExpectSubscriptionAndError();

                ExpectMsg("cleaned-up");
                // termination is tiggered by user code
                Watch(a);
                ExpectTerminated(a);
            }, materializer);
        }