Exemple #1
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));
        }
        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.CreateSubscriberProbe <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();
        }
        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.CreateSubscriberProbe <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));
        }
        public void ActorPublisher_should_only_allow_one_subscriber()
        {
            var probe    = CreateTestProbe();
            var actorRef = Sys.ActorOf(TestPublisher.Props(probe.Ref));
            var s        = this.CreateManualSubscriberProbe <string>();

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

            ActorPublisher.Create <string>(actorRef).Subscribe(s2);
            s2.ExpectSubscriptionAndError()
            .Should()
            .BeOfType <IllegalStateException>()
            .Which.Message.Should()
            .Be($"ActorPublisher {ReactiveStreamsCompliance.SupportsOnlyASingleSubscriber}");
        }
        public void ActorPublisher_should_terminate_after_signalling_onCompleteThenStop()
        {
            var probe    = CreateTestProbe();
            var actorRef = Sys.ActorOf(TestPublisher.Props(probe.Ref));
            var s        = this.CreateSubscriberProbe <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));
        }
        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.CreateSubscriberProbe <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();
        }
        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.CreateManualSubscriberProbe <int>();
                pub.Subscribe(sub);
                sub.ExpectSubscriptionAndError();

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