|
7 | 7 | using Microsoft.AspNetCore.SignalR.Specification.Tests; |
8 | 8 | using Microsoft.AspNetCore.SignalR.Tests; |
9 | 9 | using Microsoft.AspNetCore.Testing; |
| 10 | +using Microsoft.Extensions.Logging; |
10 | 11 | using Microsoft.Extensions.Logging.Abstractions; |
| 12 | +using Microsoft.Extensions.Logging.Testing; |
11 | 13 | using Microsoft.Extensions.Options; |
| 14 | +using Moq; |
12 | 15 | using Newtonsoft.Json.Linq; |
13 | 16 | using Newtonsoft.Json.Serialization; |
14 | 17 | using Xunit; |
@@ -83,6 +86,48 @@ public async Task CamelCasedJsonIsPreservedAcrossRedisBoundary() |
83 | 86 | } |
84 | 87 | } |
85 | 88 |
|
| 89 | + [Fact] |
| 90 | + public async Task ErrorFromConnectionFactoryLogsAndAllowsDisconnect() |
| 91 | + { |
| 92 | + var server = new TestRedisServer(); |
| 93 | + |
| 94 | + var testSink = new TestSink(); |
| 95 | + var logger = new TestLogger("", testSink, true); |
| 96 | + var mockLoggerFactory = new Mock<ILoggerFactory>(); |
| 97 | + mockLoggerFactory |
| 98 | + .Setup(m => m.CreateLogger(It.IsAny<string>())) |
| 99 | + .Returns((string categoryName) => (ILogger)logger); |
| 100 | + var loggerT = mockLoggerFactory.Object.CreateLogger<RedisHubLifetimeManager<Hub>>(); |
| 101 | + |
| 102 | + var manager = new RedisHubLifetimeManager<Hub>( |
| 103 | + loggerT, |
| 104 | + Options.Create(new RedisOptions() |
| 105 | + { |
| 106 | + ConnectionFactory = _ => throw new Exception("throw from connect") |
| 107 | + }), |
| 108 | + new DefaultHubProtocolResolver(new IHubProtocol[] |
| 109 | + { |
| 110 | + }, NullLogger<DefaultHubProtocolResolver>.Instance)); |
| 111 | + |
| 112 | + using (var client = new TestClient()) |
| 113 | + { |
| 114 | + var connection = HubConnectionContextUtils.Create(client.Connection); |
| 115 | + |
| 116 | + var ex = await Assert.ThrowsAsync<Exception>(() => manager.OnConnectedAsync(connection)).DefaultTimeout(); |
| 117 | + Assert.Equal("throw from connect", ex.Message); |
| 118 | + |
| 119 | + await manager.OnDisconnectedAsync(connection).DefaultTimeout(); |
| 120 | + } |
| 121 | + |
| 122 | + var logs = testSink.Writes.ToArray(); |
| 123 | + // Two of the same error logs, one for the pre-emptive attempt to connect to Redis in the ctor, and one during the OnConnectedAsync for the connection |
| 124 | + Assert.Equal(2, logs.Length); |
| 125 | + Assert.Equal("Error connecting to Redis.", logs[0].Message); |
| 126 | + Assert.Equal("throw from connect", logs[0].Exception.Message); |
| 127 | + Assert.Equal("Error connecting to Redis.", logs[1].Message); |
| 128 | + Assert.Equal("throw from connect", logs[1].Exception.Message); |
| 129 | + } |
| 130 | + |
86 | 131 | public override TestRedisServer CreateBackplane() |
87 | 132 | { |
88 | 133 | return new TestRedisServer(); |
|
0 commit comments