Skip to content
This repository has been archived by the owner on Jan 23, 2023. It is now read-only.
/ corefx Public archive

Add System.ComponentModel.EventBasedAsync tests #20931

Merged
merged 4 commits into from
Jun 19, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System.Diagnostics;
using System.Threading;
using Xunit;

namespace System.ComponentModel.Tests
{
public class AsyncOperationFinalizerTests : RemoteExecutorTestBase
{
[Fact]
public void Finalizer_OperationCompleted_DoesNotCallOperationCompleted()
{
RemoteInvoke(() =>
{
Completed();

GC.Collect();
GC.WaitForPendingFinalizers();

return SuccessExitCode;
}).Dispose();
}

private void Completed()
{
// This is in a helper method to ensure the JIT doesn't artifically extend the lifetime of the operation.
var tracker = new OperationCompletedTracker();
AsyncOperationManager.SynchronizationContext = tracker;
AsyncOperation operation = AsyncOperationManager.CreateOperation(new object());

Assert.False(tracker.OperationDidComplete);
operation.OperationCompleted();
Assert.True(tracker.OperationDidComplete);
}

[Fact]
public void Finalizer_OperationNotCompleted_CompletesOperation()
{
RemoteInvoke(() =>
{
var tracker = new OperationCompletedTracker();
NotCompleted(tracker);

GC.Collect();
GC.WaitForPendingFinalizers();

Assert.True(tracker.OperationDidComplete);

return SuccessExitCode;
}).Dispose();
}

private void NotCompleted(OperationCompletedTracker tracker)
{
// This is in a helper method to ensure the JIT doesn't artifically extend the lifetime of the operation.
AsyncOperationManager.SynchronizationContext = tracker;
AsyncOperation operation = AsyncOperationManager.CreateOperation(new object());
Assert.False(tracker.OperationDidComplete);
}

public class OperationCompletedTracker : SynchronizationContext
{
public bool OperationDidComplete { get; set; }

public override void OperationCompleted()
{
Assert.False(OperationDidComplete);
OperationDidComplete = true;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// See the LICENSE file in the project root for more information.

using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;
Expand Down Expand Up @@ -70,6 +71,30 @@ public void TestBackgroundWorkerBasic()
}
}

[Fact]
public void RunWorkerAsync_NoOnWorkHandler_SetsResultToNull()
{
var backgroundWorker = new BackgroundWorker { WorkerReportsProgress = true };
bool isCompleted = false;
backgroundWorker.RunWorkerCompleted += (sender, e) =>
{
isCompleted = true;
Assert.Null(e.Result);
Assert.False(backgroundWorker.IsBusy);
};
backgroundWorker.RunWorkerAsync();

var stopwatch = new Stopwatch();
stopwatch.Start();
while (!isCompleted)
{
if (stopwatch.Elapsed > TimeSpan.FromSeconds(10))
{
throw new Exception("The background worker never completed.");
}
}
}

#region TestCancelAsync

private ManualResetEventSlim manualResetEvent3;
Expand Down Expand Up @@ -283,6 +308,16 @@ public void TestReportProgressSync()
Assert.Equal(expectedProgress, actualProgress);
}

[Fact]
public void ReportProgress_NoProgressHandle_Nop()
{
var backgroundWorker = new BackgroundWorker { WorkerReportsProgress = true };
foreach (int i in new int[] { 1, 2, 3, 4, 5 })
{
backgroundWorker.ReportProgress(i);
}
}

[Fact]
public void TestReportProgressWithWorkerReportsProgressFalse()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'netstandard-Release|AnyCPU'" />
<ItemGroup>
<Compile Include="AsyncOperationManagerTests.cs" />
<Compile Include="AsyncOperationFinalizerTests.cs" />
<Compile Include="RunWorkerCompletedEventArgsTests.cs" />
<Compile Include="AsyncCompletedEventArgsTests.cs" />
<Compile Include="DoWorkEventArgsTests.cs" />
Expand All @@ -20,5 +21,11 @@
<Compile Include="BackgroundWorkerTests.cs" />
<Compile Include="TestException.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="$(CommonTestPath)\System\Diagnostics\RemoteExecutorConsoleApp\RemoteExecutorConsoleApp.csproj">
<Project>{69e46a6f-9966-45a5-8945-2559fe337827}</Project>
<Name>RemoteExecutorConsoleApp</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), dir.targets))\dir.targets" />
</Project>