public void SameAsRetry_OnCompleted()
        {
            var scheduler = new TestScheduler();
            var source = scheduler.CreateHotObservable<int>(
                OnNext(0, 1),
                OnNext(0, 2),
                OnCompleted<int>(0),
                OnNext(0, 3));

            var called = 0;
            var retryRecorder = scheduler.CreateObserver<int>();
            source.Retry().Subscribe(retryRecorder);
            var onErrorRecorder = scheduler.CreateObserver<int>();
            source.OnErrorRetry((Exception e) => { ++called; }).Subscribe(onErrorRecorder);

            scheduler.Start();

            retryRecorder.Messages.Is(
                OnNext(1, 1),
                OnNext(1, 2),
                OnCompleted<int>(1));

            onErrorRecorder.Messages.Is(retryRecorder.Messages);
            called.Is(0);
        }
        public void Test_Select_Success()
        {
            var scheduler = new TestScheduler();

            // Create an observable, remember subscribe happens at 200
            var obs = scheduler.CreateHotObservable<string>(
                OnNext(150, "Erik"),
                OnNext(210, "Bart"),
                OnNext(220, "Matthew"),
                OnCompleted<string>(230)
            );

            // Start our virtual time scheduler
            var res = scheduler.Start(() =>
                {
                    return obs.Select(x => x.Length);
                });

            // Check the messages that came through at a specific time
            res.Messages.AssertEqual(
                OnNext(210, 4),
                OnNext(220, 7),
                OnCompleted<int>(230)
            );

            // Check for when subscribe and unsubscribe
            obs.Subscriptions.AssertEqual(
                Subscribe(200, 230)
            );
        }
        public void FilterBursts_TwoBurstAndGapInEachBurst_FirstInEachBurstEmitted()
        {
            var scheduler = new TestScheduler();
            var xs = scheduler.CreateHotObservable(
                OnNext(250, 1),
                OnNext(260, 2),
                OnNext(270, 3),

                OnNext(400, -1),
                OnNext(401, -2),
                OnNext(405, -3),

                OnCompleted<int>(500)
                );

            var res = scheduler.Start(() => xs.FilterBursts(3));

            res.Messages.AssertEqual(
                OnNext(250, 1),
                OnNext(400, -1),
                OnCompleted<int>(500));

            xs.Subscriptions.AssertEqual(
                Subscribe(Subscribed, 500));
        }
        public void InputHasADelayBetweenCharactersOfLongerThanFiveSecondsDuringSequence()
        {
            var scheduler = new TestScheduler();
            var inputSequence = scheduler.CreateHotObservable(
                OnNextForAll(1.0.Seconds(), "a^hello"),
                OnNextForAll(6.1.Seconds(), "world$"),
                OnNextForAll(7.0.Seconds(), "^Rx$"),
                OnCompleted<char>(7.5.Seconds())
                );

            var results = scheduler.Run(() => inputSequence.ToBarcodeReadings(scheduler),
                0.0.Seconds().Ticks,
                0.5.Seconds().Ticks,
                10.Seconds().Ticks
                );

            results.AssertEqual(EnumerableEx.Concat(
                OnNext(7.Seconds(), "Rx"),
                OnCompleted<string>(7.5.Seconds())
                ));

            inputSequence.Subscriptions.AssertEqual(
                Subscribe(0.5.Seconds(), 7.5.Seconds())
                );
        }
Esempio n. 5
0
        public void TimeSource101()
        {
            TestScheduler scheduler = new TestScheduler();
            var xs = scheduler.CreateHotObservable(s_testData);

            var timeSource = new TimeSource<long>(xs, x => new DateTimeOffset(x, TimeSpan.Zero));
            timeSource.StartTime = new DateTimeOffset(101, TimeSpan.Zero);

            var counts = from window in timeSource.Window(TimeSpan.FromTicks(5), timeSource.Scheduler)
                         from Count in window.Count()
                         select Count;

            var list = new List<Timestamped<int>>();
            counts
                .Timestamp(timeSource.Scheduler)
                .Subscribe(ts => list.Add(ts));

            timeSource.Connect();
            scheduler.AdvanceTo(120);

            Assert.AreEqual(3, list.Count);
            list.AssertEqual(
                Result(106, 2),
                Result(111, 3),
                Result(115, 1));
        }
        public void WhenAHandleToTheFileCannotBeObtainedCallFailureMethod()
        {
            //Arrange
            var testFolder = Environment.CurrentDirectory;
            var scheduler = new TestScheduler();
            var fileHandlerMock = new Mock<IFileHandler>();
            var monitorMock = new Mock<FolderMonitor>(testFolder, fileHandlerMock.Object) { CallBase = true };

            var service = monitorMock.Object;
            service.Interval = TimeSpan.FromSeconds(1);
            service.RetryCount = 3;
            service.WatcherObservable = scheduler
                .CreateHotObservable(
                    OnNext(10, new EventPattern<FileSystemEventArgs>(
                                   null,
                                   new FileSystemEventArgs(WatcherChangeTypes.Created, testFolder, "test.file"))));
            service.RetryScheduler = scheduler;
            service.StartMonitoring();

            //Act
            scheduler.AdvanceBy(TimeSpan.FromMinutes(1).Ticks);
            
            //Assert
            fileHandlerMock.Verify(
                handler =>
                handler.ReportError(It.Is<string>(v => v == Path.Combine(testFolder, "test.file")),
                                    It.Is<Exception>(e => e is FileNotFoundException)));
        }
Esempio n. 7
0
        public void ConnectableObservable_Connected()
        {
            var scheduler = new TestScheduler();

            var xs = scheduler.CreateHotObservable<int>(
                OnNext(210, 1),
                OnNext(220, 2),
                OnNext(230, 3),
                OnNext(240, 4),
                OnCompleted<int>(250)
            );

            var subject = new MySubject();

            var conn = new ConnectableObservable<int>(xs, subject);
            var disconnect = conn.Connect();

            var res = scheduler.Start(() => conn);

            res.Messages.AssertEqual(
                OnNext(210, 1),
                OnNext(220, 2),
                OnNext(230, 3),
                OnNext(240, 4),
                OnCompleted<int>(250)
            );
        }
Esempio n. 8
0
        public void Bug_1261()
        {
            var scheduler = new TestScheduler();

            var xs = scheduler.CreateHotObservable(
                OnNext(205, 1),
                OnNext(210, 2),
                OnNext(215, 3),
                OnNext(220, 4),
                OnNext(225, 5),
                OnNext(230, 6),
                OnCompleted<int>(230));

            var results = scheduler.Start(() =>
                xs.Window(TimeSpan.FromTicks(10), scheduler).Select((x, i) => x.Select(y => i.ToString() + " " + y.ToString()).Concat(Observable.Return(i.ToString() + " end", scheduler))).Merge()
            );

            results.Messages.AssertEqual(
                OnNext(205, "0 1"),
                OnNext(210, "0 2"),
                OnNext(211, "0 end"),
                OnNext(215, "1 3"),
                OnNext(220, "1 4"),
                OnNext(221, "1 end"),
                OnNext(225, "2 5"),
                OnNext(230, "2 6"),
                OnNext(231, "2 end"),
                OnCompleted<string>(231)
            );
        }
Esempio n. 9
0
        public void Bug_1283()
        {
            var scheduler = new TestScheduler();

            var xs = scheduler.CreateHotObservable(
                OnNext(100, 1),
                OnNext(220, 2),
                OnNext(240, 3),
                OnNext(300, 4),
                OnNext(310, 5),
                OnCompleted<int>(350)
            );

            var results = scheduler.Start(() =>
                xs.Window(TimeSpan.FromTicks(100), scheduler).Select((x, i) => x.Select(y => i.ToString() + " " + y.ToString()).Concat(Observable.Return(i.ToString() + " end", scheduler))).Merge()
            );

            results.Messages.AssertEqual(
                OnNext(220, "0 2"),
                OnNext(240, "0 3"),
                OnNext(300, "0 4"),
                OnNext(301, "0 end"),
                OnNext(310, "1 5"),
                OnNext(351, "1 end"),
                OnCompleted<string>(351)
            );
        }
        public void FilterBurstsInHotObservable()
        {
            var scheduler = new TestScheduler();
            var xs = scheduler.CreateHotObservable(
                OnNext(250, 1),
                OnNext(258, 2),
                OnNext(262, 3),

                OnNext(450, -1),
                OnNext(451, -2),
                OnNext(460, -3),

                OnCompleted<int>(500)
                );

            var res = scheduler.Start(() => xs.FilterBursts(TimeSpan.FromTicks(10),scheduler));

            res.Messages.AssertEqual(
                OnNext(250, 1),
                OnNext(450, -1),
                OnCompleted<int>(500));

            xs.Subscriptions.AssertEqual(
                Subscribe(Subscribed, 500));
        }
Esempio n. 11
0
        public void Await()
        {
            var scheduler = new TestScheduler();

            var xs = scheduler.CreateHotObservable(
                OnNext(20, -1),
                OnNext(150, 0),
                OnNext(220, 1),
                OnNext(290, 2),
                OnNext(340, 3),
                OnCompleted<int>(410)
            );

            var awaiter = default(AsyncSubject<int>);
            var result = default(int);
            var t = long.MaxValue;

            scheduler.ScheduleAbsolute(100, () => awaiter = xs.GetAwaiter());
            scheduler.ScheduleAbsolute(200, () => awaiter.OnCompleted(() => { t = scheduler.Clock; result = awaiter.GetResult(); }));

            scheduler.Start();

            Assert.AreEqual(410, t);
            Assert.AreEqual(3, result);

            xs.Subscriptions.AssertEqual(
                Subscribe(100)
            );
        }
Esempio n. 12
0
        public void Test_Where_Error()
        {
            var exception = new Exception();
            var scheduler = new TestScheduler();

            var obs = scheduler.CreateHotObservable<string>(
                OnNext(150, "Erik"),
                OnNext(210, "Bart"),
                OnNext(220, "Matthew"),
                OnError<string>(230, exception)
            );

            var res = scheduler.Start(() =>
            {
                return obs.Where(x => x.Length > 4);
            });

            res.Messages.AssertEqual(
                OnNext(220, "Matthew"),
                OnError<string>(230, exception)
            );

            obs.Subscriptions.AssertEqual(
                Subscribe(200, 230)
            );
        }
