public async Task MyTimeoutClass_SuccessfulGet_ReturnsResult()
    {
        var stub = new SuccessHttpServiceStub();
        var my   = new MyTimeoutClass(stub);

        var result = await my.GetStringWithTimeout("http://www.example.com/")
                     .SingleAsync();

        Assert.AreEqual("stub", result);
    }
Beispiel #2
0
        public async Task MyTimeoutClass_SuccessfulGet_ReturnsResult()
        {
            var stub   = new SuccessHttpServiceStub();
            var my     = new MyTimeOutClass(stub);
            var result = await my.GetStringWithTimeout("http://www.example.com/").SingleAsync();

            var result2 = await Observable.Return("stub").Timeout(TimeSpan.FromSeconds(1)).SingleAsync();//同上封装后的代码

            Assert.AreEqual("stub", result);
        }
Beispiel #3
0
            public async Task MyTimeoutClass_SuccessfulGet_ReturnsResult()
            {
                var scheduler = new TestScheduler();
                var stub      = new SuccessHttpServiceStub
                {
                    Scheduler = scheduler,
                    Delay     = TimeSpan.FromSeconds(0.5),
                };
                var my = new MyTimeoutClass(stub);
                //SingleAsync可用来做简单的单元测试,它返回一个Task<T>对象,该对象在下一个事件到达时完成
                // var result = await my.GetStringWithTimeout("http://kworb.net/pop/").SingleAsync();
                string result = null;

                my.GetStringWithTimeout("http://kworb.net/pop/", scheduler).Subscribe(r => { result = r; });
                scheduler.Start();
                Assert.AreEqual("stub", result);
            }
    public void MyTimeoutClass_SuccessfulGetLongDelay_ThrowsTimeoutException()
    {
        var scheduler = new TestScheduler();
        var stub      = new SuccessHttpServiceStub
        {
            Scheduler = scheduler,
            Delay     = TimeSpan.FromSeconds(1.5),
        };
        var       my     = new MyTimeoutClass(stub);
        Exception result = null;

        my.GetStringWithTimeout("http://www.example.com/", scheduler)
        .Subscribe(_ => Assert.Fail("Received value"), ex => { result = ex; });

        scheduler.Start();

        Assert.IsInstanceOfType(result, typeof(TimeoutException));
    }
    public void MyTimeoutClass_SuccessfulGetShortDelay_ReturnsResult()
    {
        var scheduler = new TestScheduler();
        var stub      = new SuccessHttpServiceStub
        {
            Scheduler = scheduler,
            Delay     = TimeSpan.FromSeconds(0.5),
        };
        var    my     = new MyTimeoutClass(stub);
        string result = null;

        my.GetStringWithTimeout("http://www.example.com/", scheduler)
        .Subscribe(r => { result = r; });

        scheduler.Start();

        Assert.AreEqual("stub", result);
    }
Beispiel #6
0
        public void TestTimeoutFailsAfterLongDelay()
        {
            var scheduler = new TestScheduler();
            var stub      = new SuccessHttpServiceStub()
            {
                Scheduler = scheduler,
                Delay     = TimeSpan.FromHours(5)
            };

            var       sut    = new Timeout(stub);
            Exception result = null;

            sut.GetStringWithTimeout("www.amazon.com", scheduler)
            .Subscribe(_ => Assert.True(false), error => result = error);

            scheduler.Start();
            Assert.IsType <TimeoutException>(result);
        }
Beispiel #7
0
        public void TestTimeoutReturnsAfterDelay()
        {
            var scheduler = new TestScheduler();
            var stub      = new SuccessHttpServiceStub()
            {
                Scheduler = scheduler,
                Delay     = TimeSpan.FromMilliseconds(1)
            };

            var    sut    = new Timeout(stub);
            string result = "Failure";

            sut.GetStringWithTimeout("www.amazon.com", scheduler)
            .Subscribe(data => result = data);

            scheduler.Start();
            Assert.Equal("Success", result);
        }
Beispiel #8
0
            public async Task MyTimeoutClass_FailedGet_PropagatesFailure()
            {
                //var stub = new FailureHttpServiceStub();
                //var my = new MyTimeoutClass(stub);
                //await ThrowsExceptionAsync<HttpRequestException>(async () =>
                //{
                //    await my.GetStringWithTimeout("http://kworb.net/pop/").SingleAsync();
                //});

                //使用调度器,测试超时的情况就很容易了
                var scheduler = new TestScheduler();
                var stub      = new SuccessHttpServiceStub
                {
                    Scheduler = scheduler,
                    Delay     = TimeSpan.FromSeconds(1.5),
                };
                Exception result = null;
                var       my     = new MyTimeoutClass(stub);

                my.GetStringWithTimeout("http://kworb.net/pop/", scheduler).Subscribe(_ => Assert.Fail("Received value"), ex => { result = ex; });
                scheduler.Start();
                Assert.IsInstanceOfType(result, typeof(TimeoutException));
            }