Start() 공개 메소드

public Start ( IConnection connection, string connectionData, CancellationToken disconnectToken ) : Task
connection IConnection
connectionData string
disconnectToken System.Threading.CancellationToken
리턴 Task
예제 #1
0
        public async Task AutoTransportDoesNotTryAnotherTransportIfTransportFailsDuringStartRequest()
        {
            var mockConnection = new Mock <IConnection>();

            mockConnection.Setup(c => c.TotalTransportConnectTimeout).Returns(TimeSpan.FromSeconds(5));
            mockConnection.Setup(c => c.JsonSerializer).Returns(JsonSerializer.CreateDefault());

            var mockHttpClient = new Mock <IHttpClient>();

            var mockFailingTransport = new Mock <ClientTransportBase>(mockHttpClient.Object, "fakeTransport")
            {
                CallBase = true
            };

            mockFailingTransport.Protected()
            .Setup("OnStart", ItExpr.IsAny <IConnection>(), ItExpr.IsAny <string>(), ItExpr.IsAny <CancellationToken>())
            .Callback <IConnection, string, CancellationToken>((c, d, t) =>
            {
                mockFailingTransport.Object.ProcessResponse(c,
                                                            "{\"C\":\"d-C6243495-A,0|B,0|C,1|D,0\",\"S\":1,\"M\":[]}");
            });

            var exception        = new Exception("test exception");
            var waitForException = new TaskCompletionSource <object>();

            mockHttpClient
            .Setup(c => c.Get(It.IsAny <string>(), It.IsAny <Action <IRequest> >(), It.IsAny <bool>()))
            .Returns <string, Action <IRequest>, bool>(async(url, prepareRequest, isLongRunning) =>
            {
                mockFailingTransport.Object.TransportFailed(exception);

                await waitForException.Task;

                return(Mock.Of <IResponse>());
            });

            var mockTransport = new Mock <IClientTransport>();

            mockTransport.Setup(t => t.Start(It.IsAny <IConnection>(), It.IsAny <string>(), It.IsAny <CancellationToken>()))
            .Returns(Task.FromResult <object>(null));

            var autoTransport = new AutoTransport(mockHttpClient.Object,
                                                  new List <IClientTransport> {
                mockFailingTransport.Object, mockTransport.Object
            });

            var startException = await Assert.ThrowsAsync <StartException>(
                async() => await autoTransport.Start(mockConnection.Object, string.Empty, CancellationToken.None));

            waitForException.TrySetResult(null);

            Assert.Same(exception, startException.InnerException);

            mockTransport.Verify(
                t => t.Start(It.IsAny <IConnection>(), It.IsAny <string>(), It.IsAny <CancellationToken>()),
                Times.Never());
        }
예제 #2
0
        public void AutoTransportDoesNotTryAnotherTransportIfTransportFailsDuringStartRequest()
        {
            var mockConnection = new Mock<IConnection>();
            mockConnection.Setup(c => c.TotalTransportConnectTimeout).Returns(TimeSpan.FromSeconds(5));
            mockConnection.Setup(c => c.JsonSerializer).Returns(JsonSerializer.CreateDefault());

            var mockHttpClient = new Mock<IHttpClient>();

            var mockFailingTransport = new Mock<ClientTransportBase>(mockHttpClient.Object, "fakeTransport") {CallBase = true};
            mockFailingTransport.Protected()
                .Setup("OnStart", ItExpr.IsAny<IConnection>(), ItExpr.IsAny<string>(), ItExpr.IsAny<CancellationToken>())
                .Callback<IConnection, string, CancellationToken>((c, d, t) =>
                {
                    mockFailingTransport.Object.ProcessResponse(c,
                        "{\"C\":\"d-C6243495-A,0|B,0|C,1|D,0\",\"S\":1,\"M\":[]}");
                });

            var exception = new Exception("test exception");
            mockHttpClient
                .Setup(c => c.Get(It.IsAny<string>(), It.IsAny<Action<IRequest>>(), It.IsAny<bool>()))
                .Returns<string, Action<IRequest>, bool>((url, prepareRequest, isLongRunning) =>
                {
                    mockFailingTransport.Object.TransportFailed(exception);

                    return Task.FromResult(Mock.Of<IResponse>());
                });

            var mockTransport = new Mock<IClientTransport>();
            mockTransport.Setup(t => t.Start(It.IsAny<IConnection>(), It.IsAny<string>(), It.IsAny<CancellationToken>()))
                .Returns(Task.FromResult<object>(null));

            var autoTransport = new AutoTransport(mockHttpClient.Object,
                new List<IClientTransport> {mockFailingTransport.Object, mockTransport.Object});

            var startException =
                Assert.Throws<AggregateException>(() =>
                    autoTransport.Start(mockConnection.Object, string.Empty, CancellationToken.None)
                        .Wait(TimeSpan.FromSeconds(1))).InnerException;

            Assert.IsType<StartException>(startException);
            Assert.Same(exception, startException.InnerException);

            mockTransport.Verify(
                t => t.Start(It.IsAny<IConnection>(), It.IsAny<string>(), It.IsAny<CancellationToken>()),
                Times.Never());
        }
예제 #3
0
        public void CancelledTaskHandledinAutoTransport()
        {
            var tcs = new TaskCompletionSource<IResponse>();

            tcs.TrySetCanceled();

            var transport = new Mock<IClientTransport>();
            transport.Setup(t => t.Start(It.IsAny<IConnection>(), It.IsAny<string>(), CancellationToken.None))
                .Returns(tcs.Task);

            var transports = new List<IClientTransport>();
            transports.Add(transport.Object);

            var autoTransport = new AutoTransport(new DefaultHttpClient(), transports);
            var task = autoTransport.Start(new Connection("http://foo"), string.Empty, CancellationToken.None);

            Assert.IsType(typeof(OperationCanceledException), task.Exception.InnerException);
        }
예제 #4
0
        public void StartExceptionStopsAutoTransportFallback()
        {
            var errorTcs = new TaskCompletionSource<IResponse>();
            errorTcs.SetException(new StartException());

            var failingTransport = new Mock<IClientTransport>();
            failingTransport.Setup(t => t.Start(It.IsAny<IConnection>(), It.IsAny<string>(), CancellationToken.None))
                .Returns(errorTcs.Task)
                .Verifiable();

            var unusedTransport = new Mock<IClientTransport>();

            var transports = new List<IClientTransport>();
            transports.Add(failingTransport.Object);
            transports.Add(unusedTransport.Object);

            var autoTransport = new AutoTransport(new DefaultHttpClient(), transports);
            var startTask = autoTransport.Start(new Connection("http://foo"), string.Empty, CancellationToken.None);

            failingTransport.Verify();
            unusedTransport.Verify(t => t.Start(It.IsAny<IConnection>(), It.IsAny<string>(), CancellationToken.None), Times.Never());

            Assert.IsType(typeof(StartException), startTask.Exception.InnerException);
        }