Ejemplo n.º 1
0
        public void 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(
                e => new TestBaseServiceConnectionContainer(new List <IServiceConnection> {
                new TestServiceConnection(),
                new TestServiceConnection(),
            }, e), sem, router, null);

            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);
        }
        public async Task TestContainerWithTwoEndpointWithAllOfflineSucceedsWithWarning()
        {
            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(string.Format(MultiEndpointServiceConnectionContainer.Log.FailedWritingMessageToEndpointTemplate, "JoinGroupWithAckMessage", "(null)", "(Primary)http://url1")));
                return(true);
            }))
            {
                var sem = new TestServiceEndpointManager(
                    new ServiceEndpoint(ConnectionString1),
                    new ServiceEndpoint(ConnectionString2));

                var router    = new TestEndpointRouter(false);
                var container = new TestMultiEndpointServiceConnectionContainer("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, loggerFactory);

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

                await container.WriteAsync(DefaultGroupMessage);
            }
        }
        public async Task TestContainerWithTwoEndpointWithAllConnectedFailsWithBadRouter()
        {
            using (StartVerifiableLog(out var loggerFactory, LogLevel.Debug, logChecker: logs => true))
            {
                var sem = new TestServiceEndpointManager(
                    new ServiceEndpoint(ConnectionString1),
                    new ServiceEndpoint(ConnectionString2));
                var logger    = loggerFactory.CreateLogger <TestServiceConnection>();
                var router    = new TestEndpointRouter(true);
                var container = new TestMultiEndpointServiceConnectionContainer("hub",
                                                                                e => new TestBaseServiceConnectionContainer(new List <IServiceConnection> {
                    new TestServiceConnection(logger: logger),
                    new TestServiceConnection(logger: logger),
                    new TestServiceConnection(logger: logger),
                    new TestServiceConnection(logger: logger),
                    new TestServiceConnection(logger: logger),
                    new TestServiceConnection(logger: logger),
                    new TestServiceConnection(logger: logger),
                }, e, logger), sem, router, loggerFactory);
                _ = Task.Run(container.StartAsync);
                await container.ConnectionInitializedTask.OrTimeout();

                await Assert.ThrowsAsync <InvalidOperationException>(
                    () => container.WriteAsync(DefaultGroupMessage)
                    );
            }
        }
        public async Task TestContainerWithTwoEndpointWithAllConnectedSucceedsWithGoodRouter()
        {
            using (StartVerifiableLog(out var loggerFactory, LogLevel.Warning, logChecker: logs => true))
            {
                var sem = new TestServiceEndpointManager(
                    new ServiceEndpoint(ConnectionString1),
                    new ServiceEndpoint(ConnectionString2));

                var router    = new TestEndpointRouter(false);
                var container = new TestMultiEndpointServiceConnectionContainer("hub",
                                                                                e => 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.ConnectionInitializedTask.OrTimeout();

                await container.WriteAsync(DefaultGroupMessage);
            }
        }
        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.Keys.OrderBy(x => x.Name).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 TestMultiEndpointServiceConnectionContainer("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().OrderBy(x => x.Name).ToArray();
            Assert.Equal(2, endpoints.Length);
            Assert.Equal("1", endpoints[0].Name);
            Assert.Equal("11", endpoints[1].Name);
        }
        public async Task TestContainerWithTwoEndpointWithAllConnectedFailsWithBadRouter()
        {
            var sem = new TestServiceEndpointManager(
                new ServiceEndpoint(ConnectionString1),
                new ServiceEndpoint(ConnectionString2));

            var router    = new TestEndpointRouter(true);
            var container = new TestMultiEndpointServiceConnectionContainer("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 Assert.ThrowsAsync <InvalidOperationException>(
                () => container.WriteAsync(DefaultGroupMessage)
                );
        }
        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 TestMultiEndpointServiceConnectionContainer("hub",
                                                                            e => new TestBaseServiceConnectionContainer(new List <IServiceConnection> {
                new TestServiceConnection(),
                new TestServiceConnection(),
            }, e), sem, router, NullLoggerFactory.Instance);

            // 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);
        }
