|
1 | 1 | using System; |
| 2 | +using System.Collections.Concurrent; |
2 | 3 | using System.Diagnostics; |
3 | 4 | using System.Net.WebSockets; |
4 | 5 | using System.Threading; |
5 | | -using System.Threading.Tasks; |
6 | 6 | using FluentAssertions; |
7 | 7 | using GraphQL.Client.Abstractions; |
8 | 8 | using GraphQL.Client.Abstractions.Websocket; |
@@ -247,49 +247,55 @@ public async void CanHandleConnectionTimeout() { |
247 | 247 | return TimeSpan.Zero; |
248 | 248 | }; |
249 | 249 |
|
250 | | - var statusMonitor = client.WebsocketConnectionState.Monitor(); |
251 | | - statusMonitor.Should().HaveReceivedPayload().Which.Should() |
252 | | - .Be(GraphQLWebsocketConnectionState.Disconnected); |
253 | | - |
254 | | - Debug.WriteLine("creating subscription stream"); |
255 | | - IObservable<GraphQLResponse<MessageAddedSubscriptionResult>> observable = client.CreateSubscriptionStream<MessageAddedSubscriptionResult>(SubscriptionRequest, errorMonitor.Invoke); |
256 | | - |
257 | | - Debug.WriteLine("subscribing..."); |
258 | | - var tester = observable.Monitor(); |
259 | | - statusMonitor.Should().HaveReceivedPayload().Which.Should() |
260 | | - .Be(GraphQLWebsocketConnectionState.Connecting); |
261 | | - statusMonitor.Should().HaveReceivedPayload().Which.Should() |
262 | | - .Be(GraphQLWebsocketConnectionState.Connected); |
263 | | - callbackMonitor.Should().HaveBeenInvokedWithPayload(); |
264 | | - const string message1 = "Hello World"; |
265 | | - |
266 | | - var response = await client.AddMessageAsync(message1).ConfigureAwait(false); |
267 | | - response.Data.AddMessage.Content.Should().Be(message1); |
268 | | - tester.Should().HaveReceivedPayload() |
269 | | - .Which.Data.MessageAdded.Content.Should().Be(message1); |
270 | | - |
271 | | - Debug.WriteLine("stopping web host..."); |
272 | | - await server.StopAsync(CancellationToken.None).ConfigureAwait(false); |
273 | | - server.Dispose(); |
274 | | - Debug.WriteLine("web host stopped..."); |
275 | | - |
276 | | - errorMonitor.Should().HaveBeenInvokedWithPayload(TimeSpan.FromSeconds(10)) |
277 | | - .Which.Should().BeOfType<WebSocketException>(); |
278 | | - statusMonitor.Should().HaveReceivedPayload().Which.Should() |
279 | | - .Be(GraphQLWebsocketConnectionState.Disconnected); |
280 | | - |
281 | | - server = CreateServer(port); |
282 | | - reconnectBlocker.Set(); |
283 | | - statusMonitor.Should().HaveReceivedPayload(TimeSpan.FromSeconds(10)).Which.Should() |
284 | | - .Be(GraphQLWebsocketConnectionState.Connecting); |
285 | | - statusMonitor.Should().HaveReceivedPayload(TimeSpan.FromSeconds(10)).Which.Should() |
286 | | - .Be(GraphQLWebsocketConnectionState.Connected); |
287 | | - callbackMonitor.Should().HaveBeenInvokedWithPayload(); |
288 | | - |
289 | | - // disposing the client should complete the subscription |
290 | | - client.Dispose(); |
291 | | - tester.Should().HaveCompleted(TimeSpan.FromSeconds(5)); |
292 | | - server.Dispose(); |
| 250 | + var websocketStates = new ConcurrentQueue<GraphQLWebsocketConnectionState>(); |
| 251 | + |
| 252 | + using (client.WebsocketConnectionState.Subscribe(websocketStates.Enqueue)) { |
| 253 | + websocketStates.Should().ContainSingle(state => state == GraphQLWebsocketConnectionState.Disconnected); |
| 254 | + |
| 255 | + Debug.WriteLine("creating subscription stream"); |
| 256 | + IObservable<GraphQLResponse<MessageAddedSubscriptionResult>> observable = |
| 257 | + client.CreateSubscriptionStream<MessageAddedSubscriptionResult>(SubscriptionRequest, |
| 258 | + errorMonitor.Invoke); |
| 259 | + |
| 260 | + Debug.WriteLine("subscribing..."); |
| 261 | + var tester = observable.Monitor(); |
| 262 | + callbackMonitor.Should().HaveBeenInvokedWithPayload(); |
| 263 | + |
| 264 | + websocketStates.Should().ContainInOrder( |
| 265 | + GraphQLWebsocketConnectionState.Disconnected, |
| 266 | + GraphQLWebsocketConnectionState.Connecting, |
| 267 | + GraphQLWebsocketConnectionState.Connected); |
| 268 | + // clear the collection so the next tests on the collection work as expected |
| 269 | + websocketStates.Clear(); |
| 270 | + |
| 271 | + const string message1 = "Hello World"; |
| 272 | + var response = await client.AddMessageAsync(message1).ConfigureAwait(false); |
| 273 | + response.Data.AddMessage.Content.Should().Be(message1); |
| 274 | + tester.Should().HaveReceivedPayload() |
| 275 | + .Which.Data.MessageAdded.Content.Should().Be(message1); |
| 276 | + |
| 277 | + Debug.WriteLine("stopping web host..."); |
| 278 | + await server.StopAsync(CancellationToken.None).ConfigureAwait(false); |
| 279 | + server.Dispose(); |
| 280 | + Debug.WriteLine("web host stopped..."); |
| 281 | + |
| 282 | + errorMonitor.Should().HaveBeenInvokedWithPayload(TimeSpan.FromSeconds(10)) |
| 283 | + .Which.Should().BeOfType<WebSocketException>(); |
| 284 | + websocketStates.Should().Contain(GraphQLWebsocketConnectionState.Disconnected); |
| 285 | + |
| 286 | + server = CreateServer(port); |
| 287 | + reconnectBlocker.Set(); |
| 288 | + callbackMonitor.Should().HaveBeenInvokedWithPayload(); |
| 289 | + websocketStates.Should().ContainInOrder( |
| 290 | + GraphQLWebsocketConnectionState.Disconnected, |
| 291 | + GraphQLWebsocketConnectionState.Connecting, |
| 292 | + GraphQLWebsocketConnectionState.Connected); |
| 293 | + |
| 294 | + // disposing the client should complete the subscription |
| 295 | + client.Dispose(); |
| 296 | + tester.Should().HaveCompleted(TimeSpan.FromSeconds(5)); |
| 297 | + server.Dispose(); |
| 298 | + } |
293 | 299 | } |
294 | 300 |
|
295 | 301 |
|
|
0 commit comments