|
7 | 7 | using Xunit; |
8 | 8 | using Xunit.Abstractions; |
9 | 9 | using Xunit.Sdk; |
| 10 | +using System.Linq; |
10 | 11 |
|
11 | 12 | namespace System.Net.Sockets.Tests |
12 | 13 | { |
@@ -218,6 +219,58 @@ public ConnectTask(ITestOutputHelper output) : base(output) {} |
218 | 219 | public sealed class ConnectEap : Connect<SocketHelperEap> |
219 | 220 | { |
220 | 221 | public ConnectEap(ITestOutputHelper output) : base(output) {} |
| 222 | + |
| 223 | + [Theory] |
| 224 | + [PlatformSpecific(TestPlatforms.Windows)] |
| 225 | + [InlineData(true)] |
| 226 | + [InlineData(false)] |
| 227 | + public async Task ConnectAsync_WithData_DataReceived(bool useArrayApi) |
| 228 | + { |
| 229 | + using Socket listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); |
| 230 | + listener.Bind(new IPEndPoint(IPAddress.Loopback, 0)); |
| 231 | + IPEndPoint serverEp = (IPEndPoint)listener.LocalEndPoint!; |
| 232 | + listener.Listen(); |
| 233 | + |
| 234 | + var serverTask = Task.Run(async () => |
| 235 | + { |
| 236 | + using Socket handler = await listener.AcceptAsync(); |
| 237 | + using var cts = new CancellationTokenSource(TestSettings.PassingTestTimeout); |
| 238 | + byte[] recvBuffer = new byte[6]; |
| 239 | + int received = await handler.ReceiveAsync(recvBuffer, SocketFlags.None, cts.Token); |
| 240 | + Assert.True(received == 4); |
| 241 | + |
| 242 | + recvBuffer.AsSpan(0, 4).SequenceEqual(new byte[] { 2, 3, 4, 5 }); |
| 243 | + }); |
| 244 | + |
| 245 | + using var client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); |
| 246 | + |
| 247 | + byte[] buffer = new byte[] { 0, 1, 2, 3, 4, 5, 6, 7 }; |
| 248 | + |
| 249 | + var mre = new ManualResetEventSlim(false); |
| 250 | + var saea = new SocketAsyncEventArgs(); |
| 251 | + saea.RemoteEndPoint = serverEp; |
| 252 | + |
| 253 | + // Slice the buffer to test the offset management: |
| 254 | + if (useArrayApi) |
| 255 | + { |
| 256 | + saea.SetBuffer(buffer, 2, 4); |
| 257 | + } |
| 258 | + else |
| 259 | + { |
| 260 | + saea.SetBuffer(buffer.AsMemory(2, 4)); |
| 261 | + } |
| 262 | + |
| 263 | + saea.Completed += (_, __) => mre.Set(); |
| 264 | + |
| 265 | + if (client.ConnectAsync(saea)) |
| 266 | + { |
| 267 | + Assert.True(mre.Wait(TestSettings.PassingTestTimeout), "Timed out while waiting for connection"); |
| 268 | + } |
| 269 | + |
| 270 | + Assert.Equal(SocketError.Success, saea.SocketError); |
| 271 | + |
| 272 | + await serverTask; |
| 273 | + } |
221 | 274 | } |
222 | 275 |
|
223 | 276 | public sealed class ConnectCancellableTask : Connect<SocketHelperCancellableTask> |
|
0 commit comments