Esempio n. 13
0
        public void Infinite()
        {
            var scheduler = new TestScheduler();

            var xs = scheduler.CreateHotObservable(
                OnNext(70, 1),
                OnNext(110, 2),
                OnNext(220, 3),
                OnNext(270, 4),
                OnNext(340, 5),
                OnNext(410, 6),
                OnNext(520, 7),
                OnNext(630, 8),
                OnNext(710, 9),
                OnNext(870, 10),
                OnNext(940, 11),
                OnNext(1020, 12)
            );

            var subject = default(AsyncSubject<int>);
            var subscription = default(IDisposable);

            var results1 = scheduler.CreateObserver<int>();
            var subscription1 = default(IDisposable);

            var results2 = scheduler.CreateObserver<int>();
            var subscription2 = default(IDisposable);

            var results3 = scheduler.CreateObserver<int>();
            var subscription3 = default(IDisposable);

            scheduler.ScheduleAbsolute(100, () => subject = new AsyncSubject<int>());
            scheduler.ScheduleAbsolute(200, () => subscription = xs.Subscribe(subject));
            scheduler.ScheduleAbsolute(1000, () => subscription.Dispose());

            scheduler.ScheduleAbsolute(300, () => subscription1 = subject.Subscribe(results1));
            scheduler.ScheduleAbsolute(400, () => subscription2 = subject.Subscribe(results2));
            scheduler.ScheduleAbsolute(900, () => subscription3 = subject.Subscribe(results3));

            scheduler.ScheduleAbsolute(600, () => subscription1.Dispose());
            scheduler.ScheduleAbsolute(700, () => subscription2.Dispose());
            scheduler.ScheduleAbsolute(800, () => subscription1.Dispose());
            scheduler.ScheduleAbsolute(950, () => subscription3.Dispose());

            scheduler.Start();

            results1.Messages.AssertEqual(
            );

            results2.Messages.AssertEqual(
            );

            results3.Messages.AssertEqual(
            );
        }
Esempio n. 14
0
        public void Infinite()
        {
            var scheduler = new TestScheduler();

            var xs = scheduler.CreateHotObservable(
                OnNext(70, 1),
                OnNext(110, 2),
                OnNext(220, 3),
                OnNext(270, 4),
                OnNext(340, 5),
                OnNext(410, 6),
                OnNext(520, 7),
                OnNext(630, 8),
                OnNext(710, 9),
                OnNext(870, 10),
                OnNext(940, 11),
                OnNext(1020, 12)
                );

            var subject = default(AsyncSubject<int>);
            var subscription = default(IDisposable);

            var results1 = new MockObserver<int>(scheduler);
            var subscription1 = default(IDisposable);

            var results2 = new MockObserver<int>(scheduler);
            var subscription2 = default(IDisposable);

            var results3 = new MockObserver<int>(scheduler);
            var subscription3 = default(IDisposable);

            scheduler.Schedule(() => subject = new AsyncSubject<int>(scheduler), 100);
            scheduler.Schedule(() => subscription = xs.Subscribe(subject), 200);
            scheduler.Schedule(() => subscription.Dispose(), 1000);

            scheduler.Schedule(() => subscription1 = subject.Subscribe(results1), 300);
            scheduler.Schedule(() => subscription2 = subject.Subscribe(results2), 400);
            scheduler.Schedule(() => subscription3 = subject.Subscribe(results3), 900);

            scheduler.Schedule(() => subscription1.Dispose(), 600);
            scheduler.Schedule(() => subscription2.Dispose(), 700);
            scheduler.Schedule(() => subscription1.Dispose(), 800);
            scheduler.Schedule(() => subscription3.Dispose(), 950);

            scheduler.Run();

            results1.AssertEqual(
                );

            results2.AssertEqual(
                );

            results3.AssertEqual(
                );
        }
Esempio n. 15
0
    public void CombineLatest()
    {
      var scheduler = new TestScheduler();

      var first = scheduler.CreateHotObservable(
        OnNext(10, 'a'),
        OnNext(20, 'b'),
        OnNext(40, 'c'),
        OnCompleted<char>(70));

      var second = scheduler.CreateHotObservable(
        OnNext(30, 'd'),
        OnNext(60, 'e'),
        OnNext(100, 'f'),
        OnCompleted<char>(110));

      var third = scheduler.CreateHotObservable(
        OnNext(50, 'g'),
        OnNext(80, 'h'),
        OnCompleted<char>(90));

      var input = scheduler.CreateColdObservable(
        OnNext(0, first),
        OnNext(25, second),
        OnNext(45, third),
        OnCompleted<ITestableObservable<char>>(55));

      var result = scheduler.Start(() => input.CombineLatest(), 0, 0, 200).Messages;

      result.AssertEqual(
        OnNext<IList<char>>(10, new[] { 'a' }.SequenceEqual),
        OnNext<IList<char>>(20, new[] { 'b' }.SequenceEqual),
        OnNext<IList<char>>(30, new[] { 'b', 'd' }.SequenceEqual),
        OnNext<IList<char>>(40, new[] { 'c', 'd' }.SequenceEqual),
        OnNext<IList<char>>(50, new[] { 'c', 'd', 'g' }.SequenceEqual),
        OnNext<IList<char>>(60, new[] { 'c', 'e', 'g' }.SequenceEqual),
        OnNext<IList<char>>(80, new[] { 'c', 'e', 'h' }.SequenceEqual),
        OnNext<IList<char>>(100, new[] { 'c', 'f', 'h' }.SequenceEqual),
        OnCompleted<IList<char>>(110));
    }
        public void BurstOverFiveSeconds_TemperatureIsRisky_AlertAtBurstStartAndAfterFiveSeconds()
        {
            var testScheduler = new TestScheduler();
            var oneSecond = TimeSpan.TicksPerSecond;

            var temperatures = testScheduler.CreateHotObservable<double>(
                OnNext(310, 500.0)
            );
            var proximities = testScheduler.CreateHotObservable<Unit>(
                OnNext(100, Unit.Default),
                OnNext(1 * oneSecond-1, Unit.Default),
                OnNext(2 * oneSecond - 1, Unit.Default),
                OnNext(3 * oneSecond - 1, Unit.Default),
                OnNext(4 * oneSecond - 1, Unit.Default),
                OnNext(5 * oneSecond-1, Unit.Default),

                OnNext(6 * oneSecond - 1, Unit.Default)

            );
            var concurrencyProvider = Substitute.For<IConcurrencyProvider>();
            concurrencyProvider.ReturnsForAll<IScheduler>(testScheduler);
            var tempSensor = Substitute.For<ITemperatureSensor>();
            tempSensor.Readings.Returns(temperatures);
            var proxSensor = Substitute.For<IProximitySensor>();
            proxSensor.Readings.Returns(proximities);

            var monitor=new MachineMonitor(concurrencyProvider, tempSensor, proxSensor);

            var res = testScheduler.Start(() => monitor.ObserveAlerts(),
                0,
                0,
                long.MaxValue);

            res.Messages.AssertEqual(
                OnNext(310, (Alert a) => a.Time.Ticks == 310),
                OnNext(6*oneSecond - 1, (Alert a) => true)
            );
        }
Esempio n. 17
0
        public void Then1()
        {
            var scheduler = new TestScheduler();

            var xs = scheduler.CreateHotObservable(
                OnNext(210, 1),
                OnCompleted<int>(220)
            );

            var results = scheduler.Run(() => Observable.Join(xs.Then(a => a))).ToArray();
            results.AssertEqual(
                OnNext(210, 1),
                OnCompleted<int>(220)
            );
        }
Esempio n. 18
0
        public void TargetComponentの入出力チェック()
        {
            var scheduler = new TestScheduler();

            // OutPortから出力されたデータを蓄えるためのもの。
            var recorder = scheduler.CreateObserver<TimedLong>();

            // InPortにデータを入力するためのもの。1秒ごとにデータを送る。
            var publisher = scheduler.CreateHotObservable(
            OnNext(TimeSpan.FromSeconds(1).Ticks, new TimedLong() {Data = 1}),
            OnNext(TimeSpan.FromSeconds(2).Ticks, new TimedLong() {Data = 2}),
            OnNext(TimeSpan.FromSeconds(3).Ticks, new TimedLong() {Data = 3}),
            OnNext(TimeSpan.FromSeconds(4).Ticks, new TimedLong() {Data = 4}),
            OnNext(TimeSpan.FromSeconds(5).Ticks, new TimedLong() {Data = 5})
            );

            var comp = new TargetComponent();

            // コンポーネントのスケジューラをTestSchedulerに差し替える
            comp.ExecutionContextScheduler = scheduler;

            // 入出力ポートに接続
            comp.InPort.Connect(publisher);
            comp.OutPort.Connect(recorder);

            // コンポーネントを活性化
            var retRecorder = scheduler.CreateObserver<ReturnCode_t>();
            comp.ActivateAsync().ToObservable().Subscribe(retRecorder);
            scheduler.AdvanceBy(100);
            retRecorder.Messages.Count.Is(2);
            retRecorder.Messages.First().Value.Value.Is(ReturnCode_t.RTC_OK);

            // 時間を5秒進める
            scheduler.AdvanceBy(TimeSpan.FromSeconds(5).Ticks);

            // 入力データの値が2倍されて出力されていることを確認
            recorder.Messages.Count.Is(5);
            ReactiveAssert.AreElementsEqual(
            recorder.Messages.Select(x => x.Value.Value),
            new[] {
                    new TimedLong() {Data = 2},
                    new TimedLong() {Data = 4},
                    new TimedLong() {Data = 6},
                    new TimedLong() {Data = 8},
                    new TimedLong() {Data = 10}
                });
        }
Esempio n. 19
0
    public void BufferIntrospective()
    {
      var scheduler = new TestScheduler();
      var buffers = new List<IList<int>>();

      var source = scheduler.CreateHotObservable(
        OnNext(0, 42),
        OnCompleted(1, 0));

      using (source.BufferIntrospective(scheduler).Subscribe(buffers.Add))
      {
        scheduler.Start();

        Assert.AreEqual(1, buffers.Count);
        Assert.AreEqual(1, buffers[0].Count);
      }
    }
Esempio n. 20
0
        public void AcceptAsync_Success()
        {
            var sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            var scheduler = new TestScheduler();

            MAsyncSocketExtensions.AcceptAsObservableSocketEndPoint =
                (t1, t2) => scheduler.CreateHotObservable(OnNext(10, sock));

            var observer = scheduler.CreateObserver<Socket>();

            var client = new TcpRosListener(0);

            var result = client.AcceptAsync().Subscribe(observer);

            scheduler.AdvanceTo(10);

            observer.Messages.Is(OnNext(10, sock));
        }
Esempio n. 21
0
        public void TestHotObservable()
        {
            TestScheduler scheduler = new TestScheduler();

            var xs = scheduler.CreateHotObservable (
                scheduler.OnNextAt(100, "a"), // only works in Visual Studio
                scheduler.OnNextAt(200, "b"),
                scheduler.OnCompletedAt<string>(300)
            );

            var observer = scheduler.CreateObserver<string>();
            scheduler.Schedule(TimeSpan.FromTicks(220),
                (sched, state) => xs.Subscribe(observer));

            scheduler.Start();

            observer.Messages.AssertEqual (
                new Recorded<Notification<string>>(250, Notification.CreateOnCompleted<string>())
            );
        }
