public async Task WritingToRemoteConnectionThatFailsDoesNotThrow()
        {
            var manager1 = new RedisHubLifetimeManager <MyHub>(new LoggerFactory().CreateLogger <RedisHubLifetimeManager <MyHub> >(), Options.Create(new RedisOptions()
            {
                Factory = t => new TestConnectionMultiplexer()
            }));
            var manager2 = new RedisHubLifetimeManager <MyHub>(new LoggerFactory().CreateLogger <RedisHubLifetimeManager <MyHub> >(), Options.Create(new RedisOptions()
            {
                Factory = t => new TestConnectionMultiplexer()
            }));

            using (var client = new TestClient())
            {
                // Force an exception when writing to connection
                var connectionMock = HubConnectionContextUtils.CreateMock(client.Connection);
                connectionMock.Setup(m => m.WriteAsync(It.IsAny <HubMessage>())).Throws(new Exception());
                var connection = connectionMock.Object;

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

                // This doesn't throw because there is no connection.ConnectionId on this server so it has to publish to redis.
                // And once that happens there is no way to know if the invocation was successful or not.
                await manager1.SendConnectionAsync(connection.ConnectionId, "Hello", new object[] { "World" }).OrTimeout();
            }
        }
        public async Task InvokeConnectionAsyncForLocalConnectionDoesNotPublishToRedis()
        {
            var manager1 = new RedisHubLifetimeManager <MyHub>(new LoggerFactory().CreateLogger <RedisHubLifetimeManager <MyHub> >(), Options.Create(new RedisOptions()
            {
                Factory = t => new TestConnectionMultiplexer()
            }));
            var manager2 = new RedisHubLifetimeManager <MyHub>(new LoggerFactory().CreateLogger <RedisHubLifetimeManager <MyHub> >(), Options.Create(new RedisOptions()
            {
                Factory = t => new TestConnectionMultiplexer()
            }));

            using (var client = new TestClient())
            {
                var connection = HubConnectionContextUtils.Create(client.Connection);

                // Add connection to both "servers" to see if connection receives message twice
                await manager1.OnConnectedAsync(connection).OrTimeout();

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

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

                await AssertMessageAsync(client);

                Assert.Null(client.TryRead());
            }
        }
 public async Task InvokeConnectionAsyncOnNonExistentConnectionDoesNotThrow()
 {
     var manager = new RedisHubLifetimeManager <MyHub>(new LoggerFactory().CreateLogger <RedisHubLifetimeManager <MyHub> >(),
                                                       Options.Create(new RedisOptions()
     {
         Factory = t => new TestConnectionMultiplexer()
     }));
     await manager.SendConnectionAsync("NotARealConnectionId", "Hello", new object[] { "World" }).OrTimeout();
 }
        public async Task InvokeConnectionAsyncWritesToConnectionOutput()
        {
            using (var client = new TestClient())
            {
                var manager = new RedisHubLifetimeManager <MyHub>(new LoggerFactory().CreateLogger <RedisHubLifetimeManager <MyHub> >(),
                                                                  Options.Create(new RedisOptions()
                {
                    Factory = t => new TestConnectionMultiplexer()
                }));
                var connection = HubConnectionContextUtils.Create(client.Connection);

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

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

                await AssertMessageAsync(client);
            }
        }
        public async Task WritingToLocalConnectionThatFailsDoesNotThrowException()
        {
            var manager = new RedisHubLifetimeManager <MyHub>(new LoggerFactory().CreateLogger <RedisHubLifetimeManager <MyHub> >(), Options.Create(new RedisOptions()
            {
                Factory = t => new TestConnectionMultiplexer()
            }));

            using (var client = new TestClient())
            {
                // Force an exception when writing to connection
                var connectionMock = HubConnectionContextUtils.CreateMock(client.Connection);
                connectionMock.Setup(m => m.WriteAsync(It.IsAny <HubMessage>())).Throws(new Exception("Message"));
                var connection = connectionMock.Object;

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

                await manager.SendConnectionAsync(connection.ConnectionId, "Hello", new object[] { "World" }).OrTimeout();
            }
        }
Example #6
0
        public async Task WritingToLocalConnectionThatFailsThrowsException()
        {
            var manager = new RedisHubLifetimeManager <MyHub>(new LoggerFactory().CreateLogger <RedisHubLifetimeManager <MyHub> >(), Options.Create(new RedisOptions()
            {
                Factory = t => new TestConnectionMultiplexer()
            }));

            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 connection = HubConnectionContextUtils.Create(client.Connection, new MockChannel(writer.Object));

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

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

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