Skip to content

Commit

Permalink
small refactorings.
Browse files Browse the repository at this point in the history
  • Loading branch information
jmd authored and jmd committed Feb 6, 2024
1 parent f2122fb commit ca6c92c
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,6 @@

namespace DotJEM.Web.Scheduler.Test;

public class WebTaskSchedulerTest
{
[Test]
public void SayHello_ReturnsHello()
{

}
}

public class ScheduledTaskTest
{
[Test]
Expand Down Expand Up @@ -62,17 +53,4 @@ await Task.WhenAll(

Assert.That(executed, Is.EqualTo(1));
}
}
internal static class TaskExt
{
public static void FireAndForget(this Task task)
{

}
public static Func<T, Task> ToAsync<T>(this Action<T> action)
{
#pragma warning disable CS1998
return async arg => action(arg);
#pragma warning restore CS1998
}
}
15 changes: 15 additions & 0 deletions src/DotJEM.Web.Scheduler.Test/TaskExt.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
namespace DotJEM.Web.Scheduler.Test;

internal static class TaskExt
{
public static void FireAndForget(this Task task)
{

}
public static Func<T, Task> ToAsync<T>(this Action<T> action)
{
#pragma warning disable CS1998
return async arg => action(arg);
#pragma warning restore CS1998
}
}
21 changes: 21 additions & 0 deletions src/DotJEM.Web.Scheduler.Test/WebTaskSchedulerTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using NUnit.Framework;

namespace DotJEM.Web.Scheduler.Test;

public class WebTaskSchedulerTest
{
[Test]
public async Task SayHello_ReturnsHello()
{
WebTaskScheduler scheduler = new WebTaskScheduler();
IScheduledTask? task = null;
bool called = false;
task = scheduler.ScheduleTask("Foo", timedOut => { called = true; }, TimeSpan.FromMilliseconds(500));
await Task.WhenAny(task.WhenCompleted(), Task.Delay(TimeSpan.FromSeconds(1)));
task.Dispose();


Assert.IsTrue(called);

}
}
2 changes: 1 addition & 1 deletion src/DotJEM.Web.Scheduler/IScheduledTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,5 +71,5 @@ public interface IScheduledTask : IDisposable
///
/// </summary>
/// <returns></returns>
TaskAwaiter<int> GetAwaiter();
Task<int> WhenCompleted();
}
8 changes: 5 additions & 3 deletions src/DotJEM.Web.Scheduler/ScheduledTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,11 @@ public void Start()
}
}

/// <summary>Gets an awaiter used to await this <see cref="ScheduledTask" />, the task is signaled when the task runs to completion and is disposed.</summary>
/// <returns>An awaiter instance.</returns>
public TaskAwaiter<int> GetAwaiter() => completeCompletionSource.Task.GetAwaiter();
/// <summary>
///
/// </summary>
/// <returns></returns>
public Task<int> WhenCompleted() => completeCompletionSource.Task;

/// <summary>
/// Registers the next call for the scheduled task onto the threadpool.
Expand Down

0 comments on commit ca6c92c

Please sign in to comment.