Esempio n. 22
0
    public void SampleIntrospective()
    {
      var scheduler = new TestScheduler();

      var source = scheduler.CreateHotObservable(
        OnNext(110, "A"),
        OnNext(120, "B"),
        OnNext(130, "C"),
        OnNext(140, "D"),
        // ... delay ...
        OnNext(180, "E"),
        OnNext(190, "F"),
        OnNext(200, "G"), OnNext(201, "H"), OnNext(202, "I"), // burst
        // ... delay ...
        OnNext(250, "J"),
        // ... delay ...
        OnNext(300, "K"), OnNext(302, "L"), OnNext(304, "M"), // burst
        // complete
        OnCompleted<string>(304),
        OnNext(305, "N")
      );

      var sampledSource = source.SampleIntrospective(scheduler)
                                .Do(_ => scheduler.Sleep(15));

      AssertEqual(
        scheduler.Start(() => sampledSource, created: 0, subscribed: 0, disposed: 10000).Messages,
        OnNext(126, "A"),
        OnNext(142, "B"),
        // skipping "C"
        OnNext(158, "D"),
        OnNext(196, "E"),
        OnNext(212, "F"),
        // skipping "G"
        // skipping "H"
        OnNext(228, "I"),
        OnNext(266, "J"),
        OnNext(316, "K"),
        // skipping "L", "M"
        OnCompleted<string>(317));
    }
Esempio n. 23
0
        public void Multicast_Hot_1()
        {
            var scheduler = new TestScheduler();

            var s = new Subject<int>();

            var xs = scheduler.CreateHotObservable(
                OnNext(40, 0),
                OnNext(90, 1),
                OnNext(150, 2),
                OnNext(210, 3),
                OnNext(240, 4),
                OnNext(270, 5),
                OnNext(330, 6),
                OnNext(340, 7),
                OnCompleted<int>(390)
            );

            var c = default(IConnectableObservable<int>);
            var o = scheduler.CreateObserver<int>();
            var d1 = default(IDisposable);
            var d2 = default(IDisposable);

            scheduler.ScheduleAbsolute(50, () => c = xs.Multicast(s));
            scheduler.ScheduleAbsolute(100, () => d1 = c.Subscribe(o));
            scheduler.ScheduleAbsolute(200, () => d2 = c.Connect());
            scheduler.ScheduleAbsolute(300, () => d1.Dispose());

            scheduler.Start();

            o.Messages.AssertEqual(
                OnNext(210, 3),
                OnNext(240, 4),
                OnNext(270, 5)
            );

            xs.Subscriptions.AssertEqual(
                Subscribe(200, 390)
            );
        }
        public void InputHasADelayBetweenEachCharacterOfExaclyFiveSecondsDuringSequence()
        {
            var scheduler = new TestScheduler();
            var inputSequence = scheduler.CreateHotObservable(
                OnNextForAll(1.Seconds(), "a^helloworld$bc").TimeBetweenEach(5.Seconds()),
                OnCompleted<char>(100.Seconds())
                );

            var results = scheduler.Run(() => inputSequence.ToBarcodeReadings(scheduler),
                0.0.Seconds().Ticks,
                0.5.Seconds().Ticks,
                100.Seconds().Ticks
                );

            results.AssertEqual(EnumerableEx.Concat(
                OnNext(61.Seconds(), "helloworld"),
                OnCompleted<string>(100.Seconds())
                ));

            inputSequence.Subscriptions.AssertEqual(
                Subscribe(0.5.Seconds(), 100.Seconds())
                );
        }
Esempio n. 25
0
        public void Subscribe_Success()
        {
            var scheduler = new TestScheduler();
            var observer = scheduler.CreateObserver<std_msgs.String>();
            var publisher = scheduler.CreateHotObservable(OnNext(10, new std_msgs.String() {data = "test data"}));

            MSlaveClient.ConstructorUri = (t1, t2) => { };
            MSlaveClient.AllInstances.RequestTopicAsyncStringStringListOfProtocolInfo =
                (t1, t2, t3, t4) => Task.Factory.StartNew(() => new TopicParam() {HostName = "localhost", PortNumber = 12345, ProtocolName = "TCPROS"});
            MRosTopicServer<std_msgs.String>.ConstructorStringStringUri = (t1, t2, t3, t4) => { };
            MRosTopicServer<std_msgs.String>.AllInstances.StartAsyncTopicParamBoolean =
                (t1, t2, t3) => Task.Factory.StartNew(() => (IObservable<std_msgs.String>)publisher);

            var sub = new Subscriber<std_msgs.String>("testtopic", "test");

            (sub as ISubscriber).UpdatePublishers(new List<Uri>() {new Uri("http://localhosst")});

            sub.Subscribe(observer);

            scheduler.AdvanceBy(100);

            observer.Messages.First().Value.Value.data.Is("test data");
        }
Esempio n. 26
0
        public void FilterBursts_TwoBurstAndGapInEachBurst_FirstInEachBurstEmitted_WithTestableObserver()
        {
            var scheduler = new TestScheduler();

            // Creating an observable that will emit two bursts of values 1-to-3 and (-1)-to-(-3)
            var xs = scheduler.CreateHotObservable(
                OnNext(250, 1),
                OnNext(275, 2),
                OnNext(300, 3),

                OnNext(400, -1),
                OnNext(401, -2),
                OnNext(405, -3),

                OnCompleted<int>(500)
                );

            // Creating a TestableObserver that is capable of recording its observations
            var testableObserver = scheduler.CreateObserver<int>();

            // Act - This is the code we want to test.
            //Since we havent started the scheduler yet, its clock time is 0 (i.e. subscription time is 0)
            xs.FilterBursts(3)
                .Subscribe(testableObserver);

            // Starting the Scheduler so the TestableObservable will starts emitting
            scheduler.Start();

            // Asserting the results
            testableObserver.Messages.AssertEqual(
                OnNext(250, 1),
                OnNext(400, -1),
                OnCompleted<int>(500));

            xs.Subscriptions.AssertEqual(
                Subscribe(0, 500));
        }
Esempio n. 27
0
        public void And2()
        {
            var scheduler = new TestScheduler();

            const int N = 2;

            var obs = new List<IObservable<int>>();
            for (int i = 0; i < N; i++)
            {
                obs.Add(scheduler.CreateHotObservable(
                    OnNext(210, 1),
                    OnCompleted<int>(220)
                ));
            }

            var res = scheduler.Start(() =>
                Observable.When(obs[0].And(obs[1]).Then((a, b) => a + b))
            );

            res.Messages.AssertEqual(
                OnNext(210, N),
                OnCompleted<int>(220)
            );
        }
Esempio n. 28
0
        public static IObservable <T> CreateHotPexObservable <T>(this TestScheduler scheduler, T[] items, int[] deltaTimes, bool failOnError = false)
        {
            var ret = createPexObservable(items, deltaTimes, failOnError);

            return(scheduler.CreateHotObservable(ret));
        }
Esempio n. 29
0
        public void Infinite()
        {
            var scheduler = new TestScheduler();

            var xs = scheduler.CreateHotObservable(
                OnNext(70, 1),
                OnNext(110, 2),
                OnNext(220, 3),
                OnNext(270, 4),
                OnNext(340, 5),
                OnNext(410, 6),
                OnNext(520, 7),
                OnNext(630, 8),
                OnNext(710, 9),
                OnNext(870, 10),
                OnNext(940, 11),
                OnNext(1020, 12)
                );

            var s            = default(Subject <int>);
            var subscription = default(IDisposable);

            var results1      = scheduler.CreateObserver <int>();
            var subscription1 = default(IDisposable);

            var results2      = scheduler.CreateObserver <int>();
            var subscription2 = default(IDisposable);

            var results3      = scheduler.CreateObserver <int>();
            var subscription3 = default(IDisposable);

            scheduler.ScheduleAbsolute(100, () => s            = new Subject <int>());
            scheduler.ScheduleAbsolute(200, () => subscription = xs.Subscribe(s));
            scheduler.ScheduleAbsolute(1000, () => subscription.Dispose());

            scheduler.ScheduleAbsolute(300, () => subscription1 = s.Subscribe(results1));
            scheduler.ScheduleAbsolute(400, () => subscription2 = s.Subscribe(results2));
            scheduler.ScheduleAbsolute(900, () => subscription3 = s.Subscribe(results3));

            scheduler.ScheduleAbsolute(600, () => subscription1.Dispose());
            scheduler.ScheduleAbsolute(700, () => subscription2.Dispose());
            scheduler.ScheduleAbsolute(800, () => subscription1.Dispose());
            scheduler.ScheduleAbsolute(950, () => subscription3.Dispose());

            scheduler.Start();

            results1.Messages.AssertEqual(
                OnNext(340, 5),
                OnNext(410, 6),
                OnNext(520, 7)
                );

            results2.Messages.AssertEqual(
                OnNext(410, 6),
                OnNext(520, 7),
                OnNext(630, 8)
                );

            results3.Messages.AssertEqual(
                OnNext(940, 11)
                );
        }
Esempio n. 30
0
        public void Should_pause_and_resume()
        {
            //Arrange
            var    scheduler        = new TestScheduler();
            var    isRunningTrigger = new BehaviorSubject <bool>(true);
            Action pause            = () => isRunningTrigger.OnNext(false);
            Action resume           = () => isRunningTrigger.OnNext(true);
            var    source           = scheduler.CreateHotObservable(
                ReactiveTest.OnNext(0.1.Seconds(), 1),
                ReactiveTest.OnNext(2.0.Seconds(), 2),
                ReactiveTest.OnNext(4.0.Seconds(), 3),
                ReactiveTest.OnNext(6.0.Seconds(), 4),
                ReactiveTest.OnNext(8.0.Seconds(), 5));

            scheduler.Schedule(TimeSpan.FromSeconds(0.5), () => { pause(); });
            scheduler.Schedule(TimeSpan.FromSeconds(5.0), () => { resume(); });

            //Act
            var sut = Observable.Create <IObservable <int> >(o =>
            {
                var current           = source.Replay();
                var connection        = new SerialDisposable();
                connection.Disposable = current.Connect();
                return(isRunningTrigger
                       .DistinctUntilChanged()
                       .Select(isRunning =>
                {
                    if (isRunning)
                    {
                        //Return the current replayed values.
                        return current;
                    }
                    else
                    {
                        //Disconnect and replace current.
                        current = source.Replay();
                        connection.Disposable = current.Connect();
                        //yield silence until the next time we resume.
                        return Observable.Never <int>();
                    }
                })
                       .Subscribe(o));
            }).Switch();
            var observer = scheduler.CreateObserver <int>();

            using (sut.Subscribe(observer))
            {
                scheduler.Start();
            }
            //Assert
            var expected = new[]
            {
                ReactiveTest.OnNext(0.1.Seconds(), 1),
                ReactiveTest.OnNext(5.0.Seconds(), 2),
                ReactiveTest.OnNext(5.0.Seconds(), 3),
                ReactiveTest.OnNext(6.0.Seconds(), 4),
                ReactiveTest.OnNext(8.0.Seconds(), 5)
            };

            CollectionAssert.AreEqual(expected, observer.Messages);
        }