Ejemplo n.º 8
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);
        }
Ejemplo n.º 9
0
        public async Task TestContainerWithTwoEndpointWithAllConnectedFailsWithBadRouter()
        {
            var sem = new TestServiceEndpointManager(
                new ServiceEndpoint(ConnectionString1),
                new ServiceEndpoint(ConnectionString2));

            var router    = new TestEndpointRouter(true);
            var container = new MultiEndpointServiceConnectionContainer(
                e => new TestBaseServiceConnectionContainer(new List <IServiceConnection> {
                new TestServiceConnection(),
                new TestServiceConnection(),
                new TestServiceConnection(),
                new TestServiceConnection(),
                new TestServiceConnection(),
                new TestServiceConnection(),
                new TestServiceConnection(),
            }, e), sem, router, null);
            await Assert.ThrowsAsync <InvalidOperationException>(
                () => container.WriteAsync(DefaultGroupMessage)
                );

            await Assert.ThrowsAsync <InvalidOperationException>(
                () => container.WriteAsync("1", DefaultGroupMessage)
                );
        }
Ejemplo n.º 10
0
        public async Task TestContainerWithTwoEndpointWithAllOfflineThrows()
        {
            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);

            await Assert.ThrowsAsync <ServiceConnectionNotActiveException>(
                () => container.WriteAsync(DefaultGroupMessage)
                );

            await Assert.ThrowsAsync <ServiceConnectionNotActiveException>(
                () => container.WriteAsync("1", DefaultGroupMessage)
                );
        }
Ejemplo n.º 11
0
        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.Endpoints.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 = 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);
        }
Ejemplo n.º 12
0
        public async Task TestRunAzureSignalRWithMultipleAppSettingsAndCustomSettingsAndCustomRouter()
        {
            // Prepare the configuration
            using (StartVerifiableLog(out var loggerFactory, LogLevel.Debug))
                using (new AppSettingsConfigScope(ConnectionString, ConnectionString2))
                {
                    var hubConfig = Utility.GetTestHubConfig(loggerFactory);
                    var router    = new TestEndpointRouter();
                    hubConfig.Resolver.Register(typeof(IEndpointRouter), () => router);
                    using (WebApp.Start(ServiceUrl, app => app.RunAzureSignalR(AppName, hubConfig, options =>
                    {
                        options.Endpoints = new ServiceEndpoint[]
                        {
                            new ServiceEndpoint(ConnectionString2, EndpointType.Secondary),
                            new ServiceEndpoint(ConnectionString3, name: "chosen"),
                            new ServiceEndpoint(ConnectionString4)
                        };
                    })))
                    {
                        var options = hubConfig.Resolver.Resolve <IOptions <ServiceOptions> >();

                        Assert.Equal(ConnectionString, options.Value.ConnectionString);

                        Assert.Equal(3, options.Value.Endpoints.Length);

                        var manager   = hubConfig.Resolver.Resolve <IServiceEndpointManager>();
                        var endpoints = manager.Endpoints;
                        Assert.Equal(4, endpoints.Count);

                        var client = new HttpClient {
                            BaseAddress = new Uri(ServiceUrl)
                        };
                        var response = await client.GetAsync("/negotiate?endpoint=chosen");

                        Assert.Equal(HttpStatusCode.OK, response.StatusCode);
                        var message = await response.Content.ReadAsStringAsync();

                        var responseObject = JsonConvert.DeserializeObject <ResponseMessage>(message);
                        Assert.Equal("2.0", responseObject.ProtocolVersion);

                        // with custome router, always goes to connection string 3 as passed into the router
                        Assert.Equal("http://localhost3/aspnetclient", responseObject.RedirectUrl);

                        // Invalid request
                        response = await client.GetAsync("/negotiate");

                        Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode);

                        // Invalid request
                        response = await client.GetAsync("/negotiate?endpoint=notexists");

                        Assert.Equal(HttpStatusCode.InternalServerError, response.StatusCode);
                    }
                }
        }
