This repository has been archived by the owner on Jan 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5k
/
PipeResetTests.cs
82 lines (63 loc) · 2.13 KB
/
PipeResetTests.cs
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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System.Buffers;
using System.Threading.Tasks;
using Xunit;
namespace System.IO.Pipelines.Tests
{
public class PipeResetTests : IDisposable
{
public PipeResetTests()
{
_pool = new TestMemoryPool();
_pipe = new Pipe(new PipeOptions(_pool));
}
public void Dispose()
{
_pipe.Writer.Complete();
_pipe.Reader.Complete();
_pool?.Dispose();
}
private readonly TestMemoryPool _pool;
private readonly Pipe _pipe;
[Fact]
public async Task LengthIsReseted()
{
var source = new byte[] { 1, 2, 3 };
await _pipe.Writer.WriteAsync(source);
_pipe.Reader.Complete();
_pipe.Writer.Complete();
_pipe.Reset();
Assert.Equal(0, _pipe.Length);
}
[Fact]
public async Task ReadsAndWritesAfterReset()
{
var source = new byte[] { 1, 2, 3 };
await _pipe.Writer.WriteAsync(source);
ReadResult result = await _pipe.Reader.ReadAsync();
Assert.Equal(source, result.Buffer.ToArray());
_pipe.Reader.AdvanceTo(result.Buffer.End);
_pipe.Reader.Complete();
_pipe.Writer.Complete();
_pipe.Reset();
await _pipe.Writer.WriteAsync(source);
result = await _pipe.Reader.ReadAsync();
Assert.Equal(source, result.Buffer.ToArray());
_pipe.Reader.AdvanceTo(result.Buffer.End);
}
[Fact]
public void ResetThrowsIfReaderNotCompleted()
{
_pipe.Writer.Complete();
Assert.Throws<InvalidOperationException>(() => _pipe.Reset());
}
[Fact]
public void ResetThrowsIfWriterNotCompleted()
{
_pipe.Reader.Complete();
Assert.Throws<InvalidOperationException>(() => _pipe.Reset());
}
}
}