forked from dotnet/runtime
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAbortTest.cs
More file actions
151 lines (119 loc) · 5.6 KB
/
AbortTest.cs
File metadata and controls
151 lines (119 loc) · 5.6 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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Collections.Generic;
using System.Net.Http;
using System.Net.Test.Common;
using System.Threading;
using System.Threading.Tasks;
using Xunit;
using Xunit.Abstractions;
namespace System.Net.WebSockets.Client.Tests
{
public sealed class InvokerAbortTest : AbortTest
{
public InvokerAbortTest(ITestOutputHelper output) : base(output) { }
protected override bool UseCustomInvoker => true;
}
public sealed class HttpClientAbortTest : AbortTest
{
public HttpClientAbortTest(ITestOutputHelper output) : base(output) { }
protected override bool UseHttpClient => true;
}
public class AbortTest : ClientWebSocketTestBase
{
public AbortTest(ITestOutputHelper output) : base(output) { }
[OuterLoop("Uses external servers", typeof(PlatformDetection), nameof(PlatformDetection.LocalEchoServerIsNotAvailable))]
[ConditionalTheory(nameof(WebSocketsSupported)), MemberData(nameof(EchoServers))]
public async Task Abort_ConnectAndAbort_ThrowsWebSocketExceptionWithmessage(Uri server)
{
using (var cws = new ClientWebSocket())
{
var cts = new CancellationTokenSource(TimeOutMilliseconds);
var ub = new UriBuilder(server);
ub.Query = "delay10sec";
Task t = ConnectAsync(cws, ub.Uri, cts.Token);
cws.Abort();
WebSocketException ex = await Assert.ThrowsAsync<WebSocketException>(() => t);
Assert.Equal(ResourceHelper.GetExceptionMessage("net_webstatus_ConnectFailure"), ex.Message);
if (PlatformDetection.IsNetCore) // bug fix in netcoreapp: https://github.com/dotnet/corefx/pull/35960
{
Assert.Equal(WebSocketError.Faulted, ex.WebSocketErrorCode);
}
Assert.Equal(WebSocketState.Closed, cws.State);
}
}
[OuterLoop("Uses external servers", typeof(PlatformDetection), nameof(PlatformDetection.LocalEchoServerIsNotAvailable))]
[ConditionalTheory(nameof(WebSocketsSupported)), MemberData(nameof(EchoServers))]
public async Task Abort_SendAndAbort_Success(Uri server)
{
await TestCancellation(async (cws) =>
{
var cts = new CancellationTokenSource(TimeOutMilliseconds);
Task t = cws.SendAsync(
WebSocketData.GetBufferFromText(".delay5sec"),
WebSocketMessageType.Text,
true,
cts.Token);
cws.Abort();
await t;
}, server);
}
[OuterLoop("Uses external servers", typeof(PlatformDetection), nameof(PlatformDetection.LocalEchoServerIsNotAvailable))]
[ConditionalTheory(nameof(WebSocketsSupported)), MemberData(nameof(EchoServers))]
public async Task Abort_ReceiveAndAbort_Success(Uri server)
{
await TestCancellation(async (cws) =>
{
var ctsDefault = new CancellationTokenSource(TimeOutMilliseconds);
await cws.SendAsync(
WebSocketData.GetBufferFromText(".delay5sec"),
WebSocketMessageType.Text,
true,
ctsDefault.Token);
var recvBuffer = new byte[100];
var segment = new ArraySegment<byte>(recvBuffer);
Task t = cws.ReceiveAsync(segment, ctsDefault.Token);
cws.Abort();
await t;
}, server);
}
[OuterLoop("Uses external servers", typeof(PlatformDetection), nameof(PlatformDetection.LocalEchoServerIsNotAvailable))]
[ConditionalTheory(nameof(WebSocketsSupported)), MemberData(nameof(EchoServers))]
public async Task Abort_CloseAndAbort_Success(Uri server)
{
await TestCancellation(async (cws) =>
{
var ctsDefault = new CancellationTokenSource(TimeOutMilliseconds);
await cws.SendAsync(
WebSocketData.GetBufferFromText(".delay5sec"),
WebSocketMessageType.Text,
true,
ctsDefault.Token);
var recvBuffer = new byte[100];
var segment = new ArraySegment<byte>(recvBuffer);
Task t = cws.CloseAsync(WebSocketCloseStatus.NormalClosure, "AbortClose", ctsDefault.Token);
cws.Abort();
await t;
}, server);
}
[OuterLoop("Uses external servers", typeof(PlatformDetection), nameof(PlatformDetection.LocalEchoServerIsNotAvailable))]
[ConditionalTheory(nameof(WebSocketsSupported)), MemberData(nameof(EchoServers))]
public async Task ClientWebSocket_Abort_CloseOutputAsync(Uri server)
{
await TestCancellation(async (cws) =>
{
var ctsDefault = new CancellationTokenSource(TimeOutMilliseconds);
await cws.SendAsync(
WebSocketData.GetBufferFromText(".delay5sec"),
WebSocketMessageType.Text,
true,
ctsDefault.Token);
var recvBuffer = new byte[100];
var segment = new ArraySegment<byte>(recvBuffer);
Task t = cws.CloseOutputAsync(WebSocketCloseStatus.NormalClosure, "AbortShutdown", ctsDefault.Token);
cws.Abort();
await t;
}, server);
}
}
}