Exemplo n.º 1
0
        public async Task ServiceConnectionSendsPingMessage()
        {
            var proxy = new ServiceConnectionProxy();

            var serverTask = proxy.WaitForServerConnectionAsync(1);

            _ = proxy.StartAsync();
            await serverTask.OrTimeout();

            // TODO: make KeepAliveInterval configurable
            // Wait 6 sec and receive ping
            var pingMessageTask = proxy.WaitForApplicationMessageAsync(typeof(PingMessage));
            await Task.Delay(TimeSpan.FromSeconds(6));

            await proxy.WriteMessageAsync(new PingMessage());

            // Check server PingMessage will send after reveive service PingMessage
            await pingMessageTask.OrTimeout();

            // Wait another 6 sec and recived connection message will also trigger ping
            pingMessageTask = proxy.WaitForApplicationMessageAsync(typeof(PingMessage));
            await Task.Delay(TimeSpan.FromSeconds(6));

            await proxy.WriteMessageAsync(new OpenConnectionMessage("1", null));

            // Check server PingMessage will send after reveive service PingMessage
            await pingMessageTask.OrTimeout();

            proxy.Stop();
        }
Exemplo n.º 2
0
        public async Task WritingMultiSegmentMessageConnectionMessageWritesSingleMessage()
        {
            // 10 byte segment size
            var clientPipeOptions = new PipeOptions(minimumSegmentSize: 10);
            var proxy             = new ServiceConnectionProxy(clientPipeOptions: clientPipeOptions);

            var serverTask = proxy.WaitForServerConnectionAsync(1);

            _ = proxy.StartAsync();
            await serverTask.OrTimeout();

            var task = proxy.WaitForConnectionAsync("1");
            await proxy.WriteMessageAsync(new OpenConnectionMessage("1", null));

            var connection = await task.OrTimeout();

            var outputMessage = "This message should be more than 10 bytes";

            var dataMessageTask = proxy.WaitForApplicationMessageAsync(typeof(ConnectionDataMessage));
            await connection.Transport.Output.WriteAsync(Encoding.ASCII.GetBytes(outputMessage));

            ConnectionDataMessage message = (ConnectionDataMessage)await dataMessageTask.OrTimeout();

            Assert.Equal(message.ConnectionId, connection.ConnectionId);
            Assert.Equal(outputMessage, Encoding.ASCII.GetString(message.Payload.ToArray()));

            proxy.Stop();
        }
Exemplo n.º 3
0
        public async Task ClosingConnectionSendsCloseMessage()
        {
            var proxy = new ServiceConnectionProxy(context =>
            {
                // Just let the connection end immediately
                return(Task.CompletedTask);
            });

            var serverTask = proxy.WaitForServerConnectionAsync(1);

            _ = proxy.StartAsync();
            await serverTask.OrTimeout();

            var task             = proxy.WaitForConnectionAsync("1");
            var closeMessageTask = proxy.WaitForApplicationMessageAsync(typeof(CloseConnectionMessage));

            await proxy.WriteMessageAsync(new OpenConnectionMessage("1", null));

            var connection = await task.OrTimeout();

            CloseConnectionMessage message = (CloseConnectionMessage)await closeMessageTask.OrTimeout();

            Assert.Equal(message.ConnectionId, connection.ConnectionId);

            proxy.Stop();
        }
Exemplo n.º 4
0
        public async void ServiceLifetimeManagerIntegrationTest(string methodName, Type messageType)
        {
            var proxy = new ServiceConnectionProxy();

            var serviceConnectionManager = new ServiceConnectionManager <TestHub>();

            serviceConnectionManager.SetServiceConnection(proxy.ServiceConnectionContainer);

            var serviceLifetimeManager = new ServiceLifetimeManager <TestHub>(serviceConnectionManager,
                                                                              proxy.ClientConnectionManager, HubProtocolResolver, Logger, Marker);

            var serverTask = proxy.WaitForServerConnectionAsync(1);

            _ = proxy.StartAsync();
            await proxy.WaitForServerConnectionsInited().OrTimeout();

            await serverTask.OrTimeout();

            var task = proxy.WaitForApplicationMessageAsync(messageType);

            await InvokeMethod(serviceLifetimeManager, methodName);

            var message = await task.OrTimeout();

            VerifyServiceMessage(methodName, message);
        }
        public async void ServiceLifetimeManagerIntegrationTest(string methodName, Type messageType)
        {
            var proxy = new ServiceConnectionProxy();

            var serviceConnectionManager = new ServiceConnectionManager <TestHub>();

            serviceConnectionManager.SetServiceConnection(proxy.ServiceConnectionContainer);

            var serviceLifetimeManager = new ServiceLifetimeManager <TestHub>(serviceConnectionManager,
                                                                              proxy.ClientConnectionManager, HubProtocolResolver, Logger, Marker, _globalHubOptions, _localHubOptions);

            var serverTask = proxy.WaitForServerConnectionAsync(1);

            _ = proxy.StartAsync();
            await proxy.WaitForServerConnectionsInited().OrTimeout();

            await serverTask.OrTimeout();

            var task = proxy.WaitForApplicationMessageAsync(messageType);

            var invokeTask = InvokeMethod(serviceLifetimeManager, methodName);

            if (typeof(IAckableMessage).IsAssignableFrom(messageType))
            {
                await proxy.WriteMessageAsync(new AckMessage(1, (int)AckStatus.Ok));
            }

            // Need to return in time, or it indicate a timeout when sending ack-able messages.
            await invokeTask.OrTimeout();

            var message = await task.OrTimeout();

            VerifyServiceMessage(methodName, message);
        }
Exemplo n.º 6
0
        public async Task OnDemandConnectionSelectedToSendMessages()
        {
            var proxy = new ServiceConnectionProxy();

            var serverTask1 = proxy.WaitForServerConnectionAsync(1);

            _ = proxy.StartAsync();
            var defaultConnection = await serverTask1.OrTimeout();

            // Try to send a ping message to ask for an on-demand connection
            var serverTask2 = proxy.WaitForServerConnectionAsync(2);

            string target = "Target";
            await proxy.WriteMessageAsync(new PingMessage()
            {
                Messages = new[] { "target", target }
            });

            var onDemandConnection = (TestConnection)await serverTask2.OrTimeout();

            Assert.Equal(target, (onDemandConnection).Target);

            // pause creation of new ones
            proxy.NewConnectionsCreationPaused = true;

            // dispose the only default connection
            var serverTask3 = proxy.WaitForServerConnectionAsync(3);

            defaultConnection.Transport.Input.CancelPendingRead();
            // There won't be another connection to be created
            AssertTimeout(serverTask3);

            // now that the only connection left is on demand one verify that we still receive pings
            var pingMsgTask = proxy.WaitForApplicationMessageAsync(typeof(PingMessage));
            await Task.Delay(TimeSpan.FromSeconds(20));

            await pingMsgTask.OrTimeout();
        }
Exemplo n.º 7
0
        public async Task WritingMessagesFromConnectionGetsSentAsConnectionData()
        {
            var proxy = new ServiceConnectionProxy();

            var serverTask = proxy.WaitForServerConnectionAsync(1);

            _ = proxy.StartAsync();
            await serverTask.OrTimeout();

            var task = proxy.WaitForConnectionAsync("1");
            await proxy.WriteMessageAsync(new OpenConnectionMessage("1", null));

            var connection = await task.OrTimeout();

            var dataMessageTask = proxy.WaitForApplicationMessageAsync(typeof(ConnectionDataMessage));
            await connection.Transport.Output.WriteAsync(Encoding.ASCII.GetBytes("Hello World"));

            ConnectionDataMessage message = (ConnectionDataMessage)await dataMessageTask.OrTimeout();

            Assert.Equal(message.ConnectionId, connection.ConnectionId);
            Assert.Equal("Hello World", Encoding.ASCII.GetString(message.Payload.ToArray()));

            proxy.Stop();
        }