public void CreatColdObservable_LongWay()
        {
            var testScheduler = new TestScheduler();

            ITestableObservable <int> coldObservable = testScheduler.CreateColdObservable <int>(
                // This is the long way to configure emissions. see below for a shorter one
                new Recorded <Notification <int> >(20, Notification.CreateOnNext <int>(1)),
                new Recorded <Notification <int> >(40, Notification.CreateOnNext <int>(2)),
                new Recorded <Notification <int> >(60, Notification.CreateOnCompleted <int>())
                );

            // Creating an observer that captures the emission it recieves
            var testableObserver = testScheduler.CreateObserver <int>();


            // Subscribing the observer, but until TestSchduler is started, emissions
            // are not be emitted
            coldObservable
            .Subscribe(testableObserver);

            // Starting the TestScheduler means that only now emissions that were configured
            // will be emitted
            testScheduler.Start();

            // Asserting that every emitted value was recieved by the observer at the
            // same time it was emitted
            coldObservable.Messages
            .AssertEqual(testableObserver.Messages);

            // Asserting that the observer was subscribed at Scheduler inital time
            coldObservable.Subscriptions.AssertEqual(
                Subscribe(0));
        }
コード例 #2
0
        public void DownloadFile_DownloadRemovedOnFailure()
        {
            TestScheduler testScheduler = new TestScheduler();
            ITestableObservable <double> testObservable = testScheduler.CreateColdObservable(new Recorded <Notification <double> >(1, Notification.CreateOnError <double>(new TestException())),
                                                                                             new Recorded <Notification <double> >(2, Notification.CreateOnError <double>(new TestException())),
                                                                                             new Recorded <Notification <double> >(3, Notification.CreateOnError <double>(new TestException())));

            IStorage storage = Substitute.For <IStorage>();

            IHttpService httpService = Substitute.For <IHttpService>();

            httpService.DownloadFileAsync(Arg.Any <string>(), Arg.Any <string>(), Arg.Any <Stream>())
            .Returns(testObservable);

            var downloadManager = new Managers.DownloadManager(httpService, storage);

            downloadManager.MaxRetryCount = 3;
            string url      = "http://www.someplace.com/file.bin";
            var    download = downloadManager.DownloadFile(url);

            testScheduler.AdvanceBy(1);
            testScheduler.AdvanceBy(1);
            testScheduler.AdvanceBy(1);

            var queueDownload = downloadManager.DownloadQueue.FirstOrDefault();

            Assert.IsNull(queueDownload);
        }
コード例 #3
0
        public void DownloadFile_RemovesDownloadsFromDownloadDictionaryOnCompletion()
        {
            TestScheduler testScheduler = new TestScheduler();
            ITestableObservable <double> testObservable  = testScheduler.CreateColdObservable(new Recorded <Notification <double> >(1, Notification.CreateOnCompleted <double>()));
            ITestableObservable <double> testObservable2 = testScheduler.CreateColdObservable(new Recorded <Notification <double> >(1, Notification.CreateOnCompleted <double>()));
            IHttpService httpService = Substitute.For <IHttpService>();
            IStorage     storage     = Substitute.For <IStorage>();

            var downloadManager = new Managers.DownloadManager(httpService, storage);

            for (int i = 0; i < downloadManager.NumberOfConcurrentDownloads + 1; ++i)
            {
                string url = $"http://www.someplace.com/file_{i}.bin";
                if (i < downloadManager.NumberOfConcurrentDownloads)
                {
                    httpService.DownloadFileAsync(url, Arg.Any <string>(), Arg.Any <Stream>()).Returns(testObservable);
                }
                else
                {
                    httpService.DownloadFileAsync(url, Arg.Any <string>(), Arg.Any <Stream>())
                    .Returns(testObservable2);
                }

                var download = downloadManager.DownloadFile(url);
            }
            testScheduler.AdvanceBy(1);

            Assert.AreEqual(1, downloadManager.CurrentDownloadDictionary.Count);
        }
        public void CreatColdObservable_ShortWay()
        {
            var testScheduler = new TestScheduler();
            ITestableObservable <int> coldObservable =
                testScheduler.CreateColdObservable <int>(
                    // Inheritting your test class from ReactiveTest opens the following
                    // factory methods that make your code much more fluent
                    OnNext(20, 1),
                    OnNext(40, 2),
                    OnNext(60, 2),
                    OnCompleted <int>(900)
                    );

            // Creating an observer that captures the emission it recieves
            var testableObserver = testScheduler.CreateObserver <int>();

            // Subscribing the observer, but until TestSchduler is started, emissions
            // are not be emitted
            coldObservable
            .Subscribe(testableObserver);

            // Starting the TestScheduler means that only now emissions that were configured
            // will be emitted
            testScheduler.Start();

            // Asserting that every emitted value was recieved by the observer at the
            // same time it was emitted
            coldObservable.Messages
            .AssertEqual(testableObserver.Messages);

            // Asserting that the observer was subscribed at Scheduler inital time
            coldObservable.Subscriptions.AssertEqual(
                Subscribe(0));
        }
