public async Task WritingToGroupWithOneConnectionFailingSecondConnectionStillReceivesMessage()
        {
            using (var client1 = new TestClient())
                using (var client2 = new TestClient())
                {
                    MassTransitHubLifetimeManager <MyHub> manager = BackplaneHarness.HubLifetimeManager;

                    // Force an exception when writing to connection
                    var connectionMock = HubConnectionContextUtils.CreateMock(client1.Connection);

                    var connection1 = connectionMock;
                    var connection2 = HubConnectionContextUtils.Create(client2.Connection);

                    await manager.OnConnectedAsync(connection1).OrTimeout(Harness.TestTimeout);

                    await manager.AddToGroupAsync(connection1.ConnectionId, "group").OrTimeout(Harness.TestTimeout);

                    await manager.OnConnectedAsync(connection2).OrTimeout(Harness.TestTimeout);

                    await manager.AddToGroupAsync(connection2.ConnectionId, "group").OrTimeout(Harness.TestTimeout);

                    await manager.SendGroupAsync("group", "Hello", new object[] { "World" }).OrTimeout(Harness.TestTimeout);

                    // connection1 will throw when receiving a group message, we are making sure other connections
                    // are not affected by another connection throwing
                    await AssertMessageAsync(client2);

                    // Repeat to check that group can still be sent to
                    await manager.SendGroupAsync("group", "Hello", new object[] { "World" }).OrTimeout(Harness.TestTimeout);
                    await AssertMessageAsync(client2);
                }
        }
        public async Task AddGroupAsyncForConnectionOnDifferentServerAlreadyInGroupDoesNothing()
        {
            using (var client = new TestClient())
            {
                Task <ConsumeContext <Ack <MyHub> > > ackHandler =
                    Harness.SubscribeHandler <Ack <MyHub> >(); // Lets us verify that the ack was sent back to our bus endpoint
                MassTransitHubLifetimeManager <MyHub> manager1 = Backplane1Harness.HubLifetimeManager;
                MassTransitHubLifetimeManager <MyHub> manager2 = Backplane2Harness.HubLifetimeManager;

                var connection = HubConnectionContextUtils.Create(client.Connection);

                await manager1.OnConnectedAsync(connection).OrTimeout(Harness.TestTimeout);

                await manager1.AddToGroupAsync(connection.ConnectionId, "name").OrTimeout(Harness.TestTimeout);

                await manager2.AddToGroupAsync(connection.ConnectionId, "name").OrTimeout(Harness.TestTimeout);

                Assert.IsTrue(Backplane1Harness.GroupManagement.Consumed.Select <GroupManagement <MyHub> >().Any());

                ConsumeContext <Ack <MyHub> > responseContext = await ackHandler;

                Assert.AreEqual(manager1.ServerName, responseContext.Message.ServerName);

                await manager2.SendGroupAsync("name", "Hello", new object[] { "World" }).OrTimeout(Harness.TestTimeout);

                Assert.IsTrue(Backplane1Harness.Group.Consumed.Select <Group <MyHub> >().Any());

                await AssertMessageAsync(client);

                Assert.Null(client.TryRead());
            }
        }
        public async Task SendGroupAsyncWritesToAllConnectionsInGroupOutput()
        {
            using (var client1 = new TestClient())
                using (var client2 = new TestClient())
                {
                    MassTransitHubLifetimeManager <MyHub> manager = BackplaneHarness.HubLifetimeManager;

                    var connection1 = HubConnectionContextUtils.Create(client1.Connection);
                    var connection2 = HubConnectionContextUtils.Create(client2.Connection);

                    await manager.OnConnectedAsync(connection1).OrTimeout(Harness.TestTimeout);

                    await manager.OnConnectedAsync(connection2).OrTimeout(Harness.TestTimeout);

                    await manager.AddToGroupAsync(connection1.ConnectionId, "group").OrTimeout(Harness.TestTimeout);

                    // Because connection is local, should not have any GroupManagement
                    //Assert.IsFalse(backplaneConsumers.GroupManagementConsumer.Consumed.Select<GroupManagement<MyHub>>().Any());

                    await manager.SendGroupAsync("group", "Hello", new object[] { "World" }).OrTimeout(Harness.TestTimeout);

                    Assert.IsTrue(BackplaneHarness.Group.Consumed.Select <Group <MyHub> >().Any());

                    var message = client1.TryRead() as InvocationMessage;
                    Assert.NotNull(message);
                    Assert.AreEqual("Hello", message.Target);
                    Assert.AreEqual(1, message.Arguments.Length);
                    Assert.AreEqual("World", (string)message.Arguments[0]);

                    Assert.Null(client2.TryRead());
                }
        }
        public async Task DisconnectConnectionRemovesConnectionFromGroup()
        {
            using (var client = new TestClient())
            {
                MassTransitHubLifetimeManager <MyHub> manager = BackplaneHarness.HubLifetimeManager;

                var connection = HubConnectionContextUtils.Create(client.Connection);

                await manager.OnConnectedAsync(connection).OrTimeout(Harness.TestTimeout);

                await manager.AddToGroupAsync(connection.ConnectionId, "name").OrTimeout(Harness.TestTimeout);

                await Task.Delay(2000);

                await manager.OnDisconnectedAsync(connection).OrTimeout(Harness.TestTimeout);

                await Task.Delay(2000);

                await manager.SendGroupAsync("name", "Hello", new object[] { "World" }).OrTimeout(Harness.TestTimeout);

                await Task.Delay(2000);

                Assert.Null(client.TryRead());
            }
        }
        public async Task AddGroupAsyncForLocalConnectionAlreadyInGroupDoesNothing()
        {
            using (var client = new TestClient())
            {
                MassTransitHubLifetimeManager <MyHub> manager = BackplaneHarness.HubLifetimeManager;

                var connection = HubConnectionContextUtils.Create(client.Connection);

                await manager.OnConnectedAsync(connection).OrTimeout(Harness.TestTimeout);

                await manager.AddToGroupAsync(connection.ConnectionId, "name").OrTimeout(Harness.TestTimeout);

                await manager.AddToGroupAsync(connection.ConnectionId, "name").OrTimeout(Harness.TestTimeout);

                await manager.SendGroupAsync("name", "Hello", new object[] { "World" }).OrTimeout(Harness.TestTimeout);

                await AssertMessageAsync(client);

                Assert.Null(client.TryRead());
            }
        }
        public async Task InvokeGroupAsyncOnServerWithoutConnectionWritesOutputToGroupConnection()
        {
            using (var client = new TestClient())
            {
                MassTransitHubLifetimeManager <MyHub> manager1 = Backplane1Harness.HubLifetimeManager;
                MassTransitHubLifetimeManager <MyHub> manager2 = Backplane2Harness.HubLifetimeManager;

                var connection = HubConnectionContextUtils.Create(client.Connection);

                await manager1.OnConnectedAsync(connection).OrTimeout(Harness.TestTimeout);

                await manager1.AddToGroupAsync(connection.ConnectionId, "name").OrTimeout(Harness.TestTimeout);

                await manager2.SendGroupAsync("name", "Hello", new object[] { "World" }).OrTimeout(Harness.TestTimeout);

                Assert.IsTrue(Backplane1Harness.Group.Consumed.Select <Group <MyHub> >().Any());

                await AssertMessageAsync(client);
            }
        }