コード例 #1
0
        public async Task TestEndpointManagerWithDuplicateEndpointsAndConnectionStarted()
        {
            var sem = new TestServiceEndpointManager(
                new ServiceEndpoint(ConnectionString1, EndpointType.Primary, "1"),
                new ServiceEndpoint(ConnectionString1, EndpointType.Secondary, "2"),
                new ServiceEndpoint(ConnectionString2, EndpointType.Secondary, "11"),
                new ServiceEndpoint(ConnectionString2, EndpointType.Secondary, "12")
                );
            var endpoints = sem.Endpoints;

            Assert.Equal(2, endpoints.Length);
            Assert.Equal("1", endpoints[0].Name);
            Assert.Equal("11", endpoints[1].Name);

            var router    = new TestEndpointRouter();
            var container = new MultiEndpointServiceConnectionContainer("hub",
                                                                        e => new TestBaseServiceConnectionContainer(new List <IServiceConnection> {
                new TestSimpleServiceConnection(),
                new TestSimpleServiceConnection(),
            }, e), sem, router, null);

            // All the connections started
            _ = container.StartAsync();
            await container.ConnectionInitializedTask;

            endpoints = container.GetOnlineEndpoints().ToArray();
            Assert.Equal(2, endpoints.Length);
            Assert.Equal("1", endpoints[0].Name);
            Assert.Equal("11", endpoints[1].Name);
            Assert.Equal(2, container.Connections.Count);
        }
コード例 #2
0
        public async Task TestEndpointManagerWithDuplicateEndpoints()
        {
            var sem = new TestServiceEndpointManager(
                new ServiceEndpoint(ConnectionString1, EndpointType.Primary, "1"),
                new ServiceEndpoint(ConnectionString1, EndpointType.Secondary, "2"),
                new ServiceEndpoint(ConnectionString2, EndpointType.Secondary, "11"),
                new ServiceEndpoint(ConnectionString2, EndpointType.Secondary, "12")
                );
            var endpoints = sem.Endpoints;

            Assert.Equal(2, endpoints.Length);
            Assert.Equal("1", endpoints[0].Name);
            Assert.Equal("11", endpoints[1].Name);

            var router    = new TestEndpointRouter(false);
            var container = new MultiEndpointServiceConnectionContainer("hub",
                                                                        e => new TestBaseServiceConnectionContainer(new List <IServiceConnection> {
                new TestServiceConnection(),
                new TestServiceConnection(),
            }, e), sem, router, NullLoggerFactory.Instance);

            _ = container.StartAsync();
            await container.ConnectionInitializedTask.OrTimeout();

            endpoints = container.GetOnlineEndpoints().ToArray();
            Assert.Equal(2, endpoints.Length);
        }
コード例 #3
0
        public async Task TestContainerWithOneEndpointWithAllConnectedSucceeeds()
        {
            var sem    = new TestServiceEndpointManager(new ServiceEndpoint(ConnectionString1));
            var throws = new ServiceConnectionNotActiveException();
            var router = new TestEndpointRouter(throws);

            var writeTcs = new TaskCompletionSource <object>();
            TestBaseServiceConnectionContainer innerContainer = null;
            var container = new MultiEndpointServiceConnectionContainer("hub",
                                                                        e => innerContainer = new TestBaseServiceConnectionContainer(new List <IServiceConnection> {
                new TestSimpleServiceConnection(writeAsyncTcs: writeTcs),
                new TestSimpleServiceConnection(writeAsyncTcs: writeTcs),
                new TestSimpleServiceConnection(writeAsyncTcs: writeTcs),
                new TestSimpleServiceConnection(writeAsyncTcs: writeTcs),
                new TestSimpleServiceConnection(writeAsyncTcs: writeTcs),
                new TestSimpleServiceConnection(writeAsyncTcs: writeTcs),
                new TestSimpleServiceConnection(writeAsyncTcs: writeTcs),
            }, e), sem, router, null);

            // All the connections started
            _ = container.StartAsync();
            await container.ConnectionInitializedTask;

            var task = container.WriteAckableMessageAsync(DefaultGroupMessage);
            await writeTcs.Task.OrTimeout();

            innerContainer.HandleAck(new AckMessage(1, (int)AckStatus.Ok));
            await task.OrTimeout();
        }
        public async Task TestContainerWithTwoEndpointWithAllOfflineAndConnectionStartedThrows()
        {
            var sem = new TestServiceEndpointManager(
                new ServiceEndpoint(ConnectionString1),
                new ServiceEndpoint(ConnectionString2));

            var router    = new TestEndpointRouter(false);
            var container = new MultiEndpointServiceConnectionContainer(
                e => new TestBaseServiceConnectionContainer(new List <IServiceConnection> {
                new TestServiceConnection(ServiceConnectionStatus.Disconnected),
                new TestServiceConnection(ServiceConnectionStatus.Disconnected),
                new TestServiceConnection(ServiceConnectionStatus.Disconnected),
                new TestServiceConnection(ServiceConnectionStatus.Disconnected),
                new TestServiceConnection(ServiceConnectionStatus.Disconnected),
                new TestServiceConnection(ServiceConnectionStatus.Disconnected),
                new TestServiceConnection(ServiceConnectionStatus.Disconnected),
            }, e), sem, router, null);

            _ = container.StartAsync();

            // Instead of NotActiveException, throws NotConnectedException
            await Assert.ThrowsAsync <AzureSignalRNotConnectedException>(
                () => container.WriteAsync(DefaultGroupMessage)
                );

            await Assert.ThrowsAsync <AzureSignalRNotConnectedException>(
                () => container.WriteAsync("1", DefaultGroupMessage)
                );
        }