Esempio n. 31
0
        public void Buffer_Closings_InnerSubscriptions()
        {
            var scheduler = new TestScheduler();

            var xs = scheduler.CreateHotObservable(
                OnNext(90, 1),
                OnNext(180, 2),
                OnNext(250, 3),
                OnNext(260, 4),
                OnNext(310, 5),
                OnNext(340, 6),
                OnNext(410, 7),
                OnNext(420, 8),
                OnNext(470, 9),
                OnNext(550, 10),
                OnCompleted <int>(590)
                );

            var closings = new ITestableObservable <bool>[] {
                scheduler.CreateHotObservable(
                    OnNext(300, true),
                    OnNext(350, false),
                    OnCompleted <bool>(380)
                    ),
                scheduler.CreateHotObservable(
                    OnNext(400, true),
                    OnNext(510, false),
                    OnNext(620, false)
                    ),
                scheduler.CreateHotObservable(
                    OnCompleted <bool>(500)
                    ),
                scheduler.CreateHotObservable(
                    OnNext(600, true)
                    )
            };

            var window = 0;

            var res = scheduler.Start(() =>
                                      xs.Buffer(() => closings[window++])
                                      );

            res.Messages.AssertEqual(
                OnNext <IList <int> >(300, b => b.SequenceEqual(new int[] { 3, 4 })),
                OnNext <IList <int> >(400, b => b.SequenceEqual(new int[] { 5, 6 })),
                OnNext <IList <int> >(500, b => b.SequenceEqual(new int[] { 7, 8, 9 })),
                OnNext <IList <int> >(590, b => b.SequenceEqual(new int[] { 10 })),
                OnCompleted <IList <int> >(590)
                );

            xs.Subscriptions.AssertEqual(
                Subscribe(200, 590)
                );

            closings[0].Subscriptions.AssertEqual(
                Subscribe(200, 300)
                );

            closings[1].Subscriptions.AssertEqual(
                Subscribe(300, 400)
                );

            closings[2].Subscriptions.AssertEqual(
                Subscribe(400, 500)
                );

            closings[3].Subscriptions.AssertEqual(
                Subscribe(500, 590)
                );
        }
Esempio n. 32
0
        public void Infinite2()
        {
            var scheduler = new TestScheduler();

            var xs = scheduler.CreateHotObservable(
                OnNext(70, 1),
                OnNext(110, 2),
                OnNext(220, 3),
                OnNext(270, 4),
                OnNext(280, -1),
                OnNext(290, -2),
                OnNext(340, 5),
                OnNext(410, 6),
                OnNext(520, 7),
                OnNext(630, 8),
                OnNext(710, 9),
                OnNext(870, 10),
                OnNext(940, 11),
                OnNext(1020, 12)
                );

            var subject      = default(ReplaySubject <int>);
            var subscription = default(IDisposable);

            var results1      = scheduler.CreateObserver <int>();
            var subscription1 = default(IDisposable);

            var results2      = scheduler.CreateObserver <int>();
            var subscription2 = default(IDisposable);

            var results3      = scheduler.CreateObserver <int>();
            var subscription3 = default(IDisposable);

            scheduler.ScheduleAbsolute(100, () => subject      = new ReplaySubject <int>(3, TimeSpan.FromTicks(100), scheduler));
            scheduler.ScheduleAbsolute(200, () => subscription = xs.Subscribe(subject));
            scheduler.ScheduleAbsolute(1000, () => subscription.Dispose());

            scheduler.ScheduleAbsolute(300, () => subscription1 = subject.Subscribe(results1));
            scheduler.ScheduleAbsolute(400, () => subscription2 = subject.Subscribe(results2));
            scheduler.ScheduleAbsolute(900, () => subscription3 = subject.Subscribe(results3));

            scheduler.ScheduleAbsolute(600, () => subscription1.Dispose());
            scheduler.ScheduleAbsolute(700, () => subscription2.Dispose());
            scheduler.ScheduleAbsolute(800, () => subscription1.Dispose());
            scheduler.ScheduleAbsolute(950, () => subscription3.Dispose());

            scheduler.Start();

            results1.Messages.AssertEqual(
                OnNext(301, 4),
                OnNext(302, -1),
                OnNext(303, -2),
                OnNext(341, 5),
                OnNext(411, 6),
                OnNext(521, 7)
                );

            results2.Messages.AssertEqual(
                OnNext(401, 5),
                OnNext(411, 6),
                OnNext(521, 7),
                OnNext(631, 8)
                );

            results3.Messages.AssertEqual(
                OnNext(901, 10),
                OnNext(941, 11)
                );
        }
Esempio n. 33
0
 public ITestableObservable <T> CreateHotObservable <T>(params Recorded <Notification <T> >[] messages)
 {
     return(_testScheduler.CreateHotObservable <T>(messages));
 }
Esempio n. 34
0
        public void Switch_Data()
        {
            var scheduler = new TestScheduler();

            var ys1 = scheduler.CreateColdObservable(
                OnNext(10, 101),
                OnNext(20, 102),
                OnNext(110, 103),
                OnNext(120, 104),
                OnNext(210, 105),
                OnNext(220, 106),
                OnCompleted <int>(230)
                );

            var ys2 = scheduler.CreateColdObservable(
                OnNext(10, 201),
                OnNext(20, 202),
                OnNext(30, 203),
                OnNext(40, 204),
                OnCompleted <int>(50)
                );

            var ys3 = scheduler.CreateColdObservable(
                OnNext(10, 301),
                OnNext(20, 302),
                OnNext(30, 303),
                OnNext(40, 304),
                OnCompleted <int>(150)
                );

            var xs = scheduler.CreateHotObservable(
                OnNext <IObservable <int> >(300, ys1),
                OnNext <IObservable <int> >(400, ys2),
                OnNext <IObservable <int> >(500, ys3),
                OnCompleted <IObservable <int> >(600)
                );

            var res = scheduler.Start(() =>
                                      xs.Switch()
                                      );

            res.Messages.AssertEqual(
                OnNext(310, 101),
                OnNext(320, 102),
                OnNext(410, 201),
                OnNext(420, 202),
                OnNext(430, 203),
                OnNext(440, 204),
                OnNext(510, 301),
                OnNext(520, 302),
                OnNext(530, 303),
                OnNext(540, 304),
                OnCompleted <int>(650)
                );

#if !NO_PERF
            // BREAKING CHANGE v2 > v1.x -> More aggressive disposal behavior
            xs.Subscriptions.AssertEqual(
                Subscribe(200, 600)
                );
#else
            xs.Subscriptions.AssertEqual(
                Subscribe(200, 650)
                );
#endif

            ys1.Subscriptions.AssertEqual(
                Subscribe(300, 400)
                );

#if !NO_PERF
            // BREAKING CHANGE v2 > v1.x -> More aggressive disposal behavior
            ys2.Subscriptions.AssertEqual(
                Subscribe(400, 450)
                );
#else
            ys2.Subscriptions.AssertEqual(
                Subscribe(400, 500)
                );
#endif

            ys3.Subscriptions.AssertEqual(
                Subscribe(500, 650)
                );
        }
Esempio n. 35
0
        public void Should_emit_values_along_routes()
        {
            var viaRoute1 = "via-route-1";
            var viaRoute2 = "via-route-2";

            var scheduler = new TestScheduler();

            MqttSource.SetupGet(it => it.Id).Returns("source-1");
            SSESource.SetupGet(it => it.Id).Returns("source-2");

            OpenRGBSink.SetupGet(it => it.Id).Returns("sink-1");
            WallpaperSink.SetupGet(it => it.Id).Returns("sink-2");

            var routes = new RouteOptions[]
            {
                new RouteOptions
                {
                    From = "source-1",
                    To   = new string[]
                    {
                        "sink-1",
                        "sink-2"
                    }
                },
                new RouteOptions
                {
                    From = "source-2",
                    To   = new string[]
                    {
                        "sink-1",
                    }
                }
            };

            var broker = new Broker()
                         .RegisterSources(MqttSource.Object, SSESource.Object)
                         .RegisterSinks(OpenRGBSink.Object, WallpaperSink.Object)
                         .UseRoutes(routes);


            MqttSource.Setup(it => it.Get()).Returns(() =>
            {
                return(scheduler.CreateHotObservable(OnNext(150, viaRoute1)));
            });

            SSESource.Setup(it => it.Get()).Returns(() =>
            {
                return(scheduler.CreateHotObservable(OnNext(100, viaRoute2)));
            });


            broker.Listen();

            scheduler.AdvanceBy(200);

            OpenRGBSink.Verify((it) => it.Consume(viaRoute1), Times.Once());
            OpenRGBSink.Verify((it) => it.Consume(viaRoute2), Times.Once());

            WallpaperSink.Verify((it) => it.Consume(viaRoute1), Times.Once());
            WallpaperSink.Verify((it) => it.Consume(viaRoute2), Times.Never());
        }
Esempio n. 36
0
        public void Switch_InnerThrows()
        {
            var scheduler = new TestScheduler();

            var ex = new Exception();

            var ys1 = scheduler.CreateColdObservable(
                OnNext(10, 101),
                OnNext(20, 102),
                OnNext(110, 103),
                OnNext(120, 104),
                OnNext(210, 105),
                OnNext(220, 106),
                OnCompleted <int>(230)
                );

            var ys2 = scheduler.CreateColdObservable(
                OnNext(10, 201),
                OnNext(20, 202),
                OnNext(30, 203),
                OnNext(40, 204),
                OnError <int>(50, ex)
                );

            var ys3 = scheduler.CreateColdObservable(
                OnNext(10, 301),
                OnNext(20, 302),
                OnNext(30, 303),
                OnNext(40, 304),
                OnCompleted <int>(150)
                );

            var xs = scheduler.CreateHotObservable(
                OnNext <IObservable <int> >(300, ys1),
                OnNext <IObservable <int> >(400, ys2),
                OnNext <IObservable <int> >(500, ys3),
                OnCompleted <IObservable <int> >(600)
                );

            var res = scheduler.Start(() =>
                                      xs.Switch()
                                      );

            res.Messages.AssertEqual(
                OnNext(310, 101),
                OnNext(320, 102),
                OnNext(410, 201),
                OnNext(420, 202),
                OnNext(430, 203),
                OnNext(440, 204),
                OnError <int>(450, ex)
                );

            xs.Subscriptions.AssertEqual(
                Subscribe(200, 450)
                );

            ys1.Subscriptions.AssertEqual(
                Subscribe(300, 400)
                );

            ys2.Subscriptions.AssertEqual(
                Subscribe(400, 450)
                );

            ys3.Subscriptions.AssertEqual(
                );
        }
