forked from npgsql/npgsql
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathReadBufferTests.cs
More file actions
171 lines (145 loc) · 5.64 KB
/
ReadBufferTests.cs
File metadata and controls
171 lines (145 loc) · 5.64 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
using Npgsql.Internal;
using NUnit.Framework;
using System;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
namespace Npgsql.Tests;
[FixtureLifeCycle(LifeCycle.InstancePerTestCase)] // Parallel access to a single buffer
class ReadBufferTests
{
[Test]
public void Skip()
{
for (byte i = 0; i < 50; i++)
Writer.WriteByte(i);
ReadBuffer.Ensure(10);
ReadBuffer.Skip(7);
Assert.That(ReadBuffer.ReadByte(), Is.EqualTo(7));
ReadBuffer.Skip(10);
ReadBuffer.Ensure(1);
Assert.That(ReadBuffer.ReadByte(), Is.EqualTo(18));
ReadBuffer.Skip(20);
ReadBuffer.Ensure(1);
Assert.That(ReadBuffer.ReadByte(), Is.EqualTo(39));
}
[Test]
public void ReadSingle()
{
const float expected = 8.7f;
var bytes = BitConverter.GetBytes(expected);
Array.Reverse(bytes);
Writer.Write(bytes);
ReadBuffer.Ensure(4);
Assert.That(ReadBuffer.ReadSingle(), Is.EqualTo(expected));
}
[Test]
public void ReadDouble()
{
const double expected = 8.7;
var bytes = BitConverter.GetBytes(expected);
Array.Reverse(bytes);
Writer.Write(bytes);
ReadBuffer.Ensure(8);
Assert.That(ReadBuffer.ReadDouble(), Is.EqualTo(expected));
}
[Test]
public void ReadNullTerminatedString_buffered_only()
{
Writer
.Write(NpgsqlWriteBuffer.UTF8Encoding.GetBytes(new string("foo")))
.WriteByte(0)
.Write(NpgsqlWriteBuffer.UTF8Encoding.GetBytes(new string("bar")))
.WriteByte(0);
ReadBuffer.Ensure(1);
Assert.That(ReadBuffer.ReadNullTerminatedString(), Is.EqualTo("foo"));
Assert.That(ReadBuffer.ReadNullTerminatedString(), Is.EqualTo("bar"));
}
[Test]
public async Task ReadNullTerminatedString_with_io()
{
Writer.Write(NpgsqlWriteBuffer.UTF8Encoding.GetBytes(new string("Chunked ")));
await ReadBuffer.Ensure(1, async: true);
var task = ReadBuffer.ReadNullTerminatedString(async: true);
Assert.That(!task.IsCompleted);
Writer
.Write(NpgsqlWriteBuffer.UTF8Encoding.GetBytes(new string("string")))
.WriteByte(0)
.Write(NpgsqlWriteBuffer.UTF8Encoding.GetBytes(new string("bar")))
.WriteByte(0);
Assert.That(await task, Is.EqualTo("Chunked string"));
Assert.That(ReadBuffer.ReadNullTerminatedString(), Is.EqualTo("bar"));
}
[SetUp]
public void SetUp()
{
var stream = new MockStream();
ReadBuffer = new NpgsqlReadBuffer(null, stream, null, NpgsqlReadBuffer.DefaultSize, NpgsqlWriteBuffer.UTF8Encoding, NpgsqlWriteBuffer.RelaxedUTF8Encoding);
Writer = stream.Writer;
}
// ReSharper disable once InconsistentNaming
NpgsqlReadBuffer ReadBuffer = default!;
// ReSharper disable once InconsistentNaming
MockStream.MockStreamWriter Writer = default!;
class MockStream : Stream
{
const int Size = 8192;
internal MockStreamWriter Writer { get; }
public MockStream() => Writer = new MockStreamWriter(this);
TaskCompletionSource<object> _tcs = new();
readonly byte[] _data = new byte[Size];
int _filled;
public override int Read(byte[] buffer, int offset, int count)
=> Read(buffer, offset, count, async: false).GetAwaiter().GetResult();
public override Task<int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
=> Read(buffer, offset, count, async: true);
async Task<int> Read(byte[] buffer, int offset, int count, bool async)
{
if (_filled == 0)
{
_tcs = new TaskCompletionSource<object>();
if (async)
await _tcs.Task;
else
_tcs.Task.Wait();
}
count = Math.Min(count, _filled);
new Span<byte>(_data, 0, count).CopyTo(new Span<byte>(buffer, offset, count));
new Span<byte>(_data, count, _filled - count).CopyTo(_data);
_filled -= count;
return count;
}
internal class MockStreamWriter(MockStream stream)
{
public MockStreamWriter WriteByte(byte b)
{
Span<byte> bytes = stackalloc byte[1];
bytes[0] = b;
Write(bytes);
return this;
}
public MockStreamWriter Write(ReadOnlySpan<byte> bytes)
{
if (stream._filled + bytes.Length > Size)
throw new Exception("Mock stream overrun");
bytes.CopyTo(new Span<byte>(stream._data, stream._filled, bytes.Length));
stream._filled += bytes.Length;
stream._tcs.TrySetResult(new());
return this;
}
}
public override void SetLength(long value) => throw new NotSupportedException();
public override void Write(byte[] buffer, int offset, int count) => throw new NotSupportedException();
public override long Seek(long offset, SeekOrigin origin) => throw new NotSupportedException();
public override void Flush() => throw new NotSupportedException();
public override bool CanRead => true;
public override bool CanSeek => false;
public override bool CanWrite => false;
public override long Length => throw new NotSupportedException();
public override long Position
{
get => throw new NotSupportedException();
set => throw new NotSupportedException();
}
}
}