コード例 #5
0
        public async Task TestContainerWithTwoEndpointWithAllOfflineAndConnectionStartedThrows()
        {
            var sem = new TestServiceEndpointManager(
                new ServiceEndpoint(ConnectionString1),
                new ServiceEndpoint(ConnectionString2));

            var router    = new TestEndpointRouter(false);
            var container = new MultiEndpointServiceConnectionContainer(
                e => new TestBaseServiceConnectionContainer(new List <IServiceConnection> {
                new TestServiceConnection(ServiceConnectionStatus.Disconnected),
                new TestServiceConnection(ServiceConnectionStatus.Disconnected),
                new TestServiceConnection(ServiceConnectionStatus.Disconnected),
                new TestServiceConnection(ServiceConnectionStatus.Disconnected),
                new TestServiceConnection(ServiceConnectionStatus.Disconnected),
                new TestServiceConnection(ServiceConnectionStatus.Disconnected),
                new TestServiceConnection(ServiceConnectionStatus.Disconnected),
            }, e), sem, router, null);

            _ = container.StartAsync();

            // Instead of throw, just log warning, because endpoint router can indeed returns no endpoint
            // If user wants to throw, user can had the check and throw from the router
            await container.WriteAsync(DefaultGroupMessage);

            await container.WriteAsync("1", DefaultGroupMessage);
        }
コード例 #6
0
        public async Task TestContainerWithTwoConnectedEndpointAndBadRouterThrows()
        {
            var sem = new TestServiceEndpointManager(
                new ServiceEndpoint(ConnectionString1),
                new ServiceEndpoint(ConnectionString2));

            var router    = new TestEndpointRouter(new ServiceConnectionNotActiveException());
            var container = new MultiEndpointServiceConnectionContainer("hub",
                                                                        e => new TestBaseServiceConnectionContainer(new List <IServiceConnection> {
                new TestSimpleServiceConnection(),
                new TestSimpleServiceConnection(),
                new TestSimpleServiceConnection(),
                new TestSimpleServiceConnection(),
                new TestSimpleServiceConnection(),
                new TestSimpleServiceConnection(),
                new TestSimpleServiceConnection(),
            }, e), sem, router, null);

            // All the connections started
            _ = container.StartAsync();
            await container.ConnectionInitializedTask;

            await Assert.ThrowsAsync <ServiceConnectionNotActiveException>(
                () => container.WriteAsync(DefaultGroupMessage)
                );
        }
        public void TestEndpointManagerWithDuplicateEndpointsAndConnectionStarted()
        {
            var sem = new TestServiceEndpointManager(
                new ServiceEndpoint(ConnectionString1, EndpointType.Primary, "1"),
                new ServiceEndpoint(ConnectionString1, EndpointType.Secondary, "2"),
                new ServiceEndpoint(ConnectionString2, EndpointType.Secondary, "11"),
                new ServiceEndpoint(ConnectionString2, EndpointType.Secondary, "12")
                );
            var endpoints = sem.GetAvailableEndpoints().ToArray();

            Assert.Equal(2, endpoints.Length);
            Assert.Equal("1", endpoints[0].Name);
            Assert.Equal("11", endpoints[1].Name);

            var router    = new TestEndpointRouter(false);
            var container = new MultiEndpointServiceConnectionContainer(
                e => new TestBaseServiceConnectionContainer(new List <IServiceConnection> {
                new TestServiceConnection(),
                new TestServiceConnection(),
            }, e), sem, router, null);

            // All the connections started
            _ = container.StartAsync();

            endpoints = sem.GetAvailableEndpoints().ToArray();
            Assert.Equal(2, endpoints.Length);
            Assert.Equal("1", endpoints[0].Name);
            Assert.Equal("11", endpoints[1].Name);
            Assert.Equal(2, container.Connections.Count);
        }
コード例 #8
0
        public async Task TestContainerWithTwoOfflineEndpointWriteAckableMessageSucceedsWithWarning()
        {
            using (StartVerifiableLog(out var loggerFactory, LogLevel.Warning, logChecker: logs =>
            {
                var warns = logs.Where(s => s.Write.LogLevel == LogLevel.Warning).ToList();
                Assert.Equal(2, warns.Count);
                Assert.Contains(warns, s => s.Write.Message.Contains("Message JoinGroupWithAckMessage is not sent to endpoint (Primary)http://url1 because all connections to this endpoint are offline."));
                return(true);
            }))
            {
                var sem = new TestServiceEndpointManager(
                    new ServiceEndpoint(ConnectionString1),
                    new ServiceEndpoint(ConnectionString2));

                var router    = new TestEndpointRouter();
                var container = new MultiEndpointServiceConnectionContainer("hub", e =>
                {
                    return(new TestBaseServiceConnectionContainer(new List <IServiceConnection> {
                        new TestSimpleServiceConnection(ServiceConnectionStatus.Disconnected),
                        new TestSimpleServiceConnection(ServiceConnectionStatus.Disconnected),
                        new TestSimpleServiceConnection(ServiceConnectionStatus.Disconnected),
                        new TestSimpleServiceConnection(ServiceConnectionStatus.Disconnected),
                        new TestSimpleServiceConnection(ServiceConnectionStatus.Disconnected),
                        new TestSimpleServiceConnection(ServiceConnectionStatus.Disconnected),
                        new TestSimpleServiceConnection(ServiceConnectionStatus.Disconnected),
                    }, e));
                }, sem, router, loggerFactory);

                _ = container.StartAsync();
                await container.ConnectionInitializedTask;

                await container.WriteAckableMessageAsync(DefaultGroupMessage);
            }
        }
