public void TestStrongConnectionStatus() { using (StartVerifiableLog(out var loggerFactory, LogLevel.Warning, expectedErrors: e => true, logChecker: s => { Assert.Single(s); Assert.Equal("EndpointOffline", s[0].Write.EventId.Name); return(true); })) { var endpoint1 = new HubServiceEndpoint(); var conn1 = new TestServiceConnection(); var scf = new TestServiceConnectionFactory(endpoint1 => conn1); var container = new StrongServiceConnectionContainer(scf, 5, endpoint1, loggerFactory.CreateLogger(nameof(TestStrongConnectionStatus))); // When init, consider the endpoint as online // TODO: improve the logic Assert.True(endpoint1.Online); conn1.SetStatus(ServiceConnectionStatus.Connecting); Assert.True(endpoint1.Online); conn1.SetStatus(ServiceConnectionStatus.Connected); Assert.True(endpoint1.Online); conn1.SetStatus(ServiceConnectionStatus.Disconnected); Assert.False(endpoint1.Online); conn1.SetStatus(ServiceConnectionStatus.Connecting); Assert.False(endpoint1.Online); conn1.SetStatus(ServiceConnectionStatus.Connected); Assert.True(endpoint1.Online); } }
public async Task TestRunAzureSignalRWithDefaultRouterNegotiateWithFallback() { using (StartVerifiableLog(out var loggerFactory, LogLevel.Warning, expectedErrors: e => true, logChecker: s => { Assert.Equal(4, s.Count); Assert.True(s.All(ss => ss.Write.EventId.Name == "EndpointOffline")); Assert.Contains("Hub 'AzureSignalRTest' is now disconnected from '[tt3](Primary)http://localhost3'. Please check log for detailed info.", s.Select(ss => ss.Write.Message)); Assert.Contains("Hub 'AzureSignalRTest' is now disconnected from '[tt4](Secondary)http://localhost4'. Please check log for detailed info.", s.Select(ss => ss.Write.Message)); Assert.Contains("Hub 'chat' is now disconnected from '[tt3](Primary)http://localhost3'. Please check log for detailed info.", s.Select(ss => ss.Write.Message)); Assert.Contains("Hub 'chat' is now disconnected from '[tt4](Secondary)http://localhost4'. Please check log for detailed info.", s.Select(ss => ss.Write.Message)); return(true); })) { // Prepare the configuration var hubConfig = Utility.GetTestHubConfig(loggerFactory, "chat"); hubConfig.Resolver.Register(typeof(ILoggerFactory), () => loggerFactory); var router = new DefaultEndpointRouter(); hubConfig.Resolver.Register(typeof(IEndpointRouter), () => router); var scf = new TestServiceConnectionFactory(endpoint => { var status = endpoint.Name == "es" ? ServiceConnectionStatus.Connected : ServiceConnectionStatus.Disconnected; return(new TestServiceConnection(status)); }); hubConfig.Resolver.Register(typeof(IServiceConnectionFactory), () => scf); using (WebApp.Start(ServiceUrl, app => app.RunAzureSignalR(AppName, hubConfig, options => { options.Endpoints = new ServiceEndpoint[] { new ServiceEndpoint(ConnectionString2, EndpointType.Secondary, "es"), new ServiceEndpoint(ConnectionString3, name: "tt3"), new ServiceEndpoint(ConnectionString4, name: "tt4", type: EndpointType.Secondary), }; }))) { var connection = hubConfig.Resolver.GetService(typeof(IServiceConnectionManager)) as ServiceConnectionManager; Assert.NotNull(connection); // wait for service connections to connect or disconnect await connection.ConnectionInitializedTask.OrTimeout(); var client = new HttpClient { BaseAddress = new Uri(ServiceUrl) }; var response = await client.GetAsync("/negotiate"); Assert.Equal(HttpStatusCode.OK, response.StatusCode); var message = await response.Content.ReadAsStringAsync(); var responseObject = JsonConvert.DeserializeObject <ResponseMessage>(message); Assert.Equal("2.0", responseObject.ProtocolVersion); // The default router fallbacks to the secondary Assert.Equal("http://localhost2/aspnetclient", responseObject.RedirectUrl); } } }