コード例 #5
0
                public override void Setup()
                {
                    base.Setup();

                    _bluetoothDeviceInfoMock.Setup(bdi => bdi.IsAuthenticated).Returns(false);
                    _pairDeviceResult = _testSchedulerProvider.Concurrent.CreateSingleValueColdObservable(true);
                    _bluetoothServiceMock.Setup(bs => bs.PairDevice(_bluetoothDeviceInfoMock.Object))
                    .Returns(_pairDeviceResult);
                }
コード例 #6
0
 private void Given_a_way_to_scan_projects()
 {
     _projectScanner  = new ProjectRescanner();
     _scheduler       = new TestScheduler();
     _inputObservable = _scheduler.CreateColdObservable(_sln.Projects
                                                        .Select((proj, i) => ReactiveTest.OnNext(i * 100 + 100, proj))
                                                        .Append(ReactiveTest.OnCompleted <Project>(_sln.Projects.Count() * 100 + 300))
                                                        .ToArray()
                                                        );
 }
コード例 #7
0
 public static void AssertAsExpected <T>(
     ITestableObservable <T> expected,
     ITestableObserver <T> actual)
 {
     ReactiveAssert.AreElementsEqual(
         expected: expected.Messages,
         actual: actual.Messages,
         message: debugElementsEqual(expected.Messages, actual.Messages)
         );
 }
コード例 #8
0
 public bool Equals(ITestableObservable <T> x, ITestableObservable <T> y)
 {
     if (x == null)
     {
         throw new ArgumentNullException(nameof(x));
     }
     if (y == null)
     {
         throw new ArgumentNullException(nameof(y));
     }
     return(Enumerable.SequenceEqual(x.Messages, y.Messages) &&
            Enumerable.SequenceEqual(x.Subscriptions, y.Subscriptions));
 }
コード例 #9
0
        public void GivenAnObservableThatPublishesIntegersAtIntervals(List <int> values, Duration period)
        {
            var      scheduler   = _scenario.GetEx <TestScheduler>();
            var      messages    = new Recorded <Notification <int> > [values.Count + 1];
            TimeSpan currentTime = TimeSpan.Zero;

            for (int i = 0; i < values.Count; i++)
            {
                currentTime = currentTime.Add(period);
                messages[i] = new Recorded <Notification <int> >(currentTime.Ticks, Notification.CreateOnNext(values[i]));
            }

            messages[values.Count] = new Recorded <Notification <int> >(currentTime.Ticks, Notification.CreateOnCompleted <int>());

            ITestableObservable <int> obs = scheduler.CreateColdObservable(messages);

            _scenario.Set(obs);
        }