コード例 #9
0
        public async Task TestContainerWithTwoEndpointWithAllConnectedSucceedsWithGoodRouter()
        {
            var sem = new TestServiceEndpointManager(
                new ServiceEndpoint(ConnectionString1),
                new ServiceEndpoint(ConnectionString2));

            var router     = new TestEndpointRouter();
            var writeTcs   = new TaskCompletionSource <object>();
            var containers = new Dictionary <ServiceEndpoint, TestBaseServiceConnectionContainer>();
            var container  = new MultiEndpointServiceConnectionContainer("hub",
                                                                         e => containers[e] = new TestBaseServiceConnectionContainer(new List <IServiceConnection> {
                new TestSimpleServiceConnection(writeAsyncTcs: writeTcs),
                new TestSimpleServiceConnection(writeAsyncTcs: writeTcs),
                new TestSimpleServiceConnection(writeAsyncTcs: writeTcs),
                new TestSimpleServiceConnection(writeAsyncTcs: writeTcs),
                new TestSimpleServiceConnection(writeAsyncTcs: writeTcs),
                new TestSimpleServiceConnection(writeAsyncTcs: writeTcs),
                new TestSimpleServiceConnection(writeAsyncTcs: writeTcs),
            }, e), sem, router, null);

            // All the connections started
            _ = container.StartAsync();
            await container.ConnectionInitializedTask;

            var task = container.WriteAckableMessageAsync(DefaultGroupMessage);
            await writeTcs.Task.OrTimeout();

            containers.First().Value.HandleAck(new AckMessage(1, (int)AckStatus.Ok));
            await task.OrTimeout();
        }
コード例 #10
0
        public async Task TestGetRoutedEndpointsReturnDistinctResultForMultiMessages()
        {
            var endpoints = new[]
            {
                new ServiceEndpoint(ConnectionString1, EndpointType.Primary, "1"),
                new ServiceEndpoint(ConnectionString1, EndpointType.Primary, "2"),
                new ServiceEndpoint(ConnectionString2, EndpointType.Secondary, "11"),
                new ServiceEndpoint(ConnectionString2, EndpointType.Secondary, "12")
            };

            var sem = new TestServiceEndpointManager(endpoints);

            var router    = new TestEndpointRouter(false);
            var container = new MultiEndpointServiceConnectionContainer("hub",
                                                                        e => new TestBaseServiceConnectionContainer(new List <IServiceConnection> {
                new TestServiceConnection(),
                new TestServiceConnection(),
            }, e), sem, router, null);

            // Start the container for it to disconnect
            _ = container.StartAsync();
            await container.ConnectionInitializedTask.OrTimeout();

            var result = container.GetRoutedEndpoints(new MultiGroupBroadcastDataMessage(new[] { "group1", "group2" }, null)).ToList();

            Assert.Equal(2, result.Count);

            result = container.GetRoutedEndpoints(new MultiUserDataMessage(new[] { "user1", "user2" }, null)).ToList();

            Assert.Equal(2, result.Count);
        }
コード例 #11
0
        public async Task TestContainerWithTwoEndpointWithAllOfflineAndConnectionStartedThrows()
        {
            using (StartVerifiableLog(out var loggerFactory, LogLevel.Warning))
            {
                var sem = new TestServiceEndpointManager(
                    new ServiceEndpoint(ConnectionString1),
                    new ServiceEndpoint(ConnectionString2));

                var router    = new TestEndpointRouter();
                var container = new MultiEndpointServiceConnectionContainer(
                    e => new TestBaseServiceConnectionContainer(new List <IServiceConnection> {
                    new TestServiceConnection(ServiceConnectionStatus.Disconnected),
                    new TestServiceConnection(ServiceConnectionStatus.Disconnected),
                    new TestServiceConnection(ServiceConnectionStatus.Disconnected),
                    new TestServiceConnection(ServiceConnectionStatus.Disconnected),
                    new TestServiceConnection(ServiceConnectionStatus.Disconnected),
                    new TestServiceConnection(ServiceConnectionStatus.Disconnected),
                    new TestServiceConnection(ServiceConnectionStatus.Disconnected),
                }, e), sem, router, loggerFactory);

                _ = container.StartAsync();

                // Instead of throws, it logs a warning instead, because the endpoint router can indeed filter out all the available endpoints
                // So this message not sent can be expected. Users can throw inside the router if they want
                await container.WriteAsync(DefaultGroupMessage);

                await container.WriteAsync("1", DefaultGroupMessage);
            }
        }
コード例 #12
0
        public async Task TestContainerWithTwoEndpointWithAllConnectedFailsWithBadRouter()
        {
            var sem = new TestServiceEndpointManager(
                new ServiceEndpoint(ConnectionString1),
                new ServiceEndpoint(ConnectionString2));

            var router    = new TestEndpointRouter(true);
            var container = new MultiEndpointServiceConnectionContainer("hub",
                                                                        e => new TestBaseServiceConnectionContainer(new List <IServiceConnection> {
                new TestServiceConnection(),
                new TestServiceConnection(),
                new TestServiceConnection(),
                new TestServiceConnection(),
                new TestServiceConnection(),
                new TestServiceConnection(),
                new TestServiceConnection(),
            }, e), sem, router, null);

            _ = container.StartAsync();
            await container.ConnectionInitializedTask.OrTimeout();

            await Assert.ThrowsAsync <InvalidOperationException>(
                () => container.WriteAsync(DefaultGroupMessage)
                );
        }
