示例#1
0
        private static IDependencyResolver GetDefaultResolver(IReadOnlyList <string> hubs, out IServiceConnectionManager scm)
        {
            var resolver = new DefaultDependencyResolver();

            resolver.Register(typeof(IServiceProtocol), () => new ServiceProtocol());
            var ccm = new TestClientConnectionManager();

            resolver.Register(typeof(IClientConnectionManager), () => ccm);
            var connectionManager = new ServiceConnectionManager(AppName, hubs);

            resolver.Register(typeof(IServiceConnectionManager), () => connectionManager);
            resolver.Register(typeof(IMessageParser), () => new SignalRMessageParser(hubs, resolver, NullLogger <SignalRMessageParser> .Instance));
            scm = connectionManager;
            return(resolver);
        }
示例#2
0
        public async Task ServiceConnectionDispatchTest()
        {
            int count = 0;

            using (StartVerifiableLog(out var loggerFactory, LogLevel.Debug))
            {
                var hubConfig = Utility.GetTestHubConfig(loggerFactory);
                var atm       = new AzureTransportManager(hubConfig.Resolver);
                hubConfig.Resolver.Register(typeof(ITransportManager), () => atm);

                var clientConnectionManager = new TestClientConnectionManager();
                using (var proxy = new TestServiceConnectionProxy(clientConnectionManager, loggerFactory: loggerFactory))
                {
                    // start the server connection
                    await proxy.StartServiceAsync().OrTimeout();

                    var clientConnection = Guid.NewGuid().ToString("N");

                    // Application layer sends OpenConnectionMessage
                    var openConnectionMessage = new OpenConnectionMessage(clientConnection, new Claim[0], null, "?transport=webSockets");
                    var task = proxy.WaitForClientConnectAsync(clientConnection).OrTimeout();
                    await proxy.WriteMessageAsync(openConnectionMessage);

                    await task;

                    while (count < 1000)
                    {
                        task = proxy.WaitForApplicationMessageAsync(clientConnection).OrTimeout();
                        await proxy.WriteMessageAsync(new ConnectionDataMessage(clientConnection, "Hello World".GenerateSingleFrameBuffer()));

                        await task;
                        count++;
                    }

                    task = proxy.WaitForClientDisconnectAsync(clientConnection).OrTimeout();
                    await proxy.WriteMessageAsync(new CloseConnectionMessage(clientConnection));

                    await task;

                    // Validate in transport for 1000 data messages.
                    clientConnectionManager.CurrentTransports.TryGetValue(clientConnection, out var transport);
                    Assert.NotNull(transport);
                    await transport.WaitOnDisconnected().OrTimeout();

                    Assert.Equal(transport.MessageCount, count);
                }
            }
        }
示例#3
0
        public async Task PublishConnectionDataMessagesWithLocalServiceConnectionTest(string messageKey, string messageValue, string[] availableHubs, string[] expectedHubs, string[] expectedConnectionIds)
        {
            var dr = GetDefaultResolver(availableHubs, out var scm);

            PrepareConnection(scm, out var result);

            var anotherResult = new List <ServiceMessage>();
            var anotherSCM    = new TestServiceConnectionContainer(null,
                                                                   m =>
            {
                lock (anotherResult)
                {
                    anotherResult.Add(m.Item1);
                }
            });

            var ccm = new TestClientConnectionManager(anotherSCM);

            dr.Register(typeof(IClientConnectionManager), () => ccm);

            using (var bus = new ServiceMessageBus(dr, NullLogger <ServiceMessageBus> .Instance))
            {
                await bus.Publish(SignalRMessageUtility.CreateMessage(messageKey, messageValue));
            }

            Assert.Empty(result);
            Assert.Equal(expectedHubs.Length, anotherResult.Count);
            Assert.Equal(expectedConnectionIds.Length, anotherResult.Count);

            for (var i = 0; i < expectedHubs.Length; i++)
            {
                var message = anotherResult[i] as ConnectionDataMessage;
                Assert.NotNull(message);

                Assert.Equal(expectedConnectionIds[i], message.ConnectionId);
                Assert.Equal(messageValue, message.Payload.First.GetJsonMessageFromSingleFramePayload <string>());
            }
        }