Esempio n. 37
0
        public void Catch_EnumerableTiming()
        {
            var scheduler = new TestScheduler();

            var o1 = scheduler.CreateHotObservable(
                OnNext(150, 1),
                OnNext(210, 2), // !
                OnNext(220, 3), // !
                OnError <int>(230, new Exception())
                );

            var o2 = scheduler.CreateColdObservable(
                OnNext(50, 4),  // !
                OnNext(60, 5),  // !
                OnNext(70, 6),  // !
                OnError <int>(80, new Exception())
                );

            var o3 = scheduler.CreateHotObservable(
                OnNext(150, 1),
                OnNext(200, 2),
                OnNext(210, 3),
                OnNext(220, 4),
                OnNext(230, 5),
                OnNext(270, 6),
                OnNext(320, 7), // !
                OnNext(330, 8), // !
                OnCompleted <int>(340)
                );

            var xss = new MockEnumerable <ITestableObservable <int> >(scheduler, new[] { o1, o2, o3, o2 });

            var res = scheduler.Start(() =>
                                      xss.Select(xs => (IObservable <int>)xs).Catch()
                                      );

            res.Messages.AssertEqual(
                OnNext(210, 2),
                OnNext(220, 3),
                OnNext(280, 4),
                OnNext(290, 5),
                OnNext(300, 6),
                OnNext(320, 7),
                OnNext(330, 8),
                OnCompleted <int>(340)
                );

            o1.Subscriptions.AssertEqual(
                Subscribe(200, 230)
                );

            o2.Subscriptions.AssertEqual(
                Subscribe(230, 310)
                );

            o3.Subscriptions.AssertEqual(
                Subscribe(310, 340)
                );

            xss.Subscriptions.AssertEqual(
                Subscribe(200, 340)
                );
        }
Esempio n. 38
0
        public void Infinite()
        {
            var scheduler = new TestScheduler();

            var xs = scheduler.CreateHotObservable(
                OnNext(70, 1),
                OnNext(110, 2),
                OnNext(220, 3),
                OnNext(270, 4),
                OnNext(340, 5),
                OnNext(410, 6),
                OnNext(520, 7),
                OnNext(630, 8),
                OnNext(710, 9),
                OnNext(870, 10),
                OnNext(940, 11),
                OnNext(1020, 12)
                );

            var subject      = default(ReplaySubject <int>);
            var subscription = default(IDisposable);

            var results1      = new MockObserver <int>(scheduler);
            var subscription1 = default(IDisposable);

            var results2      = new MockObserver <int>(scheduler);
            var subscription2 = default(IDisposable);

            var results3      = new MockObserver <int>(scheduler);
            var subscription3 = default(IDisposable);

            scheduler.Schedule(() => subject      = new ReplaySubject <int>(3, TimeSpan.FromTicks(100), scheduler), 100);
            scheduler.Schedule(() => subscription = xs.Subscribe(subject), 200);
            scheduler.Schedule(() => subscription.Dispose(), 1000);

            scheduler.Schedule(() => subscription1 = subject.Subscribe(results1), 300);
            scheduler.Schedule(() => subscription2 = subject.Subscribe(results2), 400);
            scheduler.Schedule(() => subscription3 = subject.Subscribe(results3), 900);

            scheduler.Schedule(() => subscription1.Dispose(), 600);
            scheduler.Schedule(() => subscription2.Dispose(), 700);
            scheduler.Schedule(() => subscription1.Dispose(), 800);
            scheduler.Schedule(() => subscription3.Dispose(), 950);

            scheduler.Run();

            results1.AssertEqual(
                OnNext(301, 3),
                OnNext(302, 4),
                OnNext(340, 5),
                OnNext(410, 6),
                OnNext(520, 7)
                );

            results2.AssertEqual(
                OnNext(401, 5),
                OnNext(410, 6),
                OnNext(520, 7),
                OnNext(630, 8)
                );

            results3.AssertEqual(
                OnNext(901, 10),
                OnNext(940, 11)
                );
        }
        public void GetBlocksWithTransactionHashes_Start()
        {
            // The test works as following:
            // There are already 2 blocks. The method then gets called with
            // a `from` value of 1, and while doing so `latestBlock` becomes
            // 2 because another `updateStream` received a new block.
            // This means that the old stream should consist of blocks 1 and 2,
            // and for completeness we assume a new block number 3 comes afterwards.

            // Setup.
            var sched = new TestScheduler();

            var poller        = new Mock <IObservable <Unit> >();
            var filterService = new Mock <IEthApiFilterService>();
            var blockService  = new Mock <IEthApiBlockService>();

            var provider = new BlockStreamProvider(
                poller.Object,
                filterService.Object,
                blockService.Object);

            blockService
            .Setup(x => x.GetBlockNumber.SendRequestAsync(null))
            .Returns(Task.FromResult(new HexBigInteger(2)));

            // Setup old blocks.
            var oldBlock = new BlockWithTransactionHashes
            {
                BlockHash = "0x1",
                Number    = new HexBigInteger(1)
            };

            blockService
            .Setup(x => x.GetBlockWithTransactionsHashesByNumber.SendRequestAsync(new HexBigInteger(new BigInteger(1)), null))
            .Returns(Task.FromResult(oldBlock));

            // Setup new blocks.
            var newBlocks = new[]
            {
                new BlockWithTransactionHashes
                {
                    BlockHash = "0x2",
                    Number    = new HexBigInteger(2)
                },
                new BlockWithTransactionHashes
                {
                    BlockHash = "0x3",
                    Number    = new HexBigInteger(3)
                }
            };

            var newBlockSource = sched.CreateHotObservable(
                OnNext(100, newBlocks[0]),
                OnNext(200, newBlocks[1]));

            // Record incoming data.
            var res = sched.Start(
                () => provider.GetBlocksWithTransactionHashes(new BlockParameter(1), newBlockSource),
                0,
                50,
                Disposed);

            res.Messages.AssertEqual(
                OnNext(50, oldBlock),
                OnNext(100, newBlocks[0]),
                OnNext(200, newBlocks[1]));
        }
Esempio n. 40
0
        public void ReplaySubjectDiesOut()
        {
            //
            // Tests v1.x behavior as documented in ReplaySubject.cs (Subscribe method).
            //

            var scheduler = new TestScheduler();

            var xs = scheduler.CreateHotObservable(
                OnNext(70, 1),
                OnNext(110, 2),
                OnNext(220, 3),
                OnNext(270, 4),
                OnNext(340, 5),
                OnNext(410, 6),
                OnNext(520, 7),
                OnCompleted <int>(580)
                );

            var subject = default(ReplaySubject <int>);

            var results1 = scheduler.CreateObserver <int>();
            var results2 = scheduler.CreateObserver <int>();
            var results3 = scheduler.CreateObserver <int>();
            var results4 = scheduler.CreateObserver <int>();

            scheduler.ScheduleAbsolute(100, () => subject = new ReplaySubject <int>(int.MaxValue, TimeSpan.FromTicks(100), scheduler));
            scheduler.ScheduleAbsolute(200, () => xs.Subscribe(subject));

            scheduler.ScheduleAbsolute(300, () => subject.Subscribe(results1));
            scheduler.ScheduleAbsolute(400, () => subject.Subscribe(results2));
            scheduler.ScheduleAbsolute(600, () => subject.Subscribe(results3));
            scheduler.ScheduleAbsolute(900, () => subject.Subscribe(results4));

            scheduler.Start();

            results1.Messages.AssertEqual(
                OnNext(301, 3),
                OnNext(302, 4),
                OnNext(341, 5),
                OnNext(411, 6),
                OnNext(521, 7),
                OnCompleted <int>(581)
                );

            results2.Messages.AssertEqual(
                OnNext(401, 5),
                OnNext(411, 6),
                OnNext(521, 7),
                OnCompleted <int>(581)
                );

            results3.Messages.AssertEqual(
                OnNext(601, 7),
                OnCompleted <int>(602)
                );

            results4.Messages.AssertEqual(
                OnCompleted <int>(901)
                );
        }
Esempio n. 41
0
        public void Switch_OuterThrows()
        {
            var scheduler = new TestScheduler();

            var ex = new Exception();

            var ys1 = scheduler.CreateColdObservable(
                OnNext(10, 101),
                OnNext(20, 102),
                OnNext(110, 103),
                OnNext(120, 104),
                OnNext(210, 105),
                OnNext(220, 106),
                OnCompleted <int>(230)
                );

            var ys2 = scheduler.CreateColdObservable(
                OnNext(10, 201),
                OnNext(20, 202),
                OnNext(30, 203),
                OnNext(40, 204),
                OnCompleted <int>(50)
                );

            var xs = scheduler.CreateHotObservable(
                OnNext <IObservable <int> >(300, ys1),
                OnNext <IObservable <int> >(400, ys2),
                OnError <IObservable <int> >(500, ex)
                );

            var res = scheduler.Start(() =>
                                      xs.Switch()
                                      );

            res.Messages.AssertEqual(
                OnNext(310, 101),
                OnNext(320, 102),
                OnNext(410, 201),
                OnNext(420, 202),
                OnNext(430, 203),
                OnNext(440, 204),
                OnError <int>(500, ex)
                );

            xs.Subscriptions.AssertEqual(
                Subscribe(200, 500)
                );

            ys1.Subscriptions.AssertEqual(
                Subscribe(300, 400)
                );

#if !NO_PERF
            // BREAKING CHANGE v2 > v1.x -> More aggressive disposal behavior
            ys2.Subscriptions.AssertEqual(
                Subscribe(400, 450)
                );
#else
            ys2.Subscriptions.AssertEqual(
                Subscribe(400, 500)
                );
#endif
        }
        public void GetBlocksWithTransaction_StartEndOldAndNew()
        {
            // Setup.
            var sched = new TestScheduler();

            var poller        = new Mock <IObservable <Unit> >();
            var filterService = new Mock <IEthApiFilterService>();
            var blockService  = new Mock <IEthApiBlockService>();

            var provider = new BlockStreamProvider(
                poller.Object,
                filterService.Object,
                blockService.Object);

            // Setup old blocks (first should be ignored).
            var oldBlocks = new List <BlockWithTransactions>();

            for (var i = 0; i < 4; i++)
            {
                oldBlocks.Add(new BlockWithTransactions
                {
                    BlockHash    = "0x" + i,
                    Number       = new HexBigInteger(i),
                    Transactions = new Transaction[0]
                });

                blockService
                .Setup(x => x.GetBlockWithTransactionsByNumber.SendRequestAsync(new HexBigInteger(new BigInteger(i)), null))
                .Returns(Task.FromResult(oldBlocks[i]));
            }

            blockService
            .Setup(x => x.GetBlockNumber.SendRequestAsync(null))
            .Returns(Task.FromResult(new HexBigInteger(oldBlocks.Last().Number)));

            // Setup new blocks (last one should be ignored).
            var newBlocks = new[]
            {
                new BlockWithTransactions
                {
                    BlockHash    = "0x4",
                    Number       = new HexBigInteger(4),
                    Transactions = new Transaction[0]
                },
                new BlockWithTransactions
                {
                    BlockHash    = "0x5",
                    Number       = new HexBigInteger(5),
                    Transactions = new Transaction[0]
                }
            };

            var newBlockSource = sched.CreateHotObservable(
                OnNext(100, newBlocks[0]),
                OnNext(100, newBlocks[1]));

            // Record incoming data.
            var res = sched.Start(
                () => provider.GetBlocksWithTransactions(
                    new BlockParameter(1),
                    new BlockParameter(4),
                    newBlockSource),
                0,
                50,
                Disposed);

            res.Messages.AssertEqual(
                OnNext(50, oldBlocks[1]),
                OnNext(50, oldBlocks[2]),
                OnNext(100, newBlocks[0]),
                OnCompleted <BlockWithTransactions>(100));
        }