コード例 #13
0
        public MultiEndpointServiceConnectionContainer Connect(string hubName)
        {
            var container = new MultiEndpointServiceConnectionContainer(
                hubName,
                endpoint => new WeakServiceConnectionContainer(_connectionFactory, _connectionCount, endpoint, _loggerFactory.CreateLogger <WeakServiceConnectionContainer>()),
                _endpointManager,
                _router,
                _loggerFactory);

            container.StartAsync();
            return(container);
        }
        public async Task TestTwoEndpointsWithAllNotFoundAck()
        {
            var sem = new TestServiceEndpointManager(
                new ServiceEndpoint(ConnectionString1),
                new ServiceEndpoint(ConnectionString2, name: "online"));

            var router   = new TestEndpointRouter();
            var writeTcs = new TaskCompletionSource <object>();
            TestBaseServiceConnectionContainer innerContainer = null;
            var containers = new Dictionary <ServiceEndpoint, TestBaseServiceConnectionContainer>();
            var container  = new MultiEndpointServiceConnectionContainer("hub", e =>
            {
                if (string.IsNullOrEmpty(e.Name))
                {
                    return(containers[e] = new TestBaseServiceConnectionContainer(new List <IServiceConnection> {
                        new TestSimpleServiceConnection(writeAsyncTcs: writeTcs),
                        new TestSimpleServiceConnection(writeAsyncTcs: writeTcs),
                        new TestSimpleServiceConnection(writeAsyncTcs: writeTcs),
                        new TestSimpleServiceConnection(writeAsyncTcs: writeTcs),
                        new TestSimpleServiceConnection(writeAsyncTcs: writeTcs),
                        new TestSimpleServiceConnection(writeAsyncTcs: writeTcs),
                        new TestSimpleServiceConnection(writeAsyncTcs: writeTcs),
                    }, e));
                }
                return(containers[e] = new TestBaseServiceConnectionContainer(new List <IServiceConnection> {
                    new TestSimpleServiceConnection(writeAsyncTcs: writeTcs),
                    new TestSimpleServiceConnection(writeAsyncTcs: writeTcs),
                    new TestSimpleServiceConnection(writeAsyncTcs: writeTcs),
                    new TestSimpleServiceConnection(writeAsyncTcs: writeTcs),
                    new TestSimpleServiceConnection(writeAsyncTcs: writeTcs),
                    new TestSimpleServiceConnection(writeAsyncTcs: writeTcs),
                    new TestSimpleServiceConnection(writeAsyncTcs: writeTcs),
                }, e));
            }, sem, router, null);

            // All the connections started
            _ = container.StartAsync();
            await container.ConnectionInitializedTask;

            var task = container.WriteAckableMessageAsync(DefaultGroupMessage);
            await writeTcs.Task.OrTimeout();

            foreach (var c in containers)
            {
                c.Value.HandleAck(new AckMessage(1, (int)AckStatus.NotFound));
            }
            var result = await task.OrTimeout();

            Assert.False(result);
        }
        public async Task TestTwoEndpointsWithCancellationToken()
        {
            var sem = new TestServiceEndpointManager(
                new ServiceEndpoint(ConnectionString1),
                new ServiceEndpoint(ConnectionString2, name: "online"));

            var router   = new TestEndpointRouter();
            var writeTcs = new TaskCompletionSource <object>();
            TestBaseServiceConnectionContainer innerContainer = null;
            var containers = new Dictionary <ServiceEndpoint, TestBaseServiceConnectionContainer>();
            var container  = new MultiEndpointServiceConnectionContainer("hub", e =>
            {
                if (string.IsNullOrEmpty(e.Name))
                {
                    return(containers[e] = new TestBaseServiceConnectionContainer(new List <IServiceConnection> {
                        new TestSimpleServiceConnection(writeAsyncTcs: writeTcs),
                        new TestSimpleServiceConnection(writeAsyncTcs: writeTcs),
                        new TestSimpleServiceConnection(writeAsyncTcs: writeTcs),
                        new TestSimpleServiceConnection(writeAsyncTcs: writeTcs),
                        new TestSimpleServiceConnection(writeAsyncTcs: writeTcs),
                        new TestSimpleServiceConnection(writeAsyncTcs: writeTcs),
                        new TestSimpleServiceConnection(writeAsyncTcs: writeTcs),
                    }, e, new AckHandler(100, 200)));
                }
                return(containers[e] = new TestBaseServiceConnectionContainer(new List <IServiceConnection> {
                    new TestSimpleServiceConnection(writeAsyncTcs: writeTcs),
                    new TestSimpleServiceConnection(writeAsyncTcs: writeTcs),
                    new TestSimpleServiceConnection(writeAsyncTcs: writeTcs),
                    new TestSimpleServiceConnection(writeAsyncTcs: writeTcs),
                    new TestSimpleServiceConnection(writeAsyncTcs: writeTcs),
                    new TestSimpleServiceConnection(writeAsyncTcs: writeTcs),
                    new TestSimpleServiceConnection(writeAsyncTcs: writeTcs),
                }, e, new AckHandler(100, 200)));
            }, sem, router, null);

            // All the connections started
            _ = container.StartAsync();
            await container.ConnectionInitializedTask;

            var task = container.WriteAckableMessageAsync(DefaultGroupMessage, new CancellationToken(true));
            await writeTcs.Task.OrTimeout();

            foreach (var c in containers)
            {
                c.Value.HandleAck(new AckMessage(1, (int)AckStatus.Timeout));
            }
            await Assert.ThrowsAnyAsync <OperationCanceledException>(async() => await task).OrTimeout();
        }
