-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWorkerFrameCodecTests.cs
More file actions
163 lines (137 loc) · 5.79 KB
/
Copy pathWorkerFrameCodecTests.cs
File metadata and controls
163 lines (137 loc) · 5.79 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
using System.Buffers.Binary;
using System.Diagnostics.CodeAnalysis;
using System.Text;
using DotPython.Protocol;
using Xunit;
namespace DotPython.WorkerTests;
[SuppressMessage(
"Reliability",
"CA2007:Consider calling ConfigureAwait on the awaited task",
Justification = "xUnit tests intentionally resume in the test context."
)]
public sealed class WorkerFrameCodecTests
{
[Fact]
public async Task RoundTrip_PreservesVersionedEnvelopeAcrossPartialReads()
{
var expected = WorkerProtocolSerializer.CreateEnvelope(
WorkerMessageType.CancelRequest,
Guid.NewGuid(),
DateTimeOffset.UtcNow.AddMinutes(1),
new WorkerCancelRequest(Guid.NewGuid())
);
await using var encoded = new MemoryStream();
var codec = new WorkerFrameCodec();
await codec.WriteAsync(encoded, expected, TestContext.Current.CancellationToken);
encoded.Position = 0;
await using var partial = new PartialReadStream(encoded, 1);
var actual = Assert.IsType<WorkerEnvelope>(
await codec.ReadAsync(partial, TestContext.Current.CancellationToken)
);
Assert.Equal(expected.Version, actual.Version);
Assert.Equal(expected.MessageType, actual.MessageType);
Assert.Equal(expected.CorrelationId, actual.CorrelationId);
Assert.Equal(
WorkerProtocolSerializer.ReadPayload<WorkerCancelRequest>(expected),
WorkerProtocolSerializer.ReadPayload<WorkerCancelRequest>(actual)
);
}
[Fact]
public async Task ReadAsync_ReturnsNullOnlyForCleanEndOfStream()
{
await using var stream = new MemoryStream();
var envelope = await new WorkerFrameCodec().ReadAsync(
stream,
TestContext.Current.CancellationToken
);
Assert.Null(envelope);
}
[Theory]
[InlineData(2, "length prefix")]
[InlineData(8, "declared payload")]
public async Task ReadAsync_RejectsTruncatedFrames(int availableBytes, string expectedMessage)
{
var bytes = new byte[12];
BinaryPrimitives.WriteInt32LittleEndian(bytes, 8);
await using var stream = new MemoryStream(bytes[..availableBytes]);
var exception = await Assert.ThrowsAsync<WorkerProtocolException>(async () =>
await new WorkerFrameCodec().ReadAsync(stream, TestContext.Current.CancellationToken)
);
Assert.Equal(WorkerProtocolFaultCodes.HandshakeFailed, exception.Fault.Code);
Assert.Contains(expectedMessage, exception.Message, StringComparison.Ordinal);
}
[Fact]
public async Task ReadAsync_RejectsOversizedFrameBeforeAllocatingPayload()
{
var bytes = new byte[sizeof(int)];
BinaryPrimitives.WriteInt32LittleEndian(bytes, 65);
await using var stream = new MemoryStream(bytes);
var exception = await Assert.ThrowsAsync<WorkerProtocolException>(async () =>
await new WorkerFrameCodec(64).ReadAsync(stream, TestContext.Current.CancellationToken)
);
Assert.Equal(WorkerProtocolFaultCodes.LimitExceeded, exception.Fault.Code);
}
[Fact]
public async Task ReadAsync_RejectsMalformedJsonAndUnknownMessageTypes()
{
var malformed = Encoding.UTF8.GetBytes("not-json");
await using var malformedStream = Frame(malformed);
await Assert.ThrowsAsync<WorkerProtocolException>(async () =>
await new WorkerFrameCodec().ReadAsync(
malformedStream,
TestContext.Current.CancellationToken
)
);
var unknown = Encoding.UTF8.GetBytes(
"{\"version\":{\"major\":1,\"minor\":0},\"messageType\":999,"
+ "\"correlationId\":\"b64d7456-30d9-4c16-b358-15809ea42644\","
+ "\"deadlineUtc\":null,\"payload\":{}}"
);
await using var unknownStream = Frame(unknown);
await Assert.ThrowsAsync<WorkerProtocolException>(async () =>
await new WorkerFrameCodec().ReadAsync(
unknownStream,
TestContext.Current.CancellationToken
)
);
}
private static MemoryStream Frame(byte[] payload)
{
var bytes = new byte[sizeof(int) + payload.Length];
BinaryPrimitives.WriteInt32LittleEndian(bytes, payload.Length);
payload.CopyTo(bytes, sizeof(int));
return new MemoryStream(bytes);
}
private sealed class PartialReadStream(Stream inner, int maximumRead) : Stream
{
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();
}
public override int Read(byte[] buffer, int offset, int count) =>
inner.Read(buffer, offset, Math.Min(count, maximumRead));
public override ValueTask<int> ReadAsync(
Memory<byte> buffer,
CancellationToken cancellationToken = default
) => inner.ReadAsync(buffer[..Math.Min(buffer.Length, maximumRead)], cancellationToken);
public override void Flush() => throw new NotSupportedException();
public override long Seek(long offset, SeekOrigin origin) =>
throw new NotSupportedException();
public override void SetLength(long value) => throw new NotSupportedException();
public override void Write(byte[] buffer, int offset, int count) =>
throw new NotSupportedException();
protected override void Dispose(bool disposing)
{
if (disposing)
{
inner.Dispose();
}
base.Dispose(disposing);
}
}
}