|
| 1 | +using System.IO.Pipelines; |
| 2 | +using System.Net.WebSockets; |
| 3 | +using System.Text; |
| 4 | +using Xunit; |
| 5 | +using Xunit.Abstractions; |
| 6 | + |
| 7 | +namespace Devlooped |
| 8 | +{ |
| 9 | + public record SimpleWebSocketPipeTests(ITestOutputHelper Output) |
| 10 | + { |
| 11 | + [Fact] |
| 12 | + public async Task WhenWebSocketNotOpen_ThenThrowsAsync() |
| 13 | + { |
| 14 | + IWebSocketPipe pipe = WebSocketPipe.Create(new ClientWebSocket()); |
| 15 | + await Assert.ThrowsAsync<InvalidOperationException>(() => pipe.RunAsync()); |
| 16 | + } |
| 17 | + |
| 18 | + [Fact] |
| 19 | + public async Task WhenConnected_ThenRuns() |
| 20 | + { |
| 21 | + await using var server = WebSocketServer.Create(Echo, null, Output); |
| 22 | + using var socket = new ClientWebSocket(); |
| 23 | + |
| 24 | + await socket.ConnectAsync(server.Uri, default); |
| 25 | + |
| 26 | + using var pipe = WebSocketPipe.Create(socket); |
| 27 | + |
| 28 | + await Task.WhenAll( |
| 29 | + pipe.RunAsync(server.Cancellation.Token), |
| 30 | + Task.Delay(100).ContinueWith(_ => server.Cancellation.Cancel())); |
| 31 | + } |
| 32 | + |
| 33 | + [Fact] |
| 34 | + public async Task WhenServerClosesWebSocket_ThenClientCompletesGracefully() |
| 35 | + { |
| 36 | + await using var server = WebSocketServer.Create(Echo, null, Output); |
| 37 | + using var socket = new ClientWebSocket(); |
| 38 | + await socket.ConnectAsync(server.Uri, default); |
| 39 | + using var pipe = WebSocketPipe.Create(socket); |
| 40 | + var run = pipe.RunAsync(); |
| 41 | + |
| 42 | + await server.DisposeAsync(); |
| 43 | + |
| 44 | + Task.WaitAny(run, Task.Delay(100).ContinueWith(_ => throw new TimeoutException())); |
| 45 | + } |
| 46 | + |
| 47 | + [Fact] |
| 48 | + public async Task WhenClientClompletes_ThenServerCompletesGracefully() |
| 49 | + { |
| 50 | + IDuplexPipe? serverPipe = default; |
| 51 | + await using var server = WebSocketServer.Create(x => |
| 52 | + { |
| 53 | + serverPipe = x; |
| 54 | + return Task.CompletedTask; |
| 55 | + }, null, Output); |
| 56 | + |
| 57 | + using var socket = new ClientWebSocket(); |
| 58 | + await socket.ConnectAsync(server.Uri, default); |
| 59 | + using var pipe = WebSocketPipe.Create(socket, closeWhenCompleted: true); |
| 60 | + var run = pipe.RunAsync(); |
| 61 | + |
| 62 | + await pipe.CompleteAsync(); |
| 63 | + |
| 64 | + Assert.Equal(WebSocketState.Closed, socket.State); |
| 65 | + Assert.NotNull(serverPipe); |
| 66 | + await Assert.ThrowsAsync<InvalidOperationException>(() => serverPipe!.Input.ReadAsync().AsTask()); |
| 67 | + } |
| 68 | + |
| 69 | + [Fact] |
| 70 | + public async Task WhenSocketClosed_ThenCompletes() |
| 71 | + { |
| 72 | + await using var server = WebSocketServer.Create(Echo, null, Output); |
| 73 | + using var socket = new ClientWebSocket(); |
| 74 | + await socket.ConnectAsync(server.Uri, default); |
| 75 | + using var pipe = WebSocketPipe.Create(socket); |
| 76 | + var run = pipe.RunAsync(); |
| 77 | + |
| 78 | + await socket.CloseAsync(WebSocketCloseStatus.NormalClosure, null, default); |
| 79 | + |
| 80 | + Assert.Equal(WebSocketState.Closed, socket.State); |
| 81 | + await run; |
| 82 | + await Assert.ThrowsAsync<InvalidOperationException>(() => pipe.Input.ReadAsync().AsTask()); |
| 83 | + } |
| 84 | + |
| 85 | + [Fact] |
| 86 | + public async Task WhenClientClompletesWithStatus_ThenServerAndClientShareStatus() |
| 87 | + { |
| 88 | + IWebSocketPipe? serverPipe = default; |
| 89 | + await using var server = WebSocketServer.Create(x => |
| 90 | + { |
| 91 | + serverPipe = x; |
| 92 | + return Task.CompletedTask; |
| 93 | + }, null, Output); |
| 94 | + |
| 95 | + using var socket = new ClientWebSocket(); |
| 96 | + await socket.ConnectAsync(server.Uri, default); |
| 97 | + using var pipe = WebSocketPipe.Create(socket); |
| 98 | + var run = pipe.RunAsync(); |
| 99 | + |
| 100 | + await pipe.CompleteAsync(WebSocketCloseStatus.InvalidMessageType, "Invalid"); |
| 101 | + |
| 102 | + Assert.Equal(WebSocketState.Closed, socket.State); |
| 103 | + Assert.Equal(WebSocketCloseStatus.InvalidMessageType, socket.CloseStatus); |
| 104 | + Assert.Equal("Invalid", socket.CloseStatusDescription); |
| 105 | + |
| 106 | + Assert.NotNull(serverPipe); |
| 107 | + Assert.Equal(WebSocketState.Closed, serverPipe!.State); |
| 108 | + Assert.Equal(WebSocketCloseStatus.InvalidMessageType, serverPipe!.CloseStatus); |
| 109 | + Assert.Equal("Invalid", serverPipe!.CloseStatusDescription); |
| 110 | + } |
| 111 | + |
| 112 | + [Fact] |
| 113 | + public async Task WhenSubProtocolSpecified_ThenServerAndClientShareSubProtocol() |
| 114 | + { |
| 115 | + IWebSocketPipe? serverPipe = default; |
| 116 | + await using var server = WebSocketServer.Create(x => |
| 117 | + { |
| 118 | + serverPipe = x; |
| 119 | + return Task.CompletedTask; |
| 120 | + }, null, Output); |
| 121 | + |
| 122 | + using var socket = new ClientWebSocket(); |
| 123 | + socket.Options.AddSubProtocol("protobuf.webpubsub.azure.v1"); |
| 124 | + await socket.ConnectAsync(server.Uri, default); |
| 125 | + using var pipe = WebSocketPipe.Create(socket); |
| 126 | + var run = pipe.RunAsync(); |
| 127 | + |
| 128 | + Assert.Equal("protobuf.webpubsub.azure.v1", socket.SubProtocol); |
| 129 | + Assert.Equal("protobuf.webpubsub.azure.v1", pipe.SubProtocol); |
| 130 | + |
| 131 | + Assert.NotNull(serverPipe); |
| 132 | + Assert.Equal("protobuf.webpubsub.azure.v1", serverPipe!.SubProtocol); |
| 133 | + } |
| 134 | + |
| 135 | + [Fact] |
| 136 | + public async Task WhenClientClompletesWithStatus_ThenCompletesWebSocketEvenIfNotSpecified() |
| 137 | + { |
| 138 | + await using var server = WebSocketServer.Create(Echo, null, Output); |
| 139 | + using var socket = new ClientWebSocket(); |
| 140 | + await socket.ConnectAsync(server.Uri, default); |
| 141 | + using var pipe = WebSocketPipe.Create(socket, closeWhenCompleted: false); |
| 142 | + var run = pipe.RunAsync(); |
| 143 | + |
| 144 | + await pipe.CompleteAsync(WebSocketCloseStatus.InvalidMessageType, "Invalid"); |
| 145 | + |
| 146 | + Assert.Equal(WebSocketState.Closed, socket.State); |
| 147 | + Assert.Equal(WebSocketCloseStatus.InvalidMessageType, socket.CloseStatus); |
| 148 | + Assert.Equal("Invalid", socket.CloseStatusDescription); |
| 149 | + } |
| 150 | + |
| 151 | + [Fact] |
| 152 | + public async Task WhenSocketDisposed_ThenStateIsClosed() |
| 153 | + { |
| 154 | + await using var server = WebSocketServer.Create(Echo, null, Output); |
| 155 | + using var socket = new ClientWebSocket(); |
| 156 | + |
| 157 | + await socket.ConnectAsync(server.Uri, default); |
| 158 | + |
| 159 | + using var pipe = WebSocketPipe.Create(socket); |
| 160 | + |
| 161 | + socket.Dispose(); |
| 162 | + |
| 163 | + Assert.Equal(WebSocketState.Closed, pipe.State); |
| 164 | + } |
| 165 | + |
| 166 | + [Fact] |
| 167 | + public async Task WhenReceivingChunks_PipeReaderExposesFullMessage() |
| 168 | + { |
| 169 | + await using var server = WebSocketServer.Create(async s => |
| 170 | + { |
| 171 | + var serverPipe = WebSocketPipe.Create(s); |
| 172 | + var serverRun = serverPipe.RunAsync(); |
| 173 | + while (await serverPipe.Input.ReadAsync() is var result && !result.IsCompleted) |
| 174 | + { |
| 175 | + // Send in "chunks" of 1 byte. |
| 176 | + for (var i = 0; i < result.Buffer.Length; i++) |
| 177 | + await s.SendAsync(result.Buffer.Slice(i, 1).First, WebSocketMessageType.Binary, i == result.Buffer.Length - 1, default); |
| 178 | + |
| 179 | + serverPipe.Input.AdvanceTo(result.Buffer.Start, result.Buffer.End); |
| 180 | + } |
| 181 | + await serverRun; |
| 182 | + }, null, Output); |
| 183 | + |
| 184 | + using var socket = new ClientWebSocket(); |
| 185 | + await socket.ConnectAsync(server.Uri, default); |
| 186 | + using var pipe = WebSocketPipe.Create(socket); |
| 187 | + |
| 188 | + var run = pipe.RunAsync(); |
| 189 | + var write = pipe.Output.WriteAsync(new ReadOnlyMemory<byte>(Encoding.UTF8.GetBytes("hello"))); |
| 190 | + var read = await pipe.Input.ReadAsync(); |
| 191 | + var echo = Encoding.UTF8.GetString(read.Buffer.FirstSpan); |
| 192 | + |
| 193 | + Assert.Equal("hello", echo); |
| 194 | + } |
| 195 | + |
| 196 | + async Task Echo(IWebSocketPipe pipe) |
| 197 | + { |
| 198 | + while (await pipe.Input.ReadAsync() is var result && !result.IsCompleted) |
| 199 | + { |
| 200 | + await pipe.Output.WriteAsync(result.Buffer.First); |
| 201 | + pipe.Input.AdvanceTo(result.Buffer.Start, result.Buffer.End); |
| 202 | + } |
| 203 | + } |
| 204 | + } |
| 205 | +} |
0 commit comments