コード例 #16
0
        public async Task TestContainerWithTwoEndpointWithPrimaryOfflineAndConnectionStartedSucceeds()
        {
            var sem = new TestServiceEndpointManager(
                new ServiceEndpoint(ConnectionString1),
                new ServiceEndpoint(ConnectionString2, EndpointType.Secondary, "online"));

            var router     = new TestEndpointRouter();
            var writeTcs   = new TaskCompletionSource <object>();
            var containers = new Dictionary <ServiceEndpoint, TestBaseServiceConnectionContainer>();
            var container  = new MultiEndpointServiceConnectionContainer("hub", e =>
            {
                if (string.IsNullOrEmpty(e.Name))
                {
                    return(containers[e] = new TestBaseServiceConnectionContainer(new List <IServiceConnection> {
                        new TestSimpleServiceConnection(ServiceConnectionStatus.Disconnected, writeAsyncTcs: writeTcs),
                        new TestSimpleServiceConnection(ServiceConnectionStatus.Disconnected, writeAsyncTcs: writeTcs),
                        new TestSimpleServiceConnection(ServiceConnectionStatus.Disconnected, writeAsyncTcs: writeTcs),
                        new TestSimpleServiceConnection(ServiceConnectionStatus.Disconnected, writeAsyncTcs: writeTcs),
                        new TestSimpleServiceConnection(ServiceConnectionStatus.Disconnected, writeAsyncTcs: writeTcs),
                        new TestSimpleServiceConnection(ServiceConnectionStatus.Disconnected, writeAsyncTcs: writeTcs),
                        new TestSimpleServiceConnection(ServiceConnectionStatus.Disconnected, writeAsyncTcs: writeTcs),
                    }, e));
                }
                return(containers[e] = new TestBaseServiceConnectionContainer(new List <IServiceConnection> {
                    new TestSimpleServiceConnection(writeAsyncTcs: writeTcs),
                    new TestSimpleServiceConnection(writeAsyncTcs: writeTcs),
                    new TestSimpleServiceConnection(writeAsyncTcs: writeTcs),
                    new TestSimpleServiceConnection(writeAsyncTcs: writeTcs),
                    new TestSimpleServiceConnection(writeAsyncTcs: writeTcs),
                    new TestSimpleServiceConnection(writeAsyncTcs: writeTcs),
                    new TestSimpleServiceConnection(writeAsyncTcs: writeTcs),
                }, e));
            }, sem, router, null);

            _ = container.StartAsync();

            var task = container.WriteAckableMessageAsync(DefaultGroupMessage);
            await writeTcs.Task.OrTimeout();

            containers.First(p => !string.IsNullOrEmpty(p.Key.Name)).Value.HandleAck(new AckMessage(1, (int)AckStatus.Ok));
            await task.OrTimeout();

            var endpoints = container.GetOnlineEndpoints().ToArray();

            Assert.Single(endpoints);

            Assert.Equal("online", endpoints.First().Name);
        }
        public async Task TestContainerWithTwoEndpointWithPrimaryOfflineAndConnectionStartedSucceeds()
        {
            var sem = new TestServiceEndpointManager(
                new ServiceEndpoint(ConnectionString1),
                new ServiceEndpoint(ConnectionString2, EndpointType.Secondary, "online"));

            var router    = new TestEndpointRouter(false);
            var container = new MultiEndpointServiceConnectionContainer(e =>
            {
                if (string.IsNullOrEmpty(e.Name))
                {
                    return(new TestServiceConnectionContainer(new List <IServiceConnection> {
                        new TestServiceConnection(ServiceConnectionStatus.Disconnected),
                        new TestServiceConnection(ServiceConnectionStatus.Disconnected),
                        new TestServiceConnection(ServiceConnectionStatus.Disconnected),
                        new TestServiceConnection(ServiceConnectionStatus.Disconnected),
                        new TestServiceConnection(ServiceConnectionStatus.Disconnected),
                        new TestServiceConnection(ServiceConnectionStatus.Disconnected),
                        new TestServiceConnection(ServiceConnectionStatus.Disconnected),
                    }, e));
                }
                return(new TestServiceConnectionContainer(new List <IServiceConnection> {
                    new TestServiceConnection(),
                    new TestServiceConnection(),
                    new TestServiceConnection(),
                    new TestServiceConnection(),
                    new TestServiceConnection(),
                    new TestServiceConnection(),
                    new TestServiceConnection(),
                }, e));
            }, sem, router, null);

            _ = container.StartAsync();

            await container.WriteAsync(DefaultGroupMessage);

            await container.WriteAsync("1", DefaultGroupMessage);

            var endpoints = sem.GetAvailableEndpoints();

            Assert.Single(endpoints);

            endpoints = sem.GetPrimaryEndpoints();
            Assert.Single(endpoints);

            Assert.Equal("online", endpoints.First().Name);
        }
コード例 #18
0
        public async Task TestTwoEndpointsWithOneSucceededAndOtherNotAcked()
        {
            var sem = new TestServiceEndpointManager(
                new ServiceEndpoint(ConnectionString1),
                new ServiceEndpoint(ConnectionString2, name: "online"));

            var router     = new TestEndpointRouter();
            var writeTcs   = new TaskCompletionSource <object>();
            var containers = new Dictionary <ServiceEndpoint, TestBaseServiceConnectionContainer>();
            var container  = new MultiEndpointServiceConnectionContainer("hub", e =>
            {
                if (string.IsNullOrEmpty(e.Name))
                {
                    return(containers[e] = new TestBaseServiceConnectionContainer(new List <IServiceConnection> {
                        new TestSimpleServiceConnection(writeAsyncTcs: writeTcs),
                        new TestSimpleServiceConnection(writeAsyncTcs: writeTcs),
                        new TestSimpleServiceConnection(writeAsyncTcs: writeTcs),
                        new TestSimpleServiceConnection(writeAsyncTcs: writeTcs),
                        new TestSimpleServiceConnection(writeAsyncTcs: writeTcs),
                        new TestSimpleServiceConnection(writeAsyncTcs: writeTcs),
                        new TestSimpleServiceConnection(writeAsyncTcs: writeTcs),
                    }, e, new AckHandler(100, 500)));
                }
                return(containers[e] = new TestBaseServiceConnectionContainer(new List <IServiceConnection> {
                    new TestSimpleServiceConnection(writeAsyncTcs: writeTcs),
                    new TestSimpleServiceConnection(writeAsyncTcs: writeTcs),
                    new TestSimpleServiceConnection(writeAsyncTcs: writeTcs),
                    new TestSimpleServiceConnection(writeAsyncTcs: writeTcs),
                    new TestSimpleServiceConnection(writeAsyncTcs: writeTcs),
                    new TestSimpleServiceConnection(writeAsyncTcs: writeTcs),
                    new TestSimpleServiceConnection(writeAsyncTcs: writeTcs),
                }, e, new AckHandler(100, 500)));
            }, sem, router, null);

            // All the connections started
            _ = container.StartAsync();
            await container.ConnectionInitializedTask;

            var task = container.WriteAckableMessageAsync(DefaultGroupMessage);
            await writeTcs.Task.OrTimeout();

            containers.First().Value.HandleAck(new AckMessage(1, (int)AckStatus.Ok));
            var result = await task.OrTimeout();

            Assert.True(result);
        }
