internal static async Task <ServerResponse> Send(string pipeName, ServerRequest request) { using (var client = await Client.ConnectAsync(pipeName, timeout: null, cancellationToken: default).ConfigureAwait(false)) { await request.WriteAsync(client.Stream).ConfigureAwait(false); return(await ServerResponse.ReadAsync(client.Stream).ConfigureAwait(false)); } }
public async Task ServerResponse_WriteRead_RoundtripsProperly() { // Arrange var response = new CompletedServerResponse(42, utf8output: false, output: "a string", error: "an error"); var memoryStream = new MemoryStream(); // Act await response.WriteAsync(memoryStream, CancellationToken.None); // Assert Assert.True(memoryStream.Position > 0); memoryStream.Position = 0; var result = (CompletedServerResponse)await ServerResponse.ReadAsync(memoryStream, CancellationToken.None); result.ReturnCode.Should().Be(42); result.Utf8Output.Should().Be(false); result.Output.Should().Be("a string"); result.ErrorOutput.Should().Be("an error"); }
public async Task ShutdownResponse_WriteRead_RoundtripsProperly() { // Arrange & Act 1 var memoryStream = new MemoryStream(); var response = new ShutdownServerResponse(42); // Assert 1 Assert.Equal(ServerResponse.ResponseType.Shutdown, response.Type); // Act 2 await response.WriteAsync(memoryStream, CancellationToken.None); // Assert 2 memoryStream.Position = 0; var read = await ServerResponse.ReadAsync(memoryStream, CancellationToken.None); read.Type.Should().Be(ServerResponse.ResponseType.Shutdown); var typed = (ShutdownServerResponse)read; typed.ServerProcessId.Should().Be(42); }
public async Task AcceptConnection_ShutdownRequest_ReturnsShutdownResponse() { // Arrange var stream = new TestableStream(); await ServerRequest.CreateShutdown().WriteAsync(stream.ReadStream, CancellationToken.None); stream.ReadStream.Position = 0; var connection = CreateConnection(stream); var connectionHost = CreateConnectionHost(); var compilerHost = CreateCompilerHost(); var dispatcher = new DefaultRequestDispatcher(connectionHost, compilerHost, CancellationToken.None); // Act var connectionResult = await dispatcher.AcceptConnection( Task.FromResult <Connection>(connection), accept : true, cancellationToken : CancellationToken.None); // Assert Assert.Equal(ConnectionResult.Reason.ClientShutdownRequest, connectionResult.CloseReason); stream.WriteStream.Position = 0; var response = await ServerResponse.ReadAsync(stream.WriteStream).ConfigureAwait(false); Assert.Equal(ServerResponse.ResponseType.Shutdown, response.Type); }
public async Task AcceptConnection_AcceptFalse_RejectsBuildRequest() { // Arrange var stream = new TestableStream(); await EmptyServerRequest.WriteAsync(stream.ReadStream, CancellationToken.None); stream.ReadStream.Position = 0; var connection = CreateConnection(stream); var connectionHost = CreateConnectionHost(); var compilerHost = CreateCompilerHost(); var dispatcher = new DefaultRequestDispatcher(connectionHost, compilerHost, CancellationToken.None); // Act var connectionResult = await dispatcher.AcceptConnection( Task.FromResult <Connection>(connection), accept : false, cancellationToken : CancellationToken.None); // Assert Assert.Equal(ConnectionResult.Reason.CompilationNotStarted, connectionResult.CloseReason); stream.WriteStream.Position = 0; var response = await ServerResponse.ReadAsync(stream.WriteStream).ConfigureAwait(false); Assert.Equal(ServerResponse.ResponseType.Rejected, response.Type); }
public async Task ServerRunning_CancelCompilation_CancelsSuccessfully() { // Arrange const int requestCount = 5; var count = 0; var completionSource = new TaskCompletionSource <bool>(); var host = CreateCompilerHost(c => c.ExecuteFunc = (req, ct) => { if (Interlocked.Increment(ref count) == requestCount) { completionSource.SetResult(true); } ct.WaitHandle.WaitOne(); return(new RejectedServerResponse()); }); var semaphore = new SemaphoreSlim(1); Action <object, EventArgs> onListening = (s, e) => { semaphore.Release(); }; using (var serverData = ServerUtilities.CreateServer(compilerHost: host, onListening: onListening)) { // Send all the requests. var clients = new List <Client>(); for (var i = 0; i < requestCount; i++) { // Wait for the server to start listening. await semaphore.WaitAsync(TimeSpan.FromMinutes(1)); var client = await Client.ConnectAsync(serverData.PipeName, timeout : null, cancellationToken : default); await EmptyServerRequest.WriteAsync(client.Stream); clients.Add(client); } // Act // Wait until all of the connections are being processed by the server. await completionSource.Task; // Now cancel var stats = await serverData.CancelAndCompleteAsync(); // Assert Assert.Equal(requestCount, stats.Connections); Assert.Equal(requestCount, count); // Read the server response to each client. foreach (var client in clients) { var task = ServerResponse.ReadAsync(client.Stream); // We expect this to throw because the stream is already closed. await Assert.ThrowsAnyAsync <IOException>(() => task); client.Dispose(); } } }