Ejemplo n.º 13
0
        public async Task TestContainerWithTwoEndpointWithOneOfflineSucceedsWithWarning()
        {
            using (StartVerifiableLog(out var loggerFactory, LogLevel.Warning, logChecker: logs =>
            {
                var warns = logs.Where(s => s.Write.LogLevel == LogLevel.Warning).ToList();
                Assert.Single(warns);
                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, name: "online"));

                var router    = new TestEndpointRouter(false);
                var container = new TestMultiEndpointServiceConnectionContainer("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, loggerFactory);

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

                await container.WriteAsync(DefaultGroupMessage);
            }
        }
Ejemplo n.º 14
0
        public async Task TestContainerWithOneEndpointWithAllConnectedSucceeeds()
        {
            var sem       = new TestServiceEndpointManager(new ServiceEndpoint(ConnectionString1));
            var router    = new TestEndpointRouter(true);
            var container = new MultiEndpointServiceConnectionContainer(
                e => new TestBaseServiceConnectionContainer(new List <IServiceConnection> {
                new TestServiceConnection(),
                new TestServiceConnection(),
                new TestServiceConnection(),
                new TestServiceConnection(),
                new TestServiceConnection(),
                new TestServiceConnection(),
                new TestServiceConnection(),
            }, e), sem, router, null);
            await container.WriteAsync(DefaultGroupMessage);

            await container.WriteAsync("1", DefaultGroupMessage);
        }
Ejemplo n.º 15
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);
        }
        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 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);

            var endpoints = sem.GetAvailableEndpoints();

            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 TestMultiEndpointServiceConnectionContainer("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);
        }
        public async Task TestContainerWithOneEndpointCustomizeRouterWithAllConnectedSucceeeds()
        {
            var sem       = new TestServiceEndpointManager(new ServiceEndpoint(ConnectionString1));
            var router    = new TestEndpointRouter(false);
            var container = new TestMultiEndpointServiceConnectionContainer("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);
        }
Ejemplo n.º 19
0
        public async Task TestContainerWithTwoEndpointWithOneOfflineThrows()
        {
            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);

            await Assert.ThrowsAsync <ServiceConnectionNotActiveException>(
                () => container.WriteAsync(DefaultGroupMessage)
                );

            await Assert.ThrowsAsync <ServiceConnectionNotActiveException>(
                () => container.WriteAsync("1", DefaultGroupMessage)
                );
        }
Ejemplo n.º 20
0
        public async Task TestContainerWithOneEndpointWithAllDisconnectedAndConnectionStartedThrows()
        {
            var sem       = new TestServiceEndpointManager(new ServiceEndpoint(ConnectionString1));
            var router    = new TestEndpointRouter(true);
            var container = new TestMultiEndpointServiceConnectionContainer("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, NullLoggerFactory.Instance);

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

            await Assert.ThrowsAsync <ServiceConnectionNotActiveException>(
                () => container.WriteAsync(DefaultGroupMessage)
                );
        }
Ejemplo n.º 21
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, null);

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

            await container.WriteAsync(DefaultGroupMessage);
        }
        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 TestMultiEndpointServiceConnectionContainer("hub",
                                                                             e => new TestBaseServiceConnectionContainer(new List <IServiceConnection> {
                new TestServiceConnection(ServiceConnectionStatus.Disconnected),
                new TestServiceConnection(ServiceConnectionStatus.Disconnected),
            }, e), sem, router, NullLoggerFactory.Instance);

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

            var container3 = new TestMultiEndpointServiceConnectionContainer("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);
        }