예제 #1
0
        public void WhenObservingOnDifferentSchedulerThenInvocationHappensOnDifferentThread()
        {
            var testThreadId = Thread.CurrentThread.ManagedThreadId;

            var waitHandle = new ManualResetEvent(false);
            var observable = new RestObservable <FakeItem>(new FakeAsyncRestClientFactory(), new TestSerializerFactory());

            observable
            .Create()
            .Where(x => x.StringValue == "blah")
            .ObserveOn(Scheduler.Default)
            .Subscribe(
                x =>
            {
            },
                () =>
            {
                var observerThreadId = Thread.CurrentThread.ManagedThreadId;
                if (observerThreadId != testThreadId)
                {
                    waitHandle.Set();
                }
            });

            var result = waitHandle.WaitOne(2000);

            Assert.True(result);
        }
예제 #2
0
 public void FixtureSetup()
 {
     // Tests against the sample OData service.
     _customerContext = new RestObservable <NorthwindCustomer>(
         new AsyncJsonRestClientFactory(new Uri("http://services.odata.org/Northwind/Northwind.svc/Customers")),
         new TestODataSerializerFactory());
 }
예제 #3
0
        public void CanCreateSubscription()
        {
            var waitHandle = new ManualResetEvent(false);
            var observable = new RestObservable <FakeItem>(new FakeAsyncRestClientFactory(), new TestSerializerFactory());

            observable
            .Create()
            .SubscribeOn(NewThreadScheduler.Default)
            .Where(x => x.StringValue == "blah")
            .ObserveOn(Scheduler.Default)
            .Subscribe(
                x =>
            {
                Console.WriteLine(Thread.CurrentThread.ManagedThreadId);
                waitHandle.Set();
            },
                () =>
            {
                Console.WriteLine(Thread.CurrentThread.ManagedThreadId);
                waitHandle.Set();
            });

            var result = waitHandle.WaitOne(5000);

            Assert.True(result);
        }
        public void WhenInvokingThenCallsRestClient()
        {
            var waitHandle     = new ManualResetEvent(false);
            var mockRestClient = new Mock <IAsyncRestClient>();

            mockRestClient.Setup(x => x.Download())
            .Returns(() => Task <Stream> .Factory.StartNew(() => "[]".ToStream()));

            var mockClientFactory = new Mock <IAsyncRestClientFactory>();

            mockClientFactory.SetupGet(x => x.ServiceBase).Returns(new Uri("http://localhost"));
            mockClientFactory.Setup(x => x.Create(It.IsAny <Uri>()))
            .Callback <Uri>(Console.WriteLine)
            .Returns(mockRestClient.Object);

            var subscription = new RestObservable <FakeItem>(mockClientFactory.Object, new TestSerializerFactory())
                               .Poll(Observable.Repeat(Unit.Default, 2))
                               .Where(x => x.IntValue == 2)
                               .Subscribe(x => { }, () => waitHandle.Set());

            waitHandle.WaitOne(2000);

            mockClientFactory.Verify(x => x.Create(It.IsAny <Uri>()), Times.Exactly(2));

            subscription.Dispose();
        }
예제 #5
0
        public void WhenGettingSingleThenReturnsResults()
        {
            var observable = new RestObservable <FakeItem>(new FakeAsyncRestClientFactory(), new TestSerializerFactory());
            var result     = observable
                             .Create()
                             .Where(x => x.StringValue == "blah")
                             .SingleOrDefault();

            Assert.Null(result);
        }
예제 #6
0
        public void Setup()
        {
            _mockRestClient = new Mock <IAsyncRestClient>();
            _mockRestClient.Setup(x => x.Download())
            .Returns(() => Task <Stream> .Factory.StartNew(() => "[]".ToStream()));

            _mockClientFactory = new Mock <IAsyncRestClientFactory>();
            _mockClientFactory.SetupGet(x => x.ServiceBase).Returns(new Uri("http://localhost"));
            _mockClientFactory.Setup(x => x.Create(It.IsAny <Uri>())).Returns(_mockRestClient.Object);

            _observable = new RestObservable <FakeItem>(_mockClientFactory.Object, new TestSerializerFactory());
        }
예제 #7
0
        public void WhenResultReturnedThenCompletesSubscription()
        {
            var waitHandle   = new ManualResetEvent(false);
            var observable   = new RestObservable <FakeItem>(new FakeAsyncRestClientFactory(), new TestSerializerFactory());
            var subscription = observable
                               .Create()
                               .Where(x => x.StringValue == "blah")
                               .Subscribe(x => { }, () => waitHandle.Set());

            var result = waitHandle.WaitOne();

            Assert.True(result);
        }
        public void WhenObservablePollsThenDoesNotComplete()
        {
            var waitHandle   = new ManualResetEvent(false);
            var factory      = new FakeAsyncRestClientFactory("[{\"Text\":\"blah\", \"Number\":1}]");
            var observable   = new RestObservable <FakeItem>(factory, new TestSerializerFactory());
            var subscription = observable
                               .Poll(Observable.Interval(TimeSpan.FromSeconds(0.5)).Select(x => Unit.Default))
                               .ObserveOn(TaskPoolScheduler.Default)
                               .Where(x => x.StringValue == "blah")
                               .Subscribe(x => { }, () => waitHandle.Set());

            var result = waitHandle.WaitOne(2000);

            Assert.False(result);

            subscription.Dispose();
        }
예제 #9
0
        public void WhenDisposingSubscriptionThenDoesNotExecute()
        {
            var completedWaitHandle = new ManualResetEvent(false);
            var onnextWaitHandle    = new ManualResetEvent(false);

            var observable   = new RestObservable <FakeItem>(new FakeAsyncRestClientFactory(2000), new TestSerializerFactory());
            var subscription = observable
                               .Create()
                               .Where(x => x.StringValue == "blah")
                               .ObserveOn(Scheduler.CurrentThread)
                               .Subscribe(x => onnextWaitHandle.Set(), () => completedWaitHandle.Set());

            subscription.Dispose();

            var next      = onnextWaitHandle.WaitOne(2000);
            var completed = completedWaitHandle.WaitOne(2000);

            Assert.False(next);
            Assert.True(completed);
        }
예제 #10
0
        public void WhenGroupingSourceThenReturnsResults()
        {
            var waitHandle = new ManualResetEvent(false);
            var observable = new RestObservable <FakeItem>(new FakeAsyncRestClientFactory(), new TestSerializerFactory());

            observable
            .Create()
            .Where(x => x.StringValue == "blah")
            .GroupBy(x => x.StringValue)
            .Subscribe(
                x => { },
                e =>
            {
                Console.WriteLine(e.Message);
                Console.WriteLine(e.StackTrace);
                waitHandle.Set();
            },
                () => waitHandle.Set());

            var result = waitHandle.WaitOne();

            Assert.True(result);
        }
        public void WhenDisposingPollSubscriptionThenCompletes()
        {
            var waitHandle   = new ManualResetEvent(false);
            var factory      = new FakeAsyncRestClientFactory("[{\"Text\":\"blah\", \"Number\":1}]");
            var observable   = new RestObservable <FakeItem>(factory, new TestSerializerFactory());
            var subscription = observable
                               .Poll(Observable.Interval(TimeSpan.FromSeconds(0.5)).Select(x => Unit.Default))
                               .Where(x => x.StringValue == "blah")
                               .Subscribe(x => { }, () => waitHandle.Set());

            Task.Factory.StartNew(
                () =>
            {
                Thread.Sleep(1000);
                subscription.Dispose();
            });

            var result = waitHandle.WaitOne(2000);

            Assert.True(result);

            subscription.Dispose();
        }