Esempio n. 43
0
        public void Error_ReplayAll()
        {
            var scheduler = new TestScheduler();

            var ex = new Exception();

            var xs = scheduler.CreateHotObservable(
                OnNext(70, 1),
                OnNext(110, 2),
                OnNext(220, 3),
                OnNext(270, 4),
                OnNext(340, 5),
                OnNext(410, 6),
                OnNext(520, 7),
                OnError<int>(630, ex),
                OnNext(640, 9),
                OnCompleted<int>(650),
                OnError<int>(660, new Exception())
            );

            var subject = default(ReplaySubject<int>);
            var subscription = default(IDisposable);

            var results1 = scheduler.CreateObserver<int>();
            var subscription1 = default(IDisposable);

            var results2 = scheduler.CreateObserver<int>();
            var subscription2 = default(IDisposable);

            var results3 = scheduler.CreateObserver<int>();
            var subscription3 = default(IDisposable);

            scheduler.ScheduleAbsolute(100, () => subject = new ReplaySubject<int>());
            scheduler.ScheduleAbsolute(200, () => subscription = xs.Subscribe(subject));
            scheduler.ScheduleAbsolute(1000, () => subscription.Dispose());

            scheduler.ScheduleAbsolute(300, () => subscription1 = subject.Subscribe(results1));
            scheduler.ScheduleAbsolute(400, () => subscription2 = subject.Subscribe(results2));
            scheduler.ScheduleAbsolute(900, () => subscription3 = subject.Subscribe(results3));

            scheduler.ScheduleAbsolute(600, () => subscription1.Dispose());
            scheduler.ScheduleAbsolute(700, () => subscription2.Dispose());
            scheduler.ScheduleAbsolute(950, () => subscription3.Dispose());

            scheduler.Start();

            results1.Messages.AssertEqual(
                OnNext(300, 3),
                OnNext(300, 4),
                OnNext(340, 5),
                OnNext(410, 6),
                OnNext(520, 7)
            );

            results2.Messages.AssertEqual(
                OnNext(400, 3),
                OnNext(400, 4),
                OnNext(400, 5),
                OnNext(410, 6),
                OnNext(520, 7),
                OnError<int>(630, ex)
            );

            results3.Messages.AssertEqual(
                OnNext(900, 3),
                OnNext(900, 4),
                OnNext(900, 5),
                OnNext(900, 6),
                OnNext(900, 7),
                OnError<int>(900, ex)
            );
        }
Esempio n. 44
0
        public void Latest2()
        {
            var scheduler = new TestScheduler();

            var xs = scheduler.CreateHotObservable(
                OnNext(210, 1),
                OnNext(220, 2),
                OnNext(230, 3),
                OnNext(240, 4),
                OnNext(250, 5),
                OnNext(260, 6),
                OnNext(270, 7),
                OnNext(280, 8),
                OnNext(290, 9),
                OnCompleted <int>(300)
                );

            var res = xs.Latest();

            var e1 = default(IEnumerator <int>);

            scheduler.ScheduleAbsolute(205, () =>
            {
                e1 = res.GetEnumerator();
            });

            var o1 = new List <int>();

            scheduler.ScheduleAbsolute(235, () =>
            {
                Assert.True(e1.MoveNext());
                o1.Add(e1.Current);
            });
            scheduler.ScheduleAbsolute(265, () =>
            {
                Assert.True(e1.MoveNext());
                o1.Add(e1.Current);
            });

            scheduler.ScheduleAbsolute(285, () => e1.Dispose());

            var e2 = default(IEnumerator);

            scheduler.ScheduleAbsolute(255, () =>
            {
                e2 = ((IEnumerable)res).GetEnumerator();
            });

            var o2 = new List <int>();

            scheduler.ScheduleAbsolute(265, () =>
            {
                Assert.True(e2.MoveNext());
                o2.Add((int)e2.Current);
            });
            scheduler.ScheduleAbsolute(275, () =>
            {
                Assert.True(e2.MoveNext());
                o2.Add((int)e2.Current);
            });

            scheduler.Start();

            xs.Subscriptions.AssertEqual(
                Subscribe(205, 285),
                Subscribe(255, 300)
                );

            o1.AssertEqual(3, 6);
            o2.AssertEqual(6, 7);
        }
Esempio n. 45
0
        static void Main(string[] args)
        {
            var sched     = new TestScheduler();
            var newConnOb = sched.CreateHotObservable(
                sched.OnNextAt(100, "1"),
                sched.OnNextAt(200, "2"),
                sched.OnNextAt(600, "3")
                );

            var remConnOb = sched.CreateHotObservable(
                sched.OnNextAt(500, "2"),
                sched.OnNextAt(900, "1"),
                sched.OnNextAt(1000, "3")
                );

            var packageSimulator = sched.CreateHotObservable(
                sched.OnNextAt(101, Tuple.Create("1", "p1")),
                sched.OnNextAt(120, Tuple.Create("1", "p2")),
                sched.OnNextAt(250, Tuple.Create("2", "p1")),
                sched.OnNextAt(450, Tuple.Create("2", "p2")),
                sched.OnNextAt(700, Tuple.Create("3", "p3")),
                sched.OnNextAt(950, Tuple.Create("1", "p1"))
                );


            var ob = Observable.Interval(TimeSpan.FromMilliseconds(50), sched).Publish().RefCount();

            packageSimulator
            .Window(newConnOb, s => remConnOb.Where(c => c == s))
            .Subscribe(window =>
            {
                string connection = null;
                var windowPublish = window.Publish();

                windowPublish.FirstOrDefaultAsync().Subscribe(p =>
                {
                    if (p == null)
                    {
                        connection = p.Item1;
                    }
                    Console.WriteLine("Created {1} at {0}", sched.Now.Millisecond, connection);
                });

                windowPublish.Where(p => p.Item1 == connection).Subscribe(_ =>
                {
                    Console.WriteLine("New item in {2} [{0}] at {1}", _, sched.Now.Millisecond, connection);
                }, () =>
                {
                    Console.WriteLine("Completed {1} at {0}", sched.Now.Millisecond, connection);
                });
                windowPublish.Connect();
            });

            sched.AdvanceToMs(1001);
            Console.ReadLine();



//			G.Writer = ConsoleWriter.Default;
//            Application.MainAsync().Wait();
//			Console.ReadLine();
        }
        public void OnErrorResumeNext_EmptyReturnThrowAndMore()
        {
            var scheduler = new TestScheduler();

            var o1 = scheduler.CreateHotObservable(
                OnNext(150, 1),
                OnCompleted <int>(205)
                );

            var o2 = scheduler.CreateHotObservable(
                OnNext(215, 2),
                OnCompleted <int>(220)
                );

            var o3 = scheduler.CreateHotObservable(
                OnNext(225, 3),
                OnNext(230, 4),
                OnCompleted <int>(235)
                );

            var o4 = scheduler.CreateHotObservable(
                OnError <int>(240, new Exception())
                );

            var o5 = scheduler.CreateHotObservable(
                OnNext(245, 5),
                OnCompleted <int>(250)
                );

            var res = scheduler.Start(() =>
                                      new[] { o1, o2, o3, o4, o5 }.OnErrorResumeNext()
                                      );

            res.Messages.AssertEqual(
                OnNext(215, 2),
                OnNext(225, 3),
                OnNext(230, 4),
                OnNext(245, 5),
                OnCompleted <int>(250)
                );

            o1.Subscriptions.AssertEqual(
                Subscribe(200, 205)
                );

            o2.Subscriptions.AssertEqual(
                Subscribe(205, 220)
                );

            o3.Subscriptions.AssertEqual(
                Subscribe(220, 235)
                );

            o4.Subscriptions.AssertEqual(
                Subscribe(235, 240)
                );

            o5.Subscriptions.AssertEqual(
                Subscribe(240, 250)
                );
        }
