-
Notifications
You must be signed in to change notification settings - Fork 5.4k
Expand file tree
/
Copy pathPipeReaderCopyToAsyncTests.cs
More file actions
326 lines (259 loc) · 11.9 KB
/
PipeReaderCopyToAsyncTests.cs
File metadata and controls
326 lines (259 loc) · 11.9 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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System;
using System.Buffers;
using System.Collections.Generic;
using System.IO.Pipelines.Tests.Infrastructure;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Xunit;
namespace System.IO.Pipelines.Tests
{
public class CopyToAsyncTests
{
private static readonly PipeOptions s_testOptions = new PipeOptions(readerScheduler: PipeScheduler.Inline, useSynchronizationContext: false);
protected Pipe Pipe { get; set; }
protected virtual PipeReader PipeReader => Pipe.Reader;
public CopyToAsyncTests()
{
Pipe = new Pipe(s_testOptions);
}
[Fact]
public async Task CopyToAsyncThrowsArgumentNullExceptionForNullDestination()
{
await AssertExtensions.ThrowsAsync<ArgumentNullException>("destination", () => PipeReader.CopyToAsync((Stream)null));
await AssertExtensions.ThrowsAsync<ArgumentNullException>("destination", () => PipeReader.CopyToAsync((PipeWriter)null));
}
[Fact]
public async Task CopyToAsyncThrowsTaskCanceledExceptionForAlreadyCancelledToken()
{
await Assert.ThrowsAsync<TaskCanceledException>(() => PipeReader.CopyToAsync(new MemoryStream(), new CancellationToken(true)));
}
[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsThreadingSupported))]
public async Task CopyToAsyncStreamWorks()
{
var messages = new List<byte[]>()
{
Encoding.UTF8.GetBytes("Hello World1"),
Encoding.UTF8.GetBytes("Hello World2"),
Encoding.UTF8.GetBytes("Hello World3"),
};
var stream = new WriteCheckMemoryStream();
Task task = PipeReader.CopyToAsync(stream);
foreach (var msg in messages)
{
await Pipe.Writer.WriteAsync(msg);
await stream.WaitForBytesWrittenAsync(msg.Length);
}
Pipe.Writer.Complete();
await task;
Assert.Equal(messages.SelectMany(msg => msg).ToArray(), stream.ToArray());
}
[Fact]
public async Task CopyToAsyncPipeWriterWorks()
{
var messages = new List<byte[]>()
{
Encoding.UTF8.GetBytes("Hello World1"),
Encoding.UTF8.GetBytes("Hello World2"),
Encoding.UTF8.GetBytes("Hello World3"),
};
var targetPipe = new Pipe(s_testOptions);
Task task = PipeReader.CopyToAsync(targetPipe.Writer);
foreach (var msg in messages)
{
await Pipe.Writer.WriteAsync(msg);
}
Pipe.Writer.Complete();
await task;
ReadResult readResult = await targetPipe.Reader.ReadAsync();
Assert.Equal(messages.SelectMany(msg => msg).ToArray(), readResult.Buffer.ToArray());
targetPipe.Reader.AdvanceTo(readResult.Buffer.End);
targetPipe.Reader.Complete();
targetPipe.Writer.Complete();
}
[Fact]
public async Task MultiSegmentWritesWorks()
{
using (var pool = new TestMemoryPool())
{
Pipe = new Pipe(new PipeOptions(pool: pool, readerScheduler: PipeScheduler.Inline));
Pipe.Writer.WriteEmpty(4096);
Pipe.Writer.WriteEmpty(4096);
Pipe.Writer.WriteEmpty(4096);
await Pipe.Writer.FlushAsync();
Pipe.Writer.Complete();
var stream = new MemoryStream();
await PipeReader.CopyToAsync(stream);
PipeReader.Complete();
Assert.Equal(4096 * 3, stream.Length);
}
}
[Fact]
public async Task MultiSegmentWritesUntilFailure()
{
using (var pool = new DisposeTrackingBufferPool())
{
Pipe = new Pipe(new PipeOptions(pool, readerScheduler: PipeScheduler.Inline, useSynchronizationContext: false));
Pipe.Writer.WriteEmpty(4096);
Pipe.Writer.WriteEmpty(4096);
Pipe.Writer.WriteEmpty(4096);
await Pipe.Writer.FlushAsync();
Pipe.Writer.Complete();
Assert.Equal(3, pool.CurrentlyRentedBlocks);
var stream = new ThrowAfterNWritesStream(2);
try
{
await PipeReader.CopyToAsync(stream);
Assert.True(false, $"CopyToAsync should have failed, wrote {stream.Writes} times.");
}
catch (InvalidOperationException)
{
}
Assert.Equal(2, stream.Writes);
Assert.Equal(1, pool.CurrentlyRentedBlocks);
Assert.Equal(2, pool.DisposedBlocks);
ReadResult result = await PipeReader.ReadAsync();
Assert.Equal(4096, result.Buffer.Length);
PipeReader.Complete();
Assert.Equal(0, pool.CurrentlyRentedBlocks);
Assert.Equal(3, pool.DisposedBlocks);
}
}
[Fact]
public async Task EmptyBufferNotWrittenToStream()
{
Pipe.Writer.Complete();
var stream = new ThrowingStream();
await PipeReader.CopyToAsync(stream);
PipeReader.Complete();
}
[Fact]
public async Task CancelingThePendingReadThrowsOperationCancelledException()
{
var stream = new MemoryStream();
Task task = PipeReader.CopyToAsync(stream);
PipeReader.CancelPendingRead();
await Assert.ThrowsAsync<OperationCanceledException>(() => task);
}
[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsThreadingSupported))]
public async Task CancelingBetweenReadsThrowsOperationCancelledException()
{
var stream = new WriteCheckMemoryStream { MidWriteCancellation = new CancellationTokenSource() };
Task task = PipeReader.CopyToAsync(stream, stream.MidWriteCancellation.Token);
Pipe.Writer.WriteEmpty(10);
await Pipe.Writer.FlushAsync();
await Assert.ThrowsAsync<TaskCanceledException>(() => task);
}
[Fact]
public async Task CancelingViaCancellationTokenThrowsOperationCancelledException()
{
var stream = new MemoryStream();
var cts = new CancellationTokenSource();
Task task = PipeReader.CopyToAsync(stream, cts.Token);
cts.Cancel();
await Assert.ThrowsAsync<OperationCanceledException>(() => task);
}
[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsThreadingSupported))]
public async Task CancelingPipeWriterViaCancellationTokenThrowsOperationCancelledException()
{
// This should make the write call pause
var targetPipe = new Pipe(new PipeOptions(pauseWriterThreshold: 1, resumeWriterThreshold: 1));
var cts = new CancellationTokenSource();
await Pipe.Writer.WriteAsync(Encoding.ASCII.GetBytes("Gello World"));
Task task = PipeReader.CopyToAsync(targetPipe.Writer, cts.Token);
cts.Cancel();
await Assert.ThrowsAsync<OperationCanceledException>(() => task);
}
[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsThreadingSupported))]
public async Task CancelingPipeWriterViaPendingFlushThrowsOperationCancelledException()
{
// This should make the write call pause
var targetPipe = new Pipe(new PipeOptions(pauseWriterThreshold: 1, resumeWriterThreshold: 1));
await Pipe.Writer.WriteAsync(Encoding.ASCII.GetBytes("Gello World"));
Task task = PipeReader.CopyToAsync(targetPipe.Writer);
targetPipe.Writer.CancelPendingFlush();
await Assert.ThrowsAsync<OperationCanceledException>(() => task);
}
[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsThreadingSupported))]
public async Task CancelingStreamViaCancellationTokenThrowsOperationCancelledException()
{
var stream = new CancelledWritesStream();
var cts = new CancellationTokenSource();
Task task = PipeReader.CopyToAsync(stream, cts.Token);
// Call write async inline, this will yield when it hits the tcs
Pipe.Writer.WriteEmpty(10);
await Pipe.Writer.FlushAsync();
// Then cancel
cts.Cancel();
// Now resume the write which should result in an exception
stream.WaitForWriteTask.TrySetResult(null);
await Assert.ThrowsAsync<OperationCanceledException>(() => task);
}
[Fact]
public async Task ThrowingFromStreamDoesNotLeavePipeReaderInBrokenState()
{
var stream = new ThrowingStream();
Task task = PipeReader.CopyToAsync(stream);
Pipe.Writer.WriteEmpty(10);
await Pipe.Writer.FlushAsync();
await Assert.ThrowsAsync<InvalidOperationException>(() => task);
Pipe.Writer.WriteEmpty(10);
await Pipe.Writer.FlushAsync();
Pipe.Writer.Complete();
var stream2 = new MemoryStream();
await PipeReader.CopyToAsync(stream2);
Assert.Equal(20, stream2.Length);
PipeReader.Complete();
}
[ConditionalTheory(typeof(PlatformDetection), nameof(PlatformDetection.IsThreadingSupported))]
[InlineData(0)]
[InlineData(1)]
public async Task ThrowingFromStreamCallsAdvanceToWithStartOfLastReadResult(int throwAfterNWrites)
{
var wrappedPipeReader = new TestPipeReader(PipeReader);
var stream = new ThrowAfterNWritesStream(throwAfterNWrites);
Task task = wrappedPipeReader.CopyToAsync(stream);
Pipe.Writer.WriteEmpty(10);
await Pipe.Writer.FlushAsync();
// Write twice for the test case where the stream throws on the second write.
Pipe.Writer.WriteEmpty(10);
await Pipe.Writer.FlushAsync();
await Assert.ThrowsAsync<InvalidOperationException>(() => task);
SequencePosition startPosition = wrappedPipeReader.LastReadResult.Buffer.Start;
Assert.NotNull(startPosition.GetObject());
Assert.True(startPosition.Equals(wrappedPipeReader.LastConsumed));
Assert.True(startPosition.Equals(wrappedPipeReader.LastExamined));
}
[Fact]
public async Task CopyToAsyncStreamCopiesRemainderAfterReadingSome()
{
var buffer = Encoding.UTF8.GetBytes("Hello World");
await Pipe.Writer.WriteAsync(buffer);
Pipe.Writer.Complete();
var result = await PipeReader.ReadAsync();
Assert.Equal(result.Buffer.ToArray(), buffer);
// Consume Hello
PipeReader.AdvanceTo(result.Buffer.GetPosition(5));
var ms = new MemoryStream();
await PipeReader.CopyToAsync(ms);
Assert.Equal(buffer.AsMemory(5).ToArray(), ms.ToArray());
}
[Fact]
public async Task CopyToAsyncPipeWriterCopiesRemainderAfterReadingSome()
{
var buffer = Encoding.UTF8.GetBytes("Hello World");
await Pipe.Writer.WriteAsync(buffer);
Pipe.Writer.Complete();
var result = await PipeReader.ReadAsync();
Assert.Equal(result.Buffer.ToArray(), buffer);
// Consume Hello
PipeReader.AdvanceTo(result.Buffer.GetPosition(5));
var ms = new MemoryStream();
await PipeReader.CopyToAsync(PipeWriter.Create(ms));
Assert.Equal(buffer.AsMemory(5).ToArray(), ms.ToArray());
}
}
}