-
-
Notifications
You must be signed in to change notification settings - Fork 722
/
StarWarsOnReviewSubCompletionTest.cs
193 lines (157 loc) · 8 KB
/
StarWarsOnReviewSubCompletionTest.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
using System;
using System.Net.WebSockets;
using System.Reflection;
using System.Threading.Tasks;
using HotChocolate.AspNetCore.Tests.Utilities;
using HotChocolate.StarWars.Models;
using HotChocolate.Subscriptions;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection;
using StrawberryShake.Transport.WebSockets;
using StrawberryShake.Transport.WebSockets.Protocols;
using Xunit;
namespace StrawberryShake.CodeGeneration.CSharp.Integration.StarWarsOnReviewSubCompletion
{
public class StarWarsOnReviewSubCompletionTest : ServerTestBase
{
public StarWarsOnReviewSubCompletionTest(TestServerFactory serverFactory) : base(serverFactory)
{
}
[Fact]
public async Task Watch_StarWarsOnReviewSubCompletion_Test()
{
// arrange
using IWebHost host = TestServerHelper.CreateServer(
_ => { },
out var port);
var topicEventSender = host.Services.GetRequiredService<ITopicEventSender>();
var serviceCollection = new ServiceCollection();
serviceCollection.AddStarWarsOnReviewSubCompletionClient(
profile: StarWarsOnReviewSubCompletionClientProfileKind.Default)
.ConfigureHttpClient(
c => c.BaseAddress = new Uri("http://localhost:" + port + "/graphql"))
.ConfigureWebSocketClient(
c => c.Uri = new Uri("ws://localhost:" + port + "/graphql"));
// act
IServiceProvider services = serviceCollection.BuildServiceProvider();
IStarWarsOnReviewSubCompletionClient client = services.GetRequiredService<IStarWarsOnReviewSubCompletionClient>();
string? commentary = null;
bool completionTriggered = false;
var sub = client.OnReviewSub.Watch();
var session = sub.Subscribe(
result => commentary = result.Data?.OnReview?.Commentary,
() => completionTriggered = true);
var topic = Episode.NewHope;
// try to send message 10 times
// make sure the subscription connection is successful
for(int times = 0; commentary is null && times < 10; times++)
{
await topicEventSender.SendAsync(topic, new Review { Stars = 1, Commentary = "Commentary" });
await Task.Delay(1_000);
}
// complete the topic of subscription from server
await topicEventSender.CompleteAsync(topic);
// waiting for completion message sent
for (int times = 0; !completionTriggered && times < 10; times++)
{
await Task.Delay(1_000);
}
// assert
Assert.True(commentary is not null && completionTriggered);
session.Dispose();
}
[Fact]
public async Task Watch_StarWarsOnReviewSubCompletionPassively_Test()
{
// arrange
using IWebHost host = TestServerHelper.CreateServer(
_ => { },
out var port);
var topicEventSender = host.Services.GetRequiredService<ITopicEventSender>();
var serviceCollection = new ServiceCollection();
serviceCollection.AddStarWarsOnReviewSubCompletionClient(
profile: StarWarsOnReviewSubCompletionClientProfileKind.Default)
.ConfigureHttpClient(
c => c.BaseAddress = new Uri("http://localhost:" + port + "/graphql"))
.ConfigureWebSocketClient(
c => c.Uri = new Uri("ws://localhost:" + port + "/graphql"));
serviceCollection.AddSingleton<SubscriptionSocketStateMonitor>();
// act
IServiceProvider services = serviceCollection.BuildServiceProvider();
IStarWarsOnReviewSubCompletionClient client = services.GetRequiredService<IStarWarsOnReviewSubCompletionClient>();
string? commentary = null;
bool completionTriggered = false;
var sub = client.OnReviewSub.Watch();
var session = sub.Subscribe(
result => commentary = result.Data?.OnReview?.Commentary,
() => completionTriggered = true);
var topic = Episode.NewHope;
// try to send message 10 times
// make sure the subscription connection is successful
for (int times = 0; commentary is null && times < 10; times++)
{
await topicEventSender.SendAsync(topic, new Review { Stars = 1, Commentary = "Commentary" });
await Task.Delay(1_000);
}
// simulate network error
var monitor = services.GetRequiredService<SubscriptionSocketStateMonitor>();
monitor.AbortSocket();
//await host.StopAsync();
// waiting for completion message sent
for (int times = 0; !completionTriggered && times < 10; times++)
{
await Task.Delay(1_000);
}
// assert
Assert.True(commentary is not null && completionTriggered);
session.Dispose();
}
}
public class SubscriptionSocketStateMonitor
{
private const BindingFlags _bindingFlags = BindingFlags.NonPublic | BindingFlags.Instance;
private readonly ISessionPool _sessionPool;
private readonly Type _sessionPoolType;
private readonly FieldInfo _sessionsField;
private readonly FieldInfo _socketOperationsDictionaryField = typeof(Session).GetField("_operations", _bindingFlags)!;
private readonly FieldInfo _socketOperationManagerField = typeof(SocketOperation).GetField("_manager", _bindingFlags)!;
private readonly FieldInfo _socketProtocolField = typeof(Session)!.GetField("_socketProtocol", _bindingFlags)!;
private readonly FieldInfo _protocolReceiverField = typeof(GraphQLWebSocketProtocol).GetField("_receiver", _bindingFlags)!;
private Type? _sessionInfoType;
private PropertyInfo? _sessionProperty;
private Type? _receiverType;
private FieldInfo? _receiverClientField;
public SubscriptionSocketStateMonitor(ISessionPool sessionPool)
{
_sessionPool = sessionPool;
_sessionPoolType = _sessionPool.GetType();
_sessionsField = _sessionPoolType.GetField("_sessions", _bindingFlags)!;
}
public void AbortSocket()
{
var sessionInfos = (_sessionsField!.GetValue(_sessionPool) as System.Collections.IDictionary)!.Values;
foreach (var sessionInfo in sessionInfos)
{
_sessionInfoType ??= sessionInfo.GetType();
_sessionProperty ??= _sessionInfoType.GetProperty("Session")!;
var session = _sessionProperty.GetValue(sessionInfo) as Session;
var socketOperations = _socketOperationsDictionaryField
.GetValue(session) as System.Collections.Concurrent.ConcurrentDictionary<string, SocketOperation>;
foreach (var operation in socketOperations!)
{
var operationsession = _socketOperationManagerField.GetValue(operation.Value) as Session;
var protocol = _socketProtocolField.GetValue(operationsession) as GraphQLWebSocketProtocol;
var receiver = _protocolReceiverField.GetValue(protocol)!;
_receiverType ??= receiver.GetType();
_receiverClientField ??= _receiverType.GetField("_client", _bindingFlags)!;
var client = _receiverClientField.GetValue(receiver) as ISocketClient;
if (client!.IsClosed is false && client is WebSocketClient webSocketClient)
{
var socket = typeof(WebSocketClient).GetField("_socket", _bindingFlags)!.GetValue(webSocketClient) as ClientWebSocket;
socket!.Abort();
}
}
}
}
}
}