-
Notifications
You must be signed in to change notification settings - Fork 0
/
HubConnectionTests.cs
180 lines (151 loc) · 7.48 KB
/
HubConnectionTests.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
namespace SignalRStreaming
{
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using FluentAssertions;
using Microsoft.AspNetCore.SignalR;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Newtonsoft.Json;
using SignalRStreaming.Client;
using SignalRStreaming.DTOs;
/// <summary>
/// This class contains tests that verify how the <see cref="StreamingHub"/> are invoked
/// correctly by the client.
/// </summary>
[TestClass]
public class HubConnectionTests
{
/// <summary>
/// Streaming test called with token authentication completes invoke successfully.
/// </summary>
/// <returns> A task that enables this method to be awaited. </returns>
[TestMethod]
public async Task StreamingTest_CalledWithTokenAuthentication_CompletesInvokeSuccessfully()
{
// Arrange.
ILoggerFactory loggerFactory = LoggerFactory.Create(builder => builder.AddConsole());
ILogger logger = loggerFactory.CreateLogger("StreamingTest");
void AdditionalRegistrations(IServiceCollection s)
{
s.Configure<StreamingTestOptions>((options) => options.ServerUriBase = new Uri("http://127.0.0.1"));
}
StartupTest.AdditionalRegistrations = AdditionalRegistrations;
List<OutputDTO> outputDTOs = new List<OutputDTO>();
// Configure Services.
await using (InProcessTestServer<StartupTest> testServer = await InProcessTestServer<StartupTest>.StartServer(loggerFactory))
{
// Initialize the Proxy.
ICocktailTestStreamingManager cocktailTestStreamingManager = testServer.Services.GetService<ICocktailTestStreamingManager>();
// Act.
try
{
CancellationToken cancellationToken = CancellationToken.None;
// Send the data to the Hub.
for (int ne = 1; ne <= 10; ne++)
{
InputDTO inputDTO = new InputDTO() { Count = ne };
await cocktailTestStreamingManager.SendStreamingTestStreamAsync(StreamingProcessedAsync, inputDTO, cancellationToken);
}
// Wait for the Hub to process all the data.
TimeSpan startTime = new TimeSpan(DateTime.Now.Ticks);
while (outputDTOs.Count < 10)
{
await Task.Delay(100);
}
// Assert.
List<OutputDTO> expectedOuputDTO = new List<OutputDTO>()
{
new OutputDTO() { Count = 1 },
new OutputDTO() { Count = 2 },
new OutputDTO() { Count = 3 },
new OutputDTO() { Count = 4 },
new OutputDTO() { Count = 5 },
new OutputDTO() { Count = 6 },
new OutputDTO() { Count = 7 },
new OutputDTO() { Count = 8 },
new OutputDTO() { Count = 9 },
new OutputDTO() { Count = 10 },
};
outputDTOs.Count.Should().Be(10, because: "10 objects should be streamed into the channel.");
expectedOuputDTO.Should().BeEquivalentTo(outputDTOs);
await cocktailTestStreamingManager.StopStreamingTestStreamAsync(StreamingProcessedAsync, cancellationToken);
}
catch (Exception ex)
{
logger.LogError(ex, "{ExceptionType} from test", ex.GetType().FullName);
throw;
}
}
// Take the processed result from the Hub and write the output count.
async Task StreamingProcessedAsync(OutputDTO outputDTO)
{
logger.LogInformation("The count is {0}", outputDTO.Count);
outputDTOs.Add(outputDTO);
await Task.CompletedTask.ConfigureAwait(false);
}
}
/// <summary> Streaming test called with a client method that returns an exception. </summary>
/// <returns> A task that enables this method to be awaited. </returns>
[TestMethod]
public async Task StreamingExceptionTest_ReturnCocktailHubException()
{
// Arrange.
ILoggerFactory loggerFactory = LoggerFactory.Create(builder => builder.AddConsole());
ILogger<StreamingExceptionTestStreamingHubConnectionManager> logger = loggerFactory.CreateLogger<StreamingExceptionTestStreamingHubConnectionManager>();
void AdditionalRegistrations(IServiceCollection s)
{
s.Configure<StreamingTestOptions>((options) => options.ServerUriBase = new Uri("http://127.0.0.1"));
}
StartupTest.AdditionalRegistrations = AdditionalRegistrations;
List<OutputDTO> outputDTOs = new List<OutputDTO>();
// Configure Services.
await using (InProcessTestServer<StartupTest> testServer = await InProcessTestServer<StartupTest>.StartServer(loggerFactory))
{
// Initialize the Proxy.
ICocktailTestStreamingManager cocktailTestStreamingManager = testServer.Services.GetService<ICocktailTestStreamingManager>();
CancellationToken cancellationToken = CancellationToken.None;
// Act.
try
{
// Send the data to the Hub.
for (int ne = 1; ne <= 10; ne++)
{
InputDTO inputDTO = new InputDTO() { Count = ne };
await cocktailTestStreamingManager.SendStreamingExceptionTestStreamAsync(StreamingExceptionAsync, inputDTO, cancellationToken);
}
// Assert. Wait for the Hub to process all the data.
TimeSpan startTime = new TimeSpan(DateTime.Now.Ticks);
while (outputDTOs.Count < 10)
{
double elapsedTime = new TimeSpan(DateTime.Now.Ticks).Subtract(startTime).TotalMilliseconds;
if (elapsedTime > 15000)
{
throw new TimeoutException("The channel processing time has been more than 10 seconds.");
}
await Task.Delay(100);
}
}
catch (Exception ex)
{
logger.LogError(ex, "{ExceptionType} from test", ex.GetType().FullName);
ex.Should().BeOfType(typeof(HubException));
}
finally
{
await cocktailTestStreamingManager.StopStreamingExceptionTestStreamAsync(StreamingExceptionAsync, cancellationToken);
}
}
// Take the processed result from the Hub and write the output count.
async Task StreamingExceptionAsync(OutputDTO outputDTO)
{
logger.LogInformation("The count is {0}", outputDTO.Count);
outputDTOs.Add(outputDTO);
await Task.CompletedTask.ConfigureAwait(false);
}
}
}
}