Exemple #1
0
        private (IChatClient, IChatTransport) CreateClient(CancellationToken token = default)
        {
            var clientToken = CancellationTokenSource.CreateLinkedTokenSource(testsLifetime.Token, token).Token;

            var(connectedClient, clientTransport) = TestingTransport.CreatePair();
            var chatClient = clientFactory.CreateClient(clientTransport);

            pendingTasks.Add(chatClient.RunAsync(clientToken));
            return(chatClient, connectedClient);
        }
        public async Task TestSuccessfulExchange()
        {
            var(wrappedTransport, remoteTransport) = TestingTransport.CreatePair();
            var testLifetime      = new CancellationTokenSource();
            var exchangeTransport = new ChatExchangeTransport(wrappedTransport);
            var respondingTask    = Task.Run(async() =>
            {
                var request = await remoteTransport.Receive(testLifetime.Token);
                if (request is ExchangeMessage {
                    Message: RequestMessage
                } exchangeMessage)
                {
                    var responseMessage = exchangeMessage.MakeResponse(new ResponseMessage());
                    await remoteTransport.Send(responseMessage, testLifetime.Token);
                }
            }, testLifetime.Token);

            // This is needed as exchangeTransport requires someone to poll Receive in order to pump messages.
            var pumpingTask = Task.Run(async() =>
            {
                await exchangeTransport.Receive(testLifetime.Token);
            }, testLifetime.Token);

            var response = await exchangeTransport.Exchange <ResponseMessage>(new RequestMessage(), testLifetime.Token);

            Assert.IsNotNull(response);
            testLifetime.Cancel();

            try
            {
                await Task.WhenAll(respondingTask, pumpingTask);
            }
            catch (OperationCanceledException)
            {
            }
        }