-
-
Notifications
You must be signed in to change notification settings - Fork 382
Expand file tree
/
Copy pathConnectionHubTest.cs
More file actions
187 lines (157 loc) · 6.53 KB
/
ConnectionHubTest.cs
File metadata and controls
187 lines (157 loc) · 6.53 KB
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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the Apache 2.0 License
// See the LICENSE file in the project root for more information.
// Maintainer: Argo Zhang(argo@live.ca) Website: https://www.blazor.zone
using Microsoft.AspNetCore.Components.Authorization;
using Microsoft.Extensions.Options;
using Microsoft.JSInterop;
using System.Security.Claims;
namespace UnitTest.Services;
public class ConnectionHubTest
{
[Fact]
public async Task Callback_Ok()
{
var context = new BunitContext();
context.JSInterop.Mode = JSRuntimeMode.Loose;
context.Services.AddBootstrapBlazor();
var authorizationContext = new MockAuthenticationStateProvider();
context.Services.AddScoped<AuthenticationStateProvider>(p => authorizationContext);
var options = context.Services.GetRequiredService<IOptions<BootstrapBlazorOptions>>();
options.Value.ConnectionHubOptions = new()
{
Enable = true,
EnableIpLocator = true,
TimeoutInterval = TimeSpan.FromMilliseconds(1000),
BeatInterval = TimeSpan.FromMilliseconds(200)
};
var client = context.Services.GetRequiredService<WebClientService>();
var service = context.Services.GetRequiredService<IConnectionService>();
var cut = context.Render<ConnectionHub>();
await cut.InvokeAsync(async () =>
{
_ = Task.Run(async () =>
{
await Task.Delay(100);
client.SetData(new ClientInfo() { Id = "test_id", Ip = "::1" });
});
await cut.Instance.Callback(new ClientInfo { Id = "test_id", Ip = "::1" });
});
Assert.Equal(1, service.Count);
// 测试 IConnectionService AddOrUpdate
service.AddOrUpdate(new ClientInfo() { Id = "test_id" });
Assert.Equal(1, service.Count);
service.AddOrUpdate(new ClientInfo() { Id = "test_id" });
Assert.Equal(1, service.Count);
// 触发 Beat 时间
await Task.Delay(200);
authorizationContext.SetAuthorized("mock_user");
await cut.InvokeAsync(async () =>
{
await cut.Instance.Callback(new ClientInfo { Id = "test_id", Ip = "::1" });
});
Assert.True(service.TryGetValue("test_id", out var item));
Assert.NotNull(item?.ClientInfo);
Assert.Equal("mock_user", item.ClientInfo.UserName);
Assert.True(item?.ConnectionTime < DateTimeOffset.Now);
cut.Dispose();
// 触发内部 ClientInfo 为空情况 覆盖 _clientInfo ??= new();
options.Value.ConnectionHubOptions.Enable = false;
client = context.Services.GetRequiredService<WebClientService>();
cut = context.Render<ConnectionHub>();
await cut.InvokeAsync(async () =>
{
client.SetData(new ClientInfo() { Id = "test_id", Ip = "::1" });
await cut.Instance.Callback(new ClientInfo { Id = "test_id", Ip = "::1" });
});
// 设置 EnableIpLocator 为 false
options.Value.ConnectionHubOptions.Enable = true;
options.Value.ConnectionHubOptions.EnableIpLocator = false;
client = context.Services.GetRequiredService<WebClientService>();
cut = context.Render<ConnectionHub>();
await cut.InvokeAsync(async () =>
{
client.SetData(new ClientInfo() { Id = "test_id", Ip = "::1" });
await cut.Instance.Callback(new ClientInfo { Id = "test_id", Ip = "::1" });
});
}
[Fact]
public async Task ExpirationScanFrequency_Ok()
{
var services = new ServiceCollection();
services.AddBootstrapBlazor();
var provider = services.BuildServiceProvider();
var options = provider.GetRequiredService<IOptions<BootstrapBlazorOptions>>();
options.Value.ConnectionHubOptions = new()
{
Enable = true,
ExpirationScanFrequency = TimeSpan.FromMicroseconds(300),
TimeoutInterval = TimeSpan.FromMilliseconds(200),
BeatInterval = TimeSpan.FromMilliseconds(100)
};
var service = provider.GetRequiredService<IConnectionService>();
service.AddOrUpdate(new ClientInfo() { Id = "test_id" });
Assert.Equal(1, service.Count);
Assert.Single(service.Connections);
await Task.Delay(500);
Assert.Equal(0, service.Count);
}
[Fact]
public async Task ExpirationScanFrequency_Cancel()
{
var type = Type.GetType("BootstrapBlazor.Components.DefaultConnectionService, BootstrapBlazor");
Assert.NotNull(type);
var service = Activator.CreateInstance(type, new BootstrapBlazorOptions()
{
ConnectionHubOptions = new()
{
Enable = true
}
});
Assert.NotNull(service);
var fieldInfo = type.GetField("_cancellationTokenSource", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
Assert.NotNull(fieldInfo);
var token = fieldInfo.GetValue(service) as CancellationTokenSource;
Assert.NotNull(token);
await Task.Delay(200);
token.Cancel();
}
[Fact]
public void ConnectionHubOptions_Ok()
{
var services = new ServiceCollection();
services.AddBootstrapBlazor();
var provider = services.BuildServiceProvider();
var service = provider.GetRequiredService<IConnectionService>();
service.AddOrUpdate(new ClientInfo() { Id = "test_id" });
Assert.Equal(1, service.Count);
}
[Fact]
public void ConnectionService_Ok()
{
var services = new ServiceCollection();
services.AddBootstrapBlazor();
var provider = services.BuildServiceProvider();
var service = provider.GetRequiredService<IConnectionService>();
service.AddOrUpdate(new ClientInfo() { Id = "test_dispose" });
var d = service as IDisposable;
d?.Dispose();
}
class MockAuthenticationStateProvider : AuthenticationStateProvider
{
ClaimsPrincipal _claim = new();
public override async Task<AuthenticationState> GetAuthenticationStateAsync()
{
await Task.Yield();
var state = new AuthenticationState(_claim);
return state;
}
public void SetAuthorized(string userName)
{
_claim = new ClaimsPrincipal(new ClaimsIdentity(new Claim[]
{
new(ClaimTypes.Name, userName),
}, "mock"));
}
}
}