Skip to content

Commit

Permalink
Merge pull request #351 from AArnott/betterTests
Browse files Browse the repository at this point in the history
Improve tests to timeout quickly instead of hang when product defects exist
  • Loading branch information
AArnott authored Nov 2, 2019
2 parents 06d287b + 4075ba3 commit af23d0e
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/StreamJsonRpc.Tests/JsonRpcWithFatalExceptionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ public async Task CancelExceptionPreferredOverConnectionLost()
[Fact]
public async Task AggregateExceptionIsNotRemovedFromAsyncMethod()
{
var remoteException = await Assert.ThrowsAnyAsync<Exception>(() => this.clientRpc.InvokeAsync(nameof(Server.AsyncMethodThrowsAggregateExceptionWithTwoInner)));
var remoteException = await Assert.ThrowsAnyAsync<Exception>(() => this.clientRpc.InvokeWithCancellationAsync(nameof(Server.AsyncMethodThrowsAggregateExceptionWithTwoInner), cancellationToken: this.TimeoutToken));

// The async server method itself strips the second of the InnerExceptions, so we can't recover it here.
// Since we only get one, we expect the inner exception (of the AggregateException)
Expand Down
9 changes: 5 additions & 4 deletions src/StreamJsonRpc.Tests/MessageHeaderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Microsoft.VisualStudio.Threading;
using StreamJsonRpc;
using Xunit;
using Xunit.Abstractions;
Expand Down Expand Up @@ -111,15 +112,15 @@ public async Task SendMessageWithEncoding(string encodingName)
var messageHandler = new HeaderDelimitedMessageHandler(this.clientStream, this.clientStream);
var rpcClient = new JsonRpc(messageHandler);
messageHandler.Encoding = Encoding.GetEncoding(encodingName);
await rpcClient.NotifyAsync("Foo");
await rpcClient.NotifyAsync("Foo").WithCancellation(this.TimeoutToken);
rpcClient.Dispose();

MemoryStream seekableServerStream = await this.GetSeekableServerStream();
int bytesRead = 0;
var reader = new StreamReader(seekableServerStream, Encoding.ASCII);
var headerLines = new List<string>();
string line;
while ((line = reader.ReadLine()) != string.Empty)
while ((line = await reader.ReadLineAsync().WithCancellation(this.TimeoutToken)) != string.Empty)
{
headerLines.Add(line);
bytesRead += line.Length + 2; // + CRLF
Expand All @@ -138,14 +139,14 @@ public async Task SendMessageWithEncoding(string encodingName)
// we need to reposition the stream at the start of the content to create a new StreamReader.
seekableServerStream.Position = bytesRead;
reader = new StreamReader(seekableServerStream, Encoding.GetEncoding(encodingName));
string json = reader.ReadToEnd();
string json = await reader.ReadToEndAsync().WithCancellation(this.TimeoutToken);
Assert.Equal('{', json[0]);
}

private async Task<MemoryStream> GetSeekableServerStream()
{
var seekableServerStream = new MemoryStream();
await this.serverStream.CopyToAsync(seekableServerStream);
await this.serverStream.CopyToAsync(seekableServerStream, 4096, this.TimeoutToken);
seekableServerStream.Position = 0;
return seekableServerStream;
}
Expand Down

0 comments on commit af23d0e

Please sign in to comment.