public async Task SendAllExceptAsyncWillCancelWithToken()
        {
            using (var client1 = new TestClient())
                using (var client2 = new TestClient(pauseWriterThreshold: 2))
                {
                    var manager     = CreateNewHubLifetimeManager();
                    var connection1 = HubConnectionContextUtils.Create(client1.Connection);
                    var connection2 = HubConnectionContextUtils.Create(client2.Connection);
                    await manager.OnConnectedAsync(connection1).OrTimeout();

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

                    var cts      = new CancellationTokenSource();
                    var sendTask = manager.SendAllExceptAsync("Hello", new object[] { "World" }, new List <string> {
                        connection1.ConnectionId
                    }, cts.Token).OrTimeout();
                    Assert.False(sendTask.IsCompleted);
                    cts.Cancel();
                    await sendTask.OrTimeout();

                    var tcs = new TaskCompletionSource <object>(TaskCreationOptions.RunContinuationsAsynchronously);
                    connection2.ConnectionAborted.Register(t =>
                    {
                        ((TaskCompletionSource <object>)t).SetResult(null);
                    }, tcs);
                    await tcs.Task.OrTimeout();

                    Assert.False(connection1.ConnectionAborted.IsCancellationRequested);
                    Assert.Null(client1.TryRead());
                }
        }
Beispiel #2
0
        public async Task SendGroupAsyncWritesToAllConnectionsInGroupOutput()
        {
            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.AddGroupAsync(connection1.ConnectionId, "gunit").OrTimeout();

                    await manager.SendGroupAsync("gunit", "Hello", new object[] { "World" }).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 async Task SendUsersAsyncWillCancelWithToken()
        {
            using (var client1 = new TestClient())
                using (var client2 = new TestClient(pauseWriterThreshold: 2))
                {
                    var manager     = CreateNewHubLifetimeManager();
                    var connection1 = HubConnectionContextUtils.Create(client1.Connection, userIdentifier: "user1");
                    var connection2 = HubConnectionContextUtils.Create(client2.Connection, userIdentifier: "user2");
                    await manager.OnConnectedAsync(connection1).OrTimeout();

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

                    var cts      = new CancellationTokenSource();
                    var sendTask = manager.SendUsersAsync(new List <string> {
                        "user1", "user2"
                    }, "Hello", new object[] { "World" }, cts.Token).OrTimeout();
                    Assert.False(sendTask.IsCompleted);
                    cts.Cancel();
                    await sendTask.OrTimeout();

                    var message = Assert.IsType <InvocationMessage>(client1.TryRead());
                    Assert.Equal("Hello", message.Target);
                    Assert.Single(message.Arguments);
                    Assert.Equal("World", (string)message.Arguments[0]);
                    var tcs = new TaskCompletionSource <object>(TaskCreationOptions.RunContinuationsAsynchronously);
                    connection2.ConnectionAborted.Register(t =>
                    {
                        ((TaskCompletionSource <object>)t).SetResult(null);
                    }, tcs);
                    await tcs.Task.OrTimeout();

                    Assert.False(connection1.ConnectionAborted.IsCancellationRequested);
                }
        }
        public async Task InvokeAllAsyncDoesNotWriteToDisconnectedConnectionsOutput()
        {
            using (var client1 = new TestClient())
                using (var client2 = new TestClient())
                {
                    var manager     = new DefaultHubLifetimeManager <MyHub>();
                    var connection1 = HubConnectionContextUtils.Create(client1.Connection);
                    var connection2 = HubConnectionContextUtils.Create(client2.Connection);

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

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

                    await manager.OnDisconnectedAsync(connection2).OrTimeout();

                    await manager.SendAllAsync("Hello", new object[] { "World" }).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 async Task SendGroupsAsyncWillCancelWithToken()
        {
            using (var client1 = new TestClient(pauseWriterThreshold: 2))
            {
                var manager     = CreateNewHubLifetimeManager();
                var connection1 = HubConnectionContextUtils.Create(client1.Connection);
                await manager.OnConnectedAsync(connection1).DefaultTimeout();

                await manager.AddToGroupAsync(connection1.ConnectionId, "group").DefaultTimeout();

                var cts      = new CancellationTokenSource();
                var sendTask = manager.SendGroupsAsync(new List <string> {
                    "group"
                }, "Hello", new object[] { "World" }, cts.Token).DefaultTimeout();
                Assert.False(sendTask.IsCompleted);
                cts.Cancel();
                await sendTask.DefaultTimeout();

                var tcs = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously);
                connection1.ConnectionAborted.Register(t =>
                {
                    ((TaskCompletionSource)t).SetResult();
                }, tcs);
                await tcs.Task.DefaultTimeout();
            }
        }
        public async Task InvokeConnectionAsyncWritesToConnectionOutput()
        {
            using (var client = new TestClient())
            {
                var manager    = new DefaultHubLifetimeManager <MyHub>();
                var connection = HubConnectionContextUtils.Create(client.Connection);

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

                await manager.SendConnectionAsync(connection.ConnectionId, "Hello", new object[] { "World" }).OrTimeout();

                var message = Assert.IsType <InvocationMessage>(client.TryRead());
                Assert.Equal("Hello", message.Target);
                Assert.Single(message.Arguments);
                Assert.Equal("World", (string)message.Arguments[0]);
            }
        }
        public async Task InvokeConnectionAsyncThrowsIfConnectionFailsToWrite()
        {
            using (var client = new TestClient())
            {
                // Force an exception when writing to connection
                var writer = new Mock <ChannelWriter <HubMessage> >();
                writer.Setup(o => o.WaitToWriteAsync(It.IsAny <CancellationToken>())).Throws(new Exception("Message"));

                var manager    = new DefaultHubLifetimeManager <MyHub>();
                var connection = HubConnectionContextUtils.Create(client.Connection, new MockChannel(writer.Object));

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

                var exception = await Assert.ThrowsAsync <Exception>(() => manager.InvokeConnectionAsync(connection.ConnectionId, "Hello", new object[] { "World" }).OrTimeout());

                Assert.Equal("Message", exception.Message);
            }
        }