Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Replace Task.Factory.StartNew with Task.Run #278

Merged
merged 12 commits into from
Apr 10, 2023
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ private void Start()
InternalBufferSize, channelCapacity)));
}
});
Task.Factory.StartNew(() =>
Task.Run(() =>
{
try
{
Expand All @@ -312,9 +312,7 @@ private void Start()
//Ignore any exception
}
},
token,
TaskCreationOptions.LongRunning,
TaskScheduler.Default)
token)
.ContinueWith(_ =>
{
if (channel.Writer.TryComplete())
Expand Down
2 changes: 1 addition & 1 deletion Source/Testably.Abstractions.Testing/Notification.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public void Wait(Func<TValue, bool>? filter = null,
Task? task = null;
if (executeWhenWaiting != null)
{
task = Task.Factory.StartNew(executeWhenWaiting.Invoke);
task = Task.Run(executeWhenWaiting.Invoke);
}

if (!_reset.Wait(timeout) ||
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public void Sleep(TimeSpan timeout)
}

_mockTimeSystem.TimeProvider.AdvanceBy(timeout);
Thread.Sleep(0);
Thread.Yield();
_callbackHandler.InvokeThreadSleepCallbacks(timeout);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Security.AccessControl;
using System.Threading.Tasks;
using Testably.Abstractions.AccessControl.Tests.TestHelpers;

namespace Testably.Abstractions.AccessControl.Tests;
Expand Down Expand Up @@ -96,14 +97,14 @@ public void SetAccessControl_ShouldChangeAccessControl()
}

[SkippableFact]
public void SetAccessControl_ShouldNotUpdateTimes()
public async Task SetAccessControl_ShouldNotUpdateTimes()
{
Skip.IfNot(Test.RunsOnWindows);

Test.SkipIfLongRunningTestsShouldBeSkipped(FileSystem);

FileSystem.File.WriteAllText("foo.txt", "abc");
TimeSystem.Thread.Sleep(3000);
await TimeSystem.Task.Delay(3000);
DateTime previousCreationTimeUtc = FileSystem.File.GetCreationTimeUtc("foo.txt");
DateTime previousLastAccessTimeUtc = FileSystem.File.GetLastAccessTimeUtc("foo.txt");
DateTime previousLastWriteTimeUtc = FileSystem.File.GetLastWriteTimeUtc("foo.txt");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using AutoFixture.Xunit2;
using System.Security.AccessControl;
using System.Threading.Tasks;
using Testably.Abstractions.AccessControl.Tests.TestHelpers;

namespace Testably.Abstractions.AccessControl.Tests;
Expand Down Expand Up @@ -98,14 +99,14 @@ public void SetAccessControl_ShouldChangeAccessControl()

[SkippableTheory]
[AutoData]
public void SetAccessControl_ShouldNotUpdateTimes(string path)
public async Task SetAccessControl_ShouldNotUpdateTimes(string path)
{
Skip.IfNot(Test.RunsOnWindows);

Test.SkipIfLongRunningTestsShouldBeSkipped(FileSystem);

FileSystem.Directory.CreateDirectory(path);
TimeSystem.Thread.Sleep(3000);
await TimeSystem.Task.Delay(3000);
DateTime previousCreationTimeUtc = FileSystem.File.GetCreationTimeUtc(path);
DateTime previousLastAccessTimeUtc = FileSystem.File.GetLastAccessTimeUtc(path);
DateTime previousLastWriteTimeUtc = FileSystem.File.GetLastWriteTimeUtc(path);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,6 @@ public void InternalBufferSize_ShouldResetQueue(string path1, string path2)
}

block2.Wait(100).Should().BeFalse();
fileSystemWatcher.Dispose();
result.Should().BeNull();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public void Sleep_InfiniteTimeSpan_ShouldNotThrowException()
}

[Fact]
public void Sleep_LessThanInfinite_ShouldNotThrowException()
public void Sleep_LessThanInfinite_ShouldThrowArgumentOutOfRangeException()
{
MockTimeSystem timeSystem = new();
Exception? exception =
Expand All @@ -80,7 +80,7 @@ public void Sleep_LessThanInfinite_ShouldNotThrowException()
}

[Fact]
public void Sleep_LessThanInfiniteTimeSpan_ShouldNotThrowException()
public void Sleep_LessThanInfiniteTimeSpan_ShouldThrowArgumentOutOfRangeException()
{
MockTimeSystem timeSystem = new();
Exception? exception =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public void EndInit_ShouldRestartListening(string path)
}
});
IWaitForChangedResult result =
fileSystemWatcher.WaitForChanged(WatcherChangeTypes.Created, 100);
fileSystemWatcher.WaitForChanged(WatcherChangeTypes.Created, 30000);

fileSystemWatcher.EnableRaisingEvents.Should().BeTrue();
result.TimedOut.Should().BeFalse();
Expand Down