Exemple #1
0
        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.InvokeConnectionAsync(connection.ConnectionId, "Hello", new object[] { "World" }).OrTimeout();

                await connection.DisposeAsync().OrTimeout();

                await AssertMessageAsync(client);

                Assert.Null(client.TryRead());
            }
        }
Exemple #2
0
        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 writer = new Mock <ChannelWriter <HubMessage> >();
                writer.Setup(o => o.WaitToWriteAsync(It.IsAny <CancellationToken>())).Throws(new Exception());

                var connection = HubConnectionContextUtils.Create(client.Connection, new MockChannel(writer.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.InvokeConnectionAsync(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 output = Channel.CreateUnbounded <HubMessage>();

                var connection = new HubConnectionContext(output, 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.InvokeConnectionAsync(connection.ConnectionId, "Hello", new object[] { "World" }).OrTimeout();

                AssertMessage(output);
                Assert.False(output.In.TryRead(out var item));
            }
        }
        public async Task InvokeConnectionAsyncOnServerWithoutConnectionWritesOutputToConnection()
        {
            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 output = Channel.CreateUnbounded <HubMessage>();

                var connection = new HubConnectionContext(output, client.Connection);

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

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

                AssertMessage(output);
            }
        }
Exemple #5
0
 public async Task InvokeConnectionAsyncOnNonExistentConnectionDoesNotThrow()
 {
     var manager = new RedisHubLifetimeManager <MyHub>(new LoggerFactory().CreateLogger <RedisHubLifetimeManager <MyHub> >(),
                                                       Options.Create(new RedisOptions()
     {
         Factory = t => new TestConnectionMultiplexer()
     }));
     await manager.InvokeConnectionAsync("NotARealConnectionId", "Hello", new object[] { "World" }).OrTimeout();
 }
Exemple #6
0
        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.InvokeConnectionAsync(connection.ConnectionId, "Hello", new object[] { "World" }).OrTimeout();

                await connection.DisposeAsync().OrTimeout();

                await AssertMessageAsync(client);
            }
        }
        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.InvokeConnectionAsync(connection.ConnectionId, "Hello", new object[] { "World" }).OrTimeout());
                Assert.Equal("Message", exception.Message);
            }
        }