Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit 4306b6d

Browse files
committed
Merge pull request #2867 from stephentoub/pipes_readwritechaintest
Add Pipes test for chain of writers/readers
2 parents 08e9df8 + 7d23f39 commit 4306b6d

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

src/System.IO.Pipes/tests/PipeTest.Read.cs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,5 +251,43 @@ public void ValidWriteByte_ValidReadByte()
251251
Assert.Equal(123, pair.readablePipe.ReadByte());
252252
}
253253
}
254+
255+
[Theory]
256+
[InlineData(5000, 1, 1)] // very small buffers
257+
[InlineData(500, 21, 18)] // lots of iterations, with read buffer smaller than write buffer
258+
[InlineData(500, 18, 21)] // lots of iterations, with write buffer smaller than read buffer
259+
[InlineData(5, 128000, 64000)] // very large buffers
260+
public async Task AsyncReadWriteChain(int iterations, int writeBufferSize, int readBufferSize)
261+
{
262+
var writeBuffer = new byte[writeBufferSize];
263+
var readBuffer = new byte[readBufferSize];
264+
var rand = new Random();
265+
266+
using (ServerClientPair pair = CreateServerClientPair())
267+
{
268+
// Repeatedly and asynchronously write to the writeable pipe and read from the readable pipe,
269+
// verifying that the correct data made it through.
270+
for (int iter = 0; iter < iterations; iter++)
271+
{
272+
rand.NextBytes(writeBuffer);
273+
Task writerTask = pair.writeablePipe.WriteAsync(writeBuffer, 0, writeBuffer.Length);
274+
275+
int totalRead = 0;
276+
while (totalRead < writeBuffer.Length)
277+
{
278+
int numRead = await pair.readablePipe.ReadAsync(readBuffer, 0, readBuffer.Length);
279+
Assert.True(numRead > 0);
280+
Assert.Equal<byte>(
281+
new ArraySegment<byte>(writeBuffer, totalRead, numRead),
282+
new ArraySegment<byte>(readBuffer, 0, numRead));
283+
totalRead += numRead;
284+
}
285+
Assert.Equal(writeBuffer.Length, totalRead);
286+
287+
await writerTask;
288+
}
289+
}
290+
}
291+
254292
}
255293
}

0 commit comments

Comments
 (0)