コード例 #10
0
        public void DownloadFile_DoesNotRunMoreDownloadsThenNumberOfConcurrentDownloads()
        {
            TestScheduler testScheduler = new TestScheduler();
            ITestableObservable <double> testObservable = testScheduler.CreateColdObservable(new Recorded <Notification <double> >(1, Notification.CreateOnError <double>(new TestException())));

            IHttpService httpService = Substitute.For <IHttpService>();

            httpService.DownloadFileAsync(Arg.Any <string>(), Arg.Any <string>(), Arg.Any <Stream>())
            .Returns(testObservable);
            IStorage storage         = Substitute.For <IStorage>();
            var      downloadManager = new Managers.DownloadManager(httpService, storage);

            for (int i = 0; i < downloadManager.NumberOfConcurrentDownloads + 1; ++i)
            {
                string url      = $"http://www.someplace.com/file_{i}.bin";
                var    download = downloadManager.DownloadFile(url);
            }


            Assert.AreEqual(1, downloadManager.DownloadQueue.Count);
        }
コード例 #11
0
        public void SetUp()
        {
            logger    = NullLogger <ProcessingPipeline> .Instance;
            scheduler = new TestScheduler();
            manager   = new Mock <IParsedReviewManager>();
            review    = new Mock <IParsedReview>();
            manager.Setup(item => item.Create()).Returns(review.Object);
            holder         = new Mock <IParsedDocumentHolder>();
            documentSource = scheduler.CreateColdObservable(
                new Recorded <Notification <IParsedDocumentHolder> >(
                    TimeSpan.FromSeconds(1).Ticks,
                    Notification.CreateOnNext(holder.Object)),
                new Recorded <Notification <IParsedDocumentHolder> >(
                    TimeSpan.FromSeconds(2).Ticks,
                    Notification.CreateOnNext(holder.Object)),
                new Recorded <Notification <IParsedDocumentHolder> >(
                    TimeSpan.FromSeconds(3).Ticks,
                    Notification.CreateOnCompleted <IParsedDocumentHolder>()));

            instance = CreateProcessingPipeline();
        }
コード例 #12
0
        public async Task DownloadFile_Download_ReportsCorrectProgressWhenContentLengthIsKnown()
        {
            TestScheduler testScheduler = new TestScheduler();
            ITestableObservable <double> testObservable = testScheduler.CreateColdObservable(new Recorded <Notification <double> >(1, Notification.CreateOnNext(0.1)),
                                                                                             new Recorded <Notification <double> >(2, Notification.CreateOnNext(0.2)),
                                                                                             new Recorded <Notification <double> >(3, Notification.CreateOnNext(0.3)),
                                                                                             new Recorded <Notification <double> >(4, Notification.CreateOnNext(0.4)),
                                                                                             new Recorded <Notification <double> >(5, Notification.CreateOnNext(0.5)));

            IHttpService httpService = Substitute.For <IHttpService>();
            IStorage     storage     = Substitute.For <IStorage>();

            httpService.DownloadFileAsync(Arg.Any <string>(), Arg.Any <string>(), Arg.Any <Stream>())
            .Returns(testObservable);

            var downloadManager = new Managers.DownloadManager(httpService, storage);

            TaskCompletionSource <bool> taskCompletionSource = new TaskCompletionSource <bool>();
            string        url      = $"http://www.someplace.com/file.bin";
            var           download = downloadManager.DownloadFile(url);
            List <double> results  = new List <double>();

            download.Progress.Subscribe(d =>
            {
                results.Add(d);
                if (results.Count == 5)
                {
                    taskCompletionSource.TrySetResult(true);
                }
            });
            testScheduler.AdvanceBy(5);
            CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(TimeSpan.FromSeconds(1));

            cancellationTokenSource.Token.Register(() =>
                                                   { taskCompletionSource.TrySetCanceled(); });

            var result = await taskCompletionSource.Task;

            Assert.AreEqual(6, results.Count);
        }