コード例 #19
0
        public async Task TestEndpointsForDifferentContainersHaveDifferentStatus()
        {
            var endpoints = new[]
            {
                new ServiceEndpoint(ConnectionString1, EndpointType.Primary, "1"),
                new ServiceEndpoint(ConnectionString1, EndpointType.Primary, "2"),
                new ServiceEndpoint(ConnectionString2, EndpointType.Secondary, "11"),
                new ServiceEndpoint(ConnectionString2, EndpointType.Secondary, "12")
            };

            var sem = new TestServiceEndpointManager(endpoints);

            var router     = new TestEndpointRouter(false);
            var container1 = new MultiEndpointServiceConnectionContainer(
                e => new TestBaseServiceConnectionContainer(new List <IServiceConnection> {
                new TestServiceConnection(ServiceConnectionStatus.Disconnected),
                new TestServiceConnection(ServiceConnectionStatus.Disconnected),
            }, e), sem, router, null);

            var container2 = new MultiEndpointServiceConnectionContainer(
                e => new TestBaseServiceConnectionContainer(new List <IServiceConnection> {
                new TestServiceConnection(),
                new TestServiceConnection(),
            }, e), sem, router, null);

            // Start the container for it to disconnect
            await container1.StartAsync();

            var result = container1.GetRoutedEndpoints(new MultiGroupBroadcastDataMessage(new[] { "group1", "group2" }, null)).ToList();

            Assert.Empty(result);

            result = container1.GetRoutedEndpoints(new MultiUserDataMessage(new[] { "user1", "user2" }, null)).ToList();

            Assert.Empty(result);

            result = container2.GetRoutedEndpoints(new MultiGroupBroadcastDataMessage(new[] { "group1", "group2" }, null)).ToList();

            Assert.Equal(2, result.Count);

            result = container2.GetRoutedEndpoints(new MultiUserDataMessage(new[] { "user1", "user2" }, null)).ToList();

            Assert.Equal(2, result.Count);
        }
コード例 #20
0
        public async Task TestContainerWithTwoEndpointWithPrimaryOfflineAndConnectionStartedSucceeds()
        {
            var sem = new TestServiceEndpointManager(
                new ServiceEndpoint(ConnectionString1),
                new ServiceEndpoint(ConnectionString2, EndpointType.Secondary, "online"));

            var router    = new TestEndpointRouter(false);
            var container = new MultiEndpointServiceConnectionContainer("hub", e =>
            {
                if (string.IsNullOrEmpty(e.Name))
                {
                    return(new TestBaseServiceConnectionContainer(new List <IServiceConnection> {
                        new TestServiceConnection(ServiceConnectionStatus.Disconnected),
                        new TestServiceConnection(ServiceConnectionStatus.Disconnected),
                        new TestServiceConnection(ServiceConnectionStatus.Disconnected),
                        new TestServiceConnection(ServiceConnectionStatus.Disconnected),
                        new TestServiceConnection(ServiceConnectionStatus.Disconnected),
                        new TestServiceConnection(ServiceConnectionStatus.Disconnected),
                        new TestServiceConnection(ServiceConnectionStatus.Disconnected),
                    }, e));
                }
                return(new TestBaseServiceConnectionContainer(new List <IServiceConnection> {
                    new TestServiceConnection(),
                    new TestServiceConnection(),
                    new TestServiceConnection(),
                    new TestServiceConnection(),
                    new TestServiceConnection(),
                    new TestServiceConnection(),
                    new TestServiceConnection(),
                }, e));
            }, sem, router, NullLoggerFactory.Instance);

            _ = container.StartAsync();

            await container.ConnectionInitializedTask;

            await container.WriteAsync(DefaultGroupMessage);

            var endpoints = container.GetOnlineEndpoints().ToArray();

            Assert.Single(endpoints);

            Assert.Equal("online", endpoints.First().Name);
        }
コード例 #21
0
        public async Task TestContainerWithTwoEndpointWithOneOfflineSucceeds()
        {
            using (StartVerifiableLog(out var loggerFactory, LogLevel.Warning))
            {
                var sem = new TestServiceEndpointManager(
                    new ServiceEndpoint(ConnectionString1),
                    new ServiceEndpoint(ConnectionString2, name: "online"));

                var router     = new TestEndpointRouter();
                var writeTcs   = new TaskCompletionSource <object>();
                var containers = new Dictionary <ServiceEndpoint, TestBaseServiceConnectionContainer>();
                var container  = new MultiEndpointServiceConnectionContainer("hub", e =>
                {
                    if (string.IsNullOrEmpty(e.Name))
                    {
                        return(containers[e] = new TestBaseServiceConnectionContainer(new List <IServiceConnection> {
                            new TestSimpleServiceConnection(ServiceConnectionStatus.Disconnected, writeAsyncTcs: writeTcs),
                            new TestSimpleServiceConnection(ServiceConnectionStatus.Disconnected, writeAsyncTcs: writeTcs),
                            new TestSimpleServiceConnection(ServiceConnectionStatus.Disconnected, writeAsyncTcs: writeTcs),
                            new TestSimpleServiceConnection(ServiceConnectionStatus.Disconnected, writeAsyncTcs: writeTcs),
                            new TestSimpleServiceConnection(ServiceConnectionStatus.Disconnected, writeAsyncTcs: writeTcs),
                            new TestSimpleServiceConnection(ServiceConnectionStatus.Disconnected, writeAsyncTcs: writeTcs),
                            new TestSimpleServiceConnection(ServiceConnectionStatus.Disconnected, writeAsyncTcs: writeTcs),
                        }, e));
                    }
                    return(containers[e] = new TestBaseServiceConnectionContainer(new List <IServiceConnection> {
                        new TestSimpleServiceConnection(writeAsyncTcs: writeTcs),
                        new TestSimpleServiceConnection(writeAsyncTcs: writeTcs),
                        new TestSimpleServiceConnection(writeAsyncTcs: writeTcs),
                        new TestSimpleServiceConnection(writeAsyncTcs: writeTcs),
                        new TestSimpleServiceConnection(writeAsyncTcs: writeTcs),
                        new TestSimpleServiceConnection(writeAsyncTcs: writeTcs),
                        new TestSimpleServiceConnection(writeAsyncTcs: writeTcs),
                    }, e));
                }, sem, router, loggerFactory);

                _ = container.StartAsync();
                var task = container.WriteAckableMessageAsync(DefaultGroupMessage);
                await writeTcs.Task.OrTimeout();

                containers.First(p => !string.IsNullOrEmpty(p.Key.Name)).Value.HandleAck(new AckMessage(1, (int)AckStatus.Ok));
                await task.OrTimeout();
            }
        }
