Skip to content

Commit

Permalink
Merge pull request #361 from AArnott/lut
Browse files Browse the repository at this point in the history
Get Live Unit Testing running more smoothly
  • Loading branch information
AArnott committed Nov 6, 2019
2 parents 2de64c4 + 28ebc62 commit 4e21f97
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/StreamJsonRpc.Tests/JsonRpcRemoteTargetTests.cs
Expand Up @@ -142,8 +142,14 @@ public async Task CanInvokeOnOriginServerFromAdditionalRemoteTarget()
public async Task CanCancelOnRemoteTarget()
{
var tokenSource = new CancellationTokenSource();
RemoteTargetOne.CancellableRemoteOperationEntered.Reset();
var task = this.originRpc.InvokeWithCancellationAsync<bool>(nameof(RemoteTargetOne.CancellableRemoteOperation), cancellationToken: tokenSource.Token);

// Don't cancel the token until we've entered the method, since the point is the RPC method receives the notice,
// not that we cancel before it's even transmitted.
await RemoteTargetOne.CancellableRemoteOperationEntered.WaitAsync(this.TimeoutToken);
tokenSource.Cancel();

var result = await task;
Assert.True(result);
}
Expand Down Expand Up @@ -278,6 +284,8 @@ public Task<T> InvokeAsync<T>(RequestId requestId, string targetName, object? ar

public class RemoteTargetOne
{
internal static readonly AsyncManualResetEvent CancellableRemoteOperationEntered = new AsyncManualResetEvent();

private static TaskCompletionSource<int> notificationTcs = new TaskCompletionSource<int>();

public static Task<int> NotificationReceived => notificationTcs.Task;
Expand Down Expand Up @@ -305,6 +313,7 @@ public static async Task<int> AddOneLongRunningAsync(int value)

public static async Task<bool> CancellableRemoteOperation(CancellationToken token)
{
CancellableRemoteOperationEntered.Set();
var retryIndex = 0;
while (retryIndex < 100)
{
Expand Down
1 change: 1 addition & 0 deletions src/StreamJsonRpc.Tests/JsonRpcTests.cs
Expand Up @@ -292,6 +292,7 @@ public async Task ThrowsIfTargetNotSet()
public async Task InvokeWithProgressParameter_NoMemoryLeakConfirm()
{
Skip.If(this.clientMessageFormatter is MessagePackFormatter, "IProgress<T> serialization is not supported for MessagePack");
Skip.If(IsRunningUnderLiveUnitTest);
WeakReference weakRef = await this.InvokeWithProgressParameter_NoMemoryLeakConfirm_Helper();
GC.Collect();
Assert.False(weakRef.IsAlive);
Expand Down
3 changes: 3 additions & 0 deletions src/StreamJsonRpc.Tests/TestBase.cs
Expand Up @@ -3,6 +3,7 @@

using System;
using System.Diagnostics;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.VisualStudio.Threading;
Expand All @@ -17,6 +18,8 @@ public abstract class TestBase : IDisposable

protected static readonly CancellationToken PrecanceledToken = new CancellationToken(canceled: true);

protected static readonly bool IsRunningUnderLiveUnitTest = AppDomain.CurrentDomain.GetAssemblies().Any(a => a.GetName().Name == "Microsoft.CodeAnalysis.LiveUnitTesting.Runtime");

private const int GCAllocationAttempts = 10;

private readonly CancellationTokenSource timeoutTokenSource;
Expand Down

0 comments on commit 4e21f97

Please sign in to comment.