コード例 #13
0
    public void SetUp()
    {
        var windowPeriod = TimeSpan.FromSeconds(5);

        _testScheduler  = new TestScheduler();
        _sourceSequence = _testScheduler.CreateColdObservable(
            //Question does the window start when the event starts, or at time 0?
            OnNext(0.1.Seconds(), 1),
            OnNext(1.0.Seconds(), 2),
            OnNext(2.0.Seconds(), 3),
            OnNext(7.0.Seconds(), 4),
            OnCompleted <int>(100.0.Seconds())
            );
        _observer = _testScheduler.CreateObserver <int>();
        _sourceSequence
        .Window(windowPeriod, _testScheduler)
        .SelectMany(window =>
                    window.Publish(
                        shared => shared.Take(1).Concat(shared.Skip(1).TakeLast(1))
                        )
                    )
        .Subscribe(_observer);
        _testScheduler.Start();
    }
コード例 #14
0
 public FakeMessageBroker(ITestableObservable <MotionEnvelope> motionData, Dictionary <string, FakeMotionLamp> lamps)
 {
     _motionData = motionData;
     _lamps      = lamps;
 }
コード例 #15
0
 public static void ShouldNotBeCompleted <T>(this ITestableObservable <T> obs)
 {
     obs.Messages.Where(x => x.Value.Kind == NotificationKind.OnCompleted)
     .Should()
     .BeEmpty();
 }
コード例 #16
0
 private void Given_a_way_to_scan_solutions()
 {
     _solutionScanner = new SolutionRescanner();
     _scheduler       = new TestScheduler();
     _inputObservable = _scheduler.CreateColdObservable(ReactiveTest.OnNext(100, _sln), ReactiveTest.OnCompleted <Solution>(300));
 }
コード例 #17
0
 public static void AdvanceToEnd <T>(this TestScheduler scheduler, ITestableObservable <T> events)
 {
     scheduler.AdvanceTo(events.Messages.Max(x => x.Time));
 }
コード例 #18
0
ファイル: BufferTest.cs プロジェクト: akarnokd/Rx.NET
        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)
                );
        }
コード例 #19
0
 public int GetHashCode(ITestableObservable <T> obj)
 {
     return(0); // ¯\_(ツ)_/¯
 }
コード例 #20
0
 public SelectorCall(long clock, IEnumerable <int> items, ITestableObservable <Items <int> > selectedObservable)
 {
     this.clock = clock;
     this.items = items;
     this.selectedObservable = selectedObservable;
 }
コード例 #21
0
 public static void AdvanceAfterElement <T>(this TestScheduler scheduler, ITestableObservable <T> events, int elementIndex, int timeAfter = 500)
 {
     scheduler.AdvanceTo(events.Messages.ElementAt(elementIndex).Time + Time.Tics(timeAfter));
 }
コード例 #22
0
                public override void Setup()
                {
                    base.Setup();

                    _bluetoothDeviceInfoMock.Setup(bdi => bdi.IsAuthenticated).Returns(false);
                    _pairDeviceResult = _testSchedulerProvider.Concurrent.CreateSingleValueColdObservable(true);
                    _bluetoothServiceMock.Setup(bs => bs.PairDevice(_bluetoothDeviceInfoMock.Object))
                        .Returns(_pairDeviceResult);
                }
コード例 #23
0
 public static void ShouldHaveNoErrors <T>(this ITestableObservable <T> obs)
 {
     obs.Messages.Where(x => x.Value.Kind == NotificationKind.OnError)
     .Should()
     .BeEmpty();
 }
コード例 #24
0
 public static void AdvanceJustAfterEnd <T>(this TestScheduler scheduler, ITestableObservable <T> events, int timeAfter = 500)
 {
     scheduler.AdvanceTo(events.Messages.Max(x => x.Time) + Time.Tics(timeAfter));
 }