Esempio n. 47
0
        public void Finite()
        {
            var scheduler = new TestScheduler();

            var xs = scheduler.CreateHotObservable(
                OnNext(70, 1),
                OnNext(110, 2),
                OnNext(220, 3),
                OnNext(270, 4),
                OnNext(340, 5),
                OnNext(410, 6),
                OnNext(520, 7),
                OnCompleted <int>(630),
                OnNext(640, 9),
                OnCompleted <int>(650),
                OnError <int>(660, new Exception())
                );

            var subject      = default(BehaviorSubject <int>);
            var subscription = default(IDisposable);

            var results1      = scheduler.CreateObserver <int>();
            var subscription1 = default(IDisposable);

            var results2      = scheduler.CreateObserver <int>();
            var subscription2 = default(IDisposable);

            var results3      = scheduler.CreateObserver <int>();
            var subscription3 = default(IDisposable);

            scheduler.ScheduleAbsolute(100, () => subject      = new BehaviorSubject <int>(100));
            scheduler.ScheduleAbsolute(200, () => subscription = xs.Subscribe(subject));
            scheduler.ScheduleAbsolute(1000, () => subscription.Dispose());

            scheduler.ScheduleAbsolute(300, () => subscription1 = subject.Subscribe(results1));
            scheduler.ScheduleAbsolute(400, () => subscription2 = subject.Subscribe(results2));
            scheduler.ScheduleAbsolute(900, () => subscription3 = subject.Subscribe(results3));

            scheduler.ScheduleAbsolute(600, () => subscription1.Dispose());
            scheduler.ScheduleAbsolute(700, () => subscription2.Dispose());
            scheduler.ScheduleAbsolute(800, () => subscription1.Dispose());
            scheduler.ScheduleAbsolute(950, () => subscription3.Dispose());

            scheduler.Start();

            results1.Messages.AssertEqual(
                OnNext(300, 4),
                OnNext(340, 5),
                OnNext(410, 6),
                OnNext(520, 7)
                );

            results2.Messages.AssertEqual(
                OnNext(400, 5),
                OnNext(410, 6),
                OnNext(520, 7),
                OnCompleted <int>(630)
                );

            results3.Messages.AssertEqual(
                OnCompleted <int>(900)
                );
        }
Esempio n. 48
0
        public static IObservable <char> CreateHotObservable(this TestScheduler scheduler, string marbles)
        {
            var marbleList = FromMarbles(marbles).ToArray();

            return(scheduler.CreateHotObservable(marbleList));
        }
Esempio n. 49
0
        public void Buffer_ShouldProduceOn_CountOrTimeoutOrFlush_ByUsingAmbAndDefferedRecursion()
        {
            //Arrange
            var timeThreshold  = TimeSpan.FromMilliseconds(20);
            var countThreshold = 3;

            var testScheduler = new TestScheduler();
            var testObserver  = testScheduler.CreateObserver <IList <int> >();

            var source = testScheduler.CreateHotObservable(
                new Recorded <Notification <int> >(TimeSpan.FromMilliseconds(1).Ticks, Notification.CreateOnNext(0)),
                new Recorded <Notification <int> >(TimeSpan.FromMilliseconds(2).Ticks, Notification.CreateOnNext(1)),
                new Recorded <Notification <int> >(TimeSpan.FromMilliseconds(3).Ticks, Notification.CreateOnNext(2)),
                // Spew above due to count
                new Recorded <Notification <int> >(TimeSpan.FromMilliseconds(4).Ticks, Notification.CreateOnNext(3)),
                // Spew above due to timeout
                new Recorded <Notification <int> >(TimeSpan.FromMilliseconds(25).Ticks, Notification.CreateOnNext(4)),
                // Spew above due to flush
                new Recorded <Notification <int> >(TimeSpan.FromMilliseconds(33).Ticks, Notification.CreateOnNext(5)),
                //Spew above due to timeout
                new Recorded <Notification <int> >(TimeSpan.FromMilliseconds(54).Ticks, Notification.CreateOnNext(6)),
                new Recorded <Notification <int> >(TimeSpan.FromMilliseconds(64).Ticks, Notification.CreateOnNext(7)),
                //Spew above due to timeout
                new Recorded <Notification <int> >(TimeSpan.FromMilliseconds(75).Ticks, Notification.CreateOnNext(8)),
                new Recorded <Notification <int> >(TimeSpan.FromMilliseconds(76).Ticks, Notification.CreateOnNext(9)),
                new Recorded <Notification <int> >(TimeSpan.FromMilliseconds(77).Ticks, Notification.CreateOnNext(10)),
                // Spew above due to count
                new Recorded <Notification <int> >(TimeSpan.FromMilliseconds(96).Ticks, Notification.CreateOnNext(11)),
                // Spew above due to flush
                new Recorded <Notification <int> >(TimeSpan.FromMilliseconds(98).Ticks, Notification.CreateOnNext(12)),
                new Recorded <Notification <int> >(TimeSpan.FromMilliseconds(99).Ticks, Notification.CreateOnNext(13)),
                new Recorded <Notification <int> >(TimeSpan.FromMilliseconds(100).Ticks, Notification.CreateOnNext(14)),
                // Spew above due to count
                new Recorded <Notification <int> >(TimeSpan.FromMilliseconds(101).Ticks, Notification.CreateOnCompleted <int>())
                );

            var manualFlush = testScheduler.CreateHotObservable(
                new Recorded <Notification <Unit> >(TimeSpan.FromMilliseconds(27).Ticks, Notification.CreateOnNext(Unit.Default)),
                new Recorded <Notification <Unit> >(TimeSpan.FromMilliseconds(97).Ticks, Notification.CreateOnNext(Unit.Default)),
                new Recorded <Notification <Unit> >(TimeSpan.FromMilliseconds(101).Ticks, Notification.CreateOnCompleted <Unit>())
                );

            var timeOrCountOrFlush = GetTimeOrCount(source, testScheduler, timeThreshold, countThreshold, manualFlush);

            //Act
            source.Buffer(timeOrCountOrFlush).Where(b => b.Any()).Subscribe(testObserver);

            //Assert
            testScheduler.Start();
            var results = testObserver.Messages
                          .Where(m => m.Value.Kind == NotificationKind.OnNext)
                          .Select(m => m.Value.Value).ToArray();

            Assert.AreEqual(8, results.Length);
            ReactiveAssert.AreElementsEqual(new[] { 0, 1, 2 }, results[0]);
            ReactiveAssert.AreElementsEqual(new[] { 3 }, results[1]);
            ReactiveAssert.AreElementsEqual(new[] { 4 }, results[2]);
            ReactiveAssert.AreElementsEqual(new[] { 5 }, results[3]);
            ReactiveAssert.AreElementsEqual(new[] { 6, 7 }, results[4]);
            ReactiveAssert.AreElementsEqual(new[] { 8, 9, 10 }, results[5]);
            ReactiveAssert.AreElementsEqual(new[] { 11 }, results[6]);
            ReactiveAssert.AreElementsEqual(new[] { 12, 13, 14 }, results[7]);
        }
        public void CombineTest()
        {
            var testScheduler = new TestScheduler();

            var history = testScheduler.CreateColdObservable(
                OnNext(1L, new DummyNotification {
                EventId = 1
            }),
                OnNext(2L, new DummyNotification {
                EventId = 2
            }),
                OnNext(3L, new DummyNotification {
                EventId = 3
            }),
                OnNext(4L, new DummyNotification {
                EventId = 4
            }),
                OnCompleted(new DummyNotification(), 5L));

            var live = testScheduler.CreateHotObservable(
                OnNext(1L, new DummyNotification {
                EventId = 3
            }),
                OnNext(2L, new DummyNotification {
                EventId = 4
            }),
                OnNext(3L, new DummyNotification {
                EventId = 5
            }),
                OnNext(4L, new DummyNotification {
                EventId = 6
            }),
                OnNext(5L, new DummyNotification {
                EventId = 7
            }),
                OnNext(6L, new DummyNotification {
                EventId = 8
            }),
                OnNext(7L, new DummyNotification {
                EventId = 9
            })
                );

            var observer = testScheduler.CreateObserver <ICacheNotification>();

            history.Combine(live).Subscribe(observer);

            testScheduler.AdvanceTo(6L);

            ReactiveAssert.AreElementsEqual(
                new[]
            {
                OnNext <ICacheNotification>(5, n => n.EventId == 1),
                OnNext <ICacheNotification>(5, n => n.EventId == 2),
                OnNext <ICacheNotification>(5, n => n.EventId == 3),
                OnNext <ICacheNotification>(5, n => n.EventId == 4),
                OnNext <ICacheNotification>(5, n => n.EventId == 5),
                OnNext <ICacheNotification>(5, n => n.EventId == 6),
                OnNext <ICacheNotification>(5, n => n.EventId == 7),
                OnNext <ICacheNotification>(6, n => n.EventId == 8)
            },
                observer.Messages);
        }
Esempio n. 51
0
        public void Canceled()
        {
            var scheduler = new TestScheduler();

            var xs = scheduler.CreateHotObservable(
                OnCompleted<int>(630),
                OnNext(640, 9),
                OnCompleted<int>(650),
                OnError<int>(660, new Exception())
            );

            var subject = default(AsyncSubject<int>);
            var subscription = default(IDisposable);

            var results1 = scheduler.CreateObserver<int>();
            var subscription1 = default(IDisposable);

            var results2 = scheduler.CreateObserver<int>();
            var subscription2 = default(IDisposable);

            var results3 = scheduler.CreateObserver<int>();
            var subscription3 = default(IDisposable);

            scheduler.ScheduleAbsolute(100, () => subject = new AsyncSubject<int>());
            scheduler.ScheduleAbsolute(200, () => subscription = xs.Subscribe(subject));
            scheduler.ScheduleAbsolute(1000, () => subscription.Dispose());

            scheduler.ScheduleAbsolute(300, () => subscription1 = subject.Subscribe(results1));
            scheduler.ScheduleAbsolute(400, () => subscription2 = subject.Subscribe(results2));
            scheduler.ScheduleAbsolute(900, () => subscription3 = subject.Subscribe(results3));

            scheduler.ScheduleAbsolute(600, () => subscription1.Dispose());
            scheduler.ScheduleAbsolute(700, () => subscription2.Dispose());
            scheduler.ScheduleAbsolute(800, () => subscription1.Dispose());
            scheduler.ScheduleAbsolute(950, () => subscription3.Dispose());

            scheduler.Start();

            results1.Messages.AssertEqual(
            );

            results2.Messages.AssertEqual(
                OnCompleted<int>(630)
            );

            results3.Messages.AssertEqual(
                OnCompleted<int>(900)
            );
        }
Esempio n. 52
0
        public void Error()
        {
            var scheduler = new TestScheduler();

            var xs = scheduler.CreateHotObservable(
                OnNext(70, 1),
                OnNext(110, 2),
                OnNext(220, 3),
                OnNext(270, 4),
                OnNext(340, 5),
                OnNext(410, 6),
                OnNext(520, 7),
                OnError <int>(630, new MockException(30)),
                OnNext(640, 9),
                OnCompleted <int>(650),
                OnError <int>(660, new MockException(1))
                );

            var subject      = default(BehaviorSubject <int>);
            var subscription = default(IDisposable);

            var results1      = new MockObserver <int>(scheduler);
            var subscription1 = default(IDisposable);

            var results2      = new MockObserver <int>(scheduler);
            var subscription2 = default(IDisposable);

            var results3      = new MockObserver <int>(scheduler);
            var subscription3 = default(IDisposable);

            scheduler.Schedule(() => subject      = new BehaviorSubject <int>(100, scheduler), 100);
            scheduler.Schedule(() => subscription = xs.Subscribe(subject), 200);
            scheduler.Schedule(() => subscription.Dispose(), 1000);

            scheduler.Schedule(() => subscription1 = subject.Subscribe(results1), 300);
            scheduler.Schedule(() => subscription2 = subject.Subscribe(results2), 400);
            scheduler.Schedule(() => subscription3 = subject.Subscribe(results3), 900);

            scheduler.Schedule(() => subscription1.Dispose(), 600);
            scheduler.Schedule(() => subscription2.Dispose(), 700);
            scheduler.Schedule(() => subscription1.Dispose(), 800);
            scheduler.Schedule(() => subscription3.Dispose(), 950);

            scheduler.Run();

            results1.AssertEqual(
                OnNext(301, 4),
                OnNext(340, 5),
                OnNext(410, 6),
                OnNext(520, 7)
                );

            results2.AssertEqual(
                OnNext(401, 5),
                OnNext(410, 6),
                OnNext(520, 7),
                OnError <int>(630, new MockException(30))
                );

            results3.AssertEqual(
                OnError <int>(901, new MockException(30))
                );
        }
Esempio n. 53
0
        public void MultipleSubscriber()
        {
            var scheduler = new TestScheduler();

            var observer1 = new OneLineCacheSubject <std_msgs.String>();
            var observer2 = new OneLineCacheSubject <std_msgs.String>();
            var observer3 = new OneLineCacheSubject <std_msgs.String>();

            var obs = scheduler.CreateHotObservable(
                OnNext(10, new std_msgs.String()
            {
                data = "abc"
            }),
                OnNext(20, new std_msgs.String()
            {
                data = "defg"
            }),
                OnNext(30, new std_msgs.String()
            {
                data = "hijklmn"
            })
                );

            var node1 = Ros.InitNodeAsync("test1").Result;
            var node2 = Ros.InitNodeAsync("test2").Result;
            var node3 = Ros.InitNodeAsync("test3").Result;

            var subscriber1 = node1.SubscriberAsync <std_msgs.String>("test_topic").Result;
            var subscriber2 = node2.SubscriberAsync <std_msgs.String>("test_topic").Result;
            var publisher   = node1.PublisherAsync <std_msgs.String>("test_topic").Result;
            var subscriber3 = node3.SubscriberAsync <std_msgs.String>("test_topic").Result;

            publisher.WaitForConnection(TestTimeout);
            subscriber1.WaitForConnection(TestTimeout);
            subscriber2.WaitForConnection(TestTimeout);
            subscriber3.WaitForConnection(TestTimeout);

            subscriber1.Subscribe(observer1);
            subscriber2.Subscribe(observer2);
            subscriber3.Subscribe(observer3);
            obs.Subscribe(publisher);

            scheduler.AdvanceBy(10);
            observer1.Timeout(TestTimeout).First().data.Is("abc");
            observer2.Timeout(TestTimeout).First().data.Is("abc");
            observer3.Timeout(TestTimeout).First().data.Is("abc");

            observer1.Dispose();
            scheduler.AdvanceBy(10);
            AssertEx.Throws <TimeoutException>(() => observer1.Timeout(TestTimeout).First());
            observer2.Timeout(TestTimeout).First().data.Is("defg");
            observer3.Timeout(TestTimeout).First().data.Is("defg");

            observer2.Dispose();
            scheduler.AdvanceBy(10);
            AssertEx.Throws <TimeoutException>(() => observer1.Timeout(TestTimeout).First());
            AssertEx.Throws <TimeoutException>(() => observer2.Timeout(TestTimeout).First());
            observer3.Timeout(TestTimeout).First().data.Is("hijklmn");


            subscriber1.Dispose();
            subscriber2.Dispose();
            subscriber3.Dispose();
            publisher.Dispose();

            node1.Dispose();
            node2.Dispose();
            node3.Dispose();
        }
Esempio n. 54
0
        public void MultiplePublisher()
        {
            var scheduler = new TestScheduler();

            var observer = new OneLineCacheSubject <std_msgs.String>();

            var obs1 = scheduler.CreateHotObservable(
                OnNext(110, new std_msgs.String()
            {
                data = "abc1"
            }),
                OnNext(210, new std_msgs.String()
            {
                data = "defg1"
            }),
                OnNext(310, new std_msgs.String()
            {
                data = "hijklmn1"
            })
                );
            var obs2 = scheduler.CreateHotObservable(
                OnNext(120, new std_msgs.String()
            {
                data = "abc2"
            }),
                OnNext(220, new std_msgs.String()
            {
                data = "defg2"
            }),
                OnNext(320, new std_msgs.String()
            {
                data = "hijklmn2"
            })
                );
            var obs3 = scheduler.CreateHotObservable(
                OnNext(130, new std_msgs.String()
            {
                data = "abc3"
            }),
                OnNext(230, new std_msgs.String()
            {
                data = "defg3"
            }),
                OnNext(330, new std_msgs.String()
            {
                data = "hijklmn3"
            })
                );

            var node1 = Ros.InitNodeAsync("test1").Result;
            var node2 = Ros.InitNodeAsync("test2").Result;
            var node3 = Ros.InitNodeAsync("test3").Result;

            var publisher1 = node1.PublisherAsync <std_msgs.String>("test_topic").Result;
            var publisher2 = node2.PublisherAsync <std_msgs.String>("test_topic").Result;
            var subscriber = node1.SubscriberAsync <std_msgs.String>("test_topic").Result;
            var publisher3 = node3.PublisherAsync <std_msgs.String>("test_topic").Result;

            publisher1.WaitForConnection(TestTimeout);
            publisher2.WaitForConnection(TestTimeout);
            publisher3.WaitForConnection(TestTimeout);
            subscriber.WaitForConnection(TestTimeout);

            subscriber.Subscribe(observer);
            obs1.Subscribe(publisher1);
            obs2.Subscribe(publisher2);
            obs3.Subscribe(publisher3);

            scheduler.AdvanceTo(110);
            observer.Timeout(TestTimeout).First().data.Is("abc1");

            scheduler.AdvanceTo(120);
            observer.Timeout(TestTimeout).First().data.Is("abc2");

            scheduler.AdvanceTo(130);
            observer.Timeout(TestTimeout).First().data.Is("abc3");

            scheduler.AdvanceTo(210);
            observer.Timeout(TestTimeout).First().data.Is("defg1");

            scheduler.AdvanceTo(220);
            observer.Timeout(TestTimeout).First().data.Is("defg2");

            scheduler.AdvanceTo(230);
            observer.Timeout(TestTimeout).First().data.Is("defg3");

            scheduler.AdvanceTo(310);
            observer.Timeout(TestTimeout).First().data.Is("hijklmn1");

            scheduler.AdvanceTo(320);
            observer.Timeout(TestTimeout).First().data.Is("hijklmn2");

            scheduler.AdvanceTo(330);
            observer.Timeout(TestTimeout).First().data.Is("hijklmn3");


            subscriber.Dispose();
            publisher1.Dispose();
            publisher2.Dispose();
            publisher3.Dispose();

            node1.Dispose();
            node2.Dispose();
            node3.Dispose();
        }
Esempio n. 55
0
        public void Test_Window()
        {
            var scheduler = new TestScheduler();

            // Create an observable with lots of data, remember subscribe happens at 200
            var obs = scheduler.CreateHotObservable<int>(
                OnNext(150, 1),
                OnNext(210, 2),
                OnNext(215, 3),
                OnNext(220, 2),
                OnNext(235, 4),
                OnNext(247, 2),
                OnNext(255, 5),
                OnNext(263, 6),
                OnNext(276, 1),
                OnNext(288, 3),
                OnNext(291, 4),
                OnCompleted<int>(300)
            );

            // Let's create rolling windows at every 50 ticks skipping 10 ticks
            var res = scheduler.Start(() =>
            {
                return from w in obs.Window(TimeSpan.FromTicks(50), TimeSpan.FromTicks(10), scheduler)
                       from sum in w.Sum()
                       select sum;
            });

            // Assert that our rolling windows for sums works
            res.Messages.AssertEqual(
                OnNext(250, 13),
                OnNext(260, 16),
                OnNext(270, 17),
                OnNext(280, 18),
                OnNext(290, 17),
                OnNext(300, 19),
                OnNext(300, 14),
                OnNext(300, 8),
                OnNext(300, 7),
                OnNext(300, 4),
                OnCompleted<int>(300)
            );

            obs.Subscriptions.AssertEqual(
                Subscribe(200, 300)
            );
        }
Esempio n. 56
0
        public void LazyRefCount_Publish()
        {
            var scheduler = new TestScheduler();

            var xs = scheduler.CreateHotObservable(
                OnNext(210, 1),
                OnNext(220, 2),
                OnNext(230, 3),
                OnNext(240, 4),
                OnNext(250, 5),
                OnNext(260, 6),
                OnNext(270, 7),
                OnNext(280, 8),
                OnNext(290, 9),
                OnCompleted <int>(300)
                );

            var res = xs.Publish().RefCount(TimeSpan.FromTicks(9), scheduler);

            var d1 = default(IDisposable);
            var o1 = scheduler.CreateObserver <int>();

            scheduler.ScheduleAbsolute(215, () => { d1 = res.Subscribe(o1); });
            scheduler.ScheduleAbsolute(235, () => { d1.Dispose(); });

            var d2 = default(IDisposable);
            var o2 = scheduler.CreateObserver <int>();

            scheduler.ScheduleAbsolute(225, () => { d2 = res.Subscribe(o2); });
            scheduler.ScheduleAbsolute(275, () =>
            {
                d2.Dispose();
            });

            var d3 = default(IDisposable);
            var o3 = scheduler.CreateObserver <int>();

            scheduler.ScheduleAbsolute(255, () => { d3 = res.Subscribe(o3); });
            scheduler.ScheduleAbsolute(265, () => { d3.Dispose(); });

            var d4 = default(IDisposable);
            var o4 = scheduler.CreateObserver <int>();

            scheduler.ScheduleAbsolute(285, () => { d4 = res.Subscribe(o4); });
            scheduler.ScheduleAbsolute(320, () => { d4.Dispose(); });

            scheduler.Start();

            o1.Messages.AssertEqual(
                OnNext(220, 2),
                OnNext(230, 3)
                );

            o2.Messages.AssertEqual(
                OnNext(230, 3),
                OnNext(240, 4),
                OnNext(250, 5),
                OnNext(260, 6),
                OnNext(270, 7)
                );

            o3.Messages.AssertEqual(
                OnNext(260, 6)
                );

            o4.Messages.AssertEqual(
                OnNext(290, 9),
                OnCompleted <int>(300)
                );

            xs.Subscriptions.AssertEqual(
                Subscribe(215, 284),
                Subscribe(285, 300)
                );
        }