コード例 #22
0
        public async Task TestContainerWithOneEndpointWithAllConnectedSucceeeds()
        {
            var sem       = new TestServiceEndpointManager(new ServiceEndpoint(ConnectionString1));
            var router    = new TestEndpointRouter(true);
            var container = new MultiEndpointServiceConnectionContainer("hub",
                                                                        e => new TestBaseServiceConnectionContainer(new List <IServiceConnection> {
                new TestServiceConnection(),
                new TestServiceConnection(),
                new TestServiceConnection(),
                new TestServiceConnection(),
                new TestServiceConnection(),
                new TestServiceConnection(),
                new TestServiceConnection(),
            }, e), sem, router, null);

            _ = container.StartAsync();
            await container.ConnectionInitializedTask.OrTimeout();

            await container.WriteAsync(DefaultGroupMessage);
        }
コード例 #23
0
        public async Task TestMultiEndpointConnectionWithNotExistEndpointRouter()
        {
            using (StartVerifiableLog(out var loggerFactory, LogLevel.Warning))
            {
                var sem = new TestServiceEndpointManager(
                    new ServiceEndpoint(ConnectionString1),
                    new ServiceEndpoint(ConnectionString2, EndpointType.Secondary, "online"));

                var router    = new NotExistEndpointRouter();
                var container = new MultiEndpointServiceConnectionContainer(e =>
                {
                    if (string.IsNullOrEmpty(e.Name))
                    {
                        return(new TestBaseServiceConnectionContainer(new List <IServiceConnection> {
                            new TestServiceConnection(ServiceConnectionStatus.Disconnected),
                            new TestServiceConnection(ServiceConnectionStatus.Disconnected),
                            new TestServiceConnection(ServiceConnectionStatus.Disconnected),
                            new TestServiceConnection(ServiceConnectionStatus.Disconnected),
                            new TestServiceConnection(ServiceConnectionStatus.Disconnected),
                            new TestServiceConnection(ServiceConnectionStatus.Disconnected),
                            new TestServiceConnection(ServiceConnectionStatus.Disconnected),
                        }, e));
                    }
                    return(new TestBaseServiceConnectionContainer(new List <IServiceConnection> {
                        new TestServiceConnection(),
                        new TestServiceConnection(),
                        new TestServiceConnection(),
                        new TestServiceConnection(),
                        new TestServiceConnection(),
                        new TestServiceConnection(),
                        new TestServiceConnection(),
                    }, e));
                }, sem, router, loggerFactory);

                _ = container.StartAsync();

                await container.WriteAsync(DefaultGroupMessage);

                await container.WriteAsync("1", DefaultGroupMessage);
            }
        }
コード例 #24
0
        public async Task TestContainerWithOneEndpointWithAllDisconnectedAndConnectionStartedThrows()
        {
            var sem       = new TestServiceEndpointManager(new ServiceEndpoint(ConnectionString1));
            var router    = new TestEndpointRouter(true);
            var container = new MultiEndpointServiceConnectionContainer("hub",
                                                                        e => new TestBaseServiceConnectionContainer(new List <IServiceConnection> {
                new TestServiceConnection(ServiceConnectionStatus.Disconnected),
                new TestServiceConnection(ServiceConnectionStatus.Disconnected),
                new TestServiceConnection(ServiceConnectionStatus.Disconnected),
                new TestServiceConnection(ServiceConnectionStatus.Disconnected),
                new TestServiceConnection(ServiceConnectionStatus.Disconnected),
                new TestServiceConnection(ServiceConnectionStatus.Disconnected),
                new TestServiceConnection(ServiceConnectionStatus.Disconnected),
            }, e), sem, router, null);

            _ = container.StartAsync();
            await container.ConnectionInitializedTask.OrTimeout();

            await Assert.ThrowsAsync <ServiceConnectionNotActiveException>(
                () => container.WriteAsync(DefaultGroupMessage)
                );
        }
コード例 #25
0
        public async Task TestContainerWithTwoEndpointWithOneOfflineAndConnectionStartedSucceeds()
        {
            var sem = new TestServiceEndpointManager(
                new ServiceEndpoint(ConnectionString1),
                new ServiceEndpoint(ConnectionString2, name: "online"));

            var router    = new TestEndpointRouter(false);
            var container = new MultiEndpointServiceConnectionContainer(e =>
            {
                if (string.IsNullOrEmpty(e.Name))
                {
                    return(new TestBaseServiceConnectionContainer(new List <IServiceConnection> {
                        new TestServiceConnection(ServiceConnectionStatus.Disconnected),
                        new TestServiceConnection(ServiceConnectionStatus.Disconnected),
                        new TestServiceConnection(ServiceConnectionStatus.Disconnected),
                        new TestServiceConnection(ServiceConnectionStatus.Disconnected),
                        new TestServiceConnection(ServiceConnectionStatus.Disconnected),
                        new TestServiceConnection(ServiceConnectionStatus.Disconnected),
                        new TestServiceConnection(ServiceConnectionStatus.Disconnected),
                    }, e));
                }
                return(new TestBaseServiceConnectionContainer(new List <IServiceConnection> {
                    new TestServiceConnection(),
                    new TestServiceConnection(),
                    new TestServiceConnection(),
                    new TestServiceConnection(),
                    new TestServiceConnection(),
                    new TestServiceConnection(),
                    new TestServiceConnection(),
                }, e));
            }, sem, router, null);

            _ = container.StartAsync();

            await container.WriteAsync(DefaultGroupMessage);

            await container.WriteAsync("1", DefaultGroupMessage);
        }
コード例 #26
0
        public async Task TestContainerWithTwoEndpointWithAllConnectedSucceedsWithGoodRouter()
        {
            var sem = new TestServiceEndpointManager(
                new ServiceEndpoint(ConnectionString1),
                new ServiceEndpoint(ConnectionString2));

            var router    = new TestEndpointRouter(false);
            var container = new MultiEndpointServiceConnectionContainer("hub",
                                                                        e => new TestBaseServiceConnectionContainer(new List <IServiceConnection> {
                new TestServiceConnection(),
                new TestServiceConnection(),
                new TestServiceConnection(),
                new TestServiceConnection(),
                new TestServiceConnection(),
                new TestServiceConnection(),
                new TestServiceConnection(),
            }, e), sem, router, NullLoggerFactory.Instance);

            _ = container.StartAsync();
            await container.ConnectionInitializedTask.OrTimeout();

            await container.WriteAsync(DefaultGroupMessage);
        }
コード例 #27
0
        public async Task TestContainerWithBadRouterThrows()
        {
            var sem       = new TestServiceEndpointManager(new ServiceEndpoint(ConnectionString1));
            var throws    = new ServiceConnectionNotActiveException();
            var router    = new TestEndpointRouter(throws);
            var container = new MultiEndpointServiceConnectionContainer("hub",
                                                                        e => new TestServiceConnectionContainer(new List <IServiceConnection> {
                new TestSimpleServiceConnection(ServiceConnectionStatus.Disconnected),
                new TestSimpleServiceConnection(ServiceConnectionStatus.Disconnected),
                new TestSimpleServiceConnection(ServiceConnectionStatus.Disconnected),
                new TestSimpleServiceConnection(ServiceConnectionStatus.Disconnected),
                new TestSimpleServiceConnection(ServiceConnectionStatus.Disconnected),
                new TestSimpleServiceConnection(ServiceConnectionStatus.Disconnected),
                new TestSimpleServiceConnection(ServiceConnectionStatus.Disconnected),
            }, e), sem, router, NullLoggerFactory.Instance);

            // All the connections started
            _ = container.StartAsync();
            await container.ConnectionInitializedTask;

            await Assert.ThrowsAsync(throws.GetType(),
                                     () => container.WriteAsync(DefaultGroupMessage)
                                     );
        }
コード例 #28
0
        public async Task TestEndpointsForDifferentContainersHaveDifferentStatus()
        {
            var endpoints = new[]
            {
                new ServiceEndpoint(ConnectionString1, EndpointType.Primary, "1"),
                new ServiceEndpoint(ConnectionString1, EndpointType.Primary, "2"),
                new ServiceEndpoint(ConnectionString2, EndpointType.Secondary, "11"),
                new ServiceEndpoint(ConnectionString2, EndpointType.Secondary, "12")
            };

            var sem = new TestServiceEndpointManager(endpoints);

            var router     = new TestEndpointRouter(false);
            var container1 = new MultiEndpointServiceConnectionContainer("hub",
                                                                         e => new TestBaseServiceConnectionContainer(new List <IServiceConnection> {
                new TestServiceConnection(ServiceConnectionStatus.Disconnected),
                new TestServiceConnection(ServiceConnectionStatus.Disconnected),
            }, e), sem, router, NullLoggerFactory.Instance);

            var container2 = new MultiEndpointServiceConnectionContainer("hub",
                                                                         e => new TestBaseServiceConnectionContainer(new List <IServiceConnection> {
                new TestServiceConnection(),
                new TestServiceConnection(),
            }, e), sem, router, NullLoggerFactory.Instance);

            var container3 = new MultiEndpointServiceConnectionContainer("hub-another",
                                                                         e => new TestBaseServiceConnectionContainer(new List <IServiceConnection> {
                new TestServiceConnection(),
                new TestServiceConnection(),
            }, e), sem, router, NullLoggerFactory.Instance);

            // Start the container for it to disconnect
            _ = container1.StartAsync();
            await container1.ConnectionInitializedTask.OrTimeout();

            // Start the container for it to disconnect
            _ = container2.StartAsync();
            await container2.ConnectionInitializedTask.OrTimeout();

            // Start the container for it to disconnect
            _ = container3.StartAsync();
            await container3.ConnectionInitializedTask.OrTimeout();

            var result = container1.GetRoutedEndpoints(new MultiGroupBroadcastDataMessage(new[] { "group1", "group2" }, null)).ToList();

            Assert.Equal(2, result.Count);

            result = container1.GetRoutedEndpoints(new MultiUserDataMessage(new[] { "user1", "user2" }, null)).ToList();

            Assert.Equal(2, result.Count);

            // The same hub shares the same endpoints
            result = container2.GetRoutedEndpoints(new MultiGroupBroadcastDataMessage(new[] { "group1", "group2" }, null)).ToList();

            Assert.Equal(2, result.Count);

            result = container2.GetRoutedEndpoints(new MultiUserDataMessage(new[] { "user1", "user2" }, null)).ToList();

            Assert.Equal(2, result.Count);

            // different hubs have different endpoint status
            result = container3.GetRoutedEndpoints(new MultiGroupBroadcastDataMessage(new[] { "group1", "group2" }, null)).ToList();

            Assert.Equal(2, result.Count);

            result = container3.GetRoutedEndpoints(new MultiUserDataMessage(new[] { "user1", "user2" }, null)).ToList();

            Assert.Equal(2, result.Count);
        }