public async Task SendGroupExceptAsyncDoesNotWriteToExcludedConnections()
        {
            using (var client1 = new TestClient())
            using (var client2 = new TestClient())
            {
                var manager = new DefaultHubLifetimeManager<MyHub>(new Logger<DefaultHubLifetimeManager<MyHub>>(NullLoggerFactory.Instance));
                var connection1 = HubConnectionContextUtils.Create(client1.Connection);
                var connection2 = HubConnectionContextUtils.Create(client2.Connection);

                await manager.OnConnectedAsync(connection1).OrTimeout();
                await manager.OnConnectedAsync(connection2).OrTimeout();

                await manager.AddToGroupAsync(connection1.ConnectionId, "gunit").OrTimeout();
                await manager.AddToGroupAsync(connection2.ConnectionId, "gunit").OrTimeout();

                await manager.SendGroupExceptAsync("gunit", "Hello", new object[] { "World" }, new []{ connection2.ConnectionId }).OrTimeout();

                var message = Assert.IsType<InvocationMessage>(client1.TryRead());
                Assert.Equal("Hello", message.Target);
                Assert.Single(message.Arguments);
                Assert.Equal("World", (string)message.Arguments[0]);

                Assert.Null(client2.TryRead());
            }
        }
        public void GlobalSetup()
        {
            _hubLifetimeManager = new DefaultHubLifetimeManager <Hub>(NullLogger <DefaultHubLifetimeManager <Hub> > .Instance);

            IHubProtocol protocol;

            if (Protocol == "json")
            {
                protocol = new NewtonsoftJsonHubProtocol();
            }
            else
            {
                protocol = new MessagePackHubProtocol();
            }

            var options = new PipeOptions();

            for (var i = 0; i < Connections; ++i)
            {
                var pair          = DuplexPipe.CreateConnectionPair(options, options);
                var connection    = new DefaultConnectionContext(Guid.NewGuid().ToString(), pair.Application, pair.Transport);
                var hubConnection = new HubConnectionContext(connection, Timeout.InfiniteTimeSpan, NullLoggerFactory.Instance);
                hubConnection.Protocol = protocol;
                _hubLifetimeManager.OnConnectedAsync(hubConnection).GetAwaiter().GetResult();
                _hubLifetimeManager.AddToGroupAsync(connection.ConnectionId, TestGroupName).GetAwaiter().GetResult();

                _ = ConsumeAsync(connection.Application);
            }

            _hubContext = new HubContext <Hub>(_hubLifetimeManager);
        }
Beispiel #3
0
    public void GlobalSetup()
    {
        _hubLifetimeManager  = new DefaultHubLifetimeManager <Hub>(NullLogger <DefaultHubLifetimeManager <Hub> > .Instance);
        _connectionIds       = new List <string>();
        _subsetConnectionIds = new List <string>();
        _groupNames          = new List <string>();
        _userIdentifiers     = new List <string>();

        var jsonHubProtocol = new NewtonsoftJsonHubProtocol();

        for (int i = 0; i < 100; i++)
        {
            string connectionId   = "connection-" + i;
            string groupName      = "group-" + i % 10;
            string userIdentifier = "user-" + i % 20;
            AddUnique(_connectionIds, connectionId);
            AddUnique(_groupNames, groupName);
            AddUnique(_userIdentifiers, userIdentifier);
            if (i % 3 == 0)
            {
                _subsetConnectionIds.Add(connectionId);
            }

            var connectionContext = new TestConnectionContext
            {
                ConnectionId = connectionId,
                Transport    = new TestDuplexPipe(ForceAsync)
            };
            var contextOptions = new HubConnectionContextOptions()
            {
                KeepAliveInterval = TimeSpan.Zero,
            };
            var hubConnectionContext = new HubConnectionContext(connectionContext, contextOptions, NullLoggerFactory.Instance);
            hubConnectionContext.UserIdentifier = userIdentifier;
            hubConnectionContext.Protocol       = jsonHubProtocol;

            _hubLifetimeManager.OnConnectedAsync(hubConnectionContext).GetAwaiter().GetResult();
            _hubLifetimeManager.AddToGroupAsync(connectionId, groupName);
        }
    }
 public async Task AddGroupOnNonExistentConnectionNoops()
 {
     var manager = new DefaultHubLifetimeManager<MyHub>(new Logger<DefaultHubLifetimeManager<MyHub>>(NullLoggerFactory.Instance));
     await manager.AddToGroupAsync("NotARealConnectionId", "MyGroup").OrTimeout();
 }