Skip to content

Commit

Permalink
Move TaskState into Extensions class and make private, #1110
Browse files Browse the repository at this point in the history
  • Loading branch information
paulirwin committed Jan 22, 2025
1 parent 631a803 commit 400939d
Showing 1 changed file with 15 additions and 15 deletions.
30 changes: 15 additions & 15 deletions src/Lucene.Net.Tests/Support/Threading/JSR166TestCase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -406,13 +406,19 @@ internal void MediumRunnable()
}

// LUCENENET TODO: Complete port
}

/// <summary>
/// LUCENENET specific - fake support for an API that feels like ThreadPoolExecutor.
/// </summary>
internal static class JSR166TestCaseExtensions
{
/// <summary>
/// LUCENENET specific - state to keep track of tasks.
/// <see cref="LimitedConcurrencyLevelTaskScheduler"/> removes tasks from the list when they complete,
/// so this class is needed to keep track of them.
/// </summary>
internal class TaskState
private class TaskState
{
private readonly TaskFactory _factory;
private readonly List<Task> _tasks = new();
Expand All @@ -438,20 +444,14 @@ public void NewTask(Action action)

public bool JoinAll(TimeSpan timeout) => Task.WhenAll(_tasks).Wait(timeout);
}
}

/// <summary>
/// LUCENENET specific - fake support for an API that feels like ThreadPoolExecutor.
/// </summary>
internal static class JSR166TestCaseExtensions
{
private static readonly ConditionalWeakTable<TaskScheduler, JSR166TestCase.TaskState> _taskFactories = new();
private static readonly ConditionalWeakTable<TaskScheduler, TaskState> _taskFactories = new();

public static void Execute(this TaskScheduler scheduler, Action action)
{
if (!_taskFactories.TryGetValue(scheduler, out JSR166TestCase.TaskState? state))
if (!_taskFactories.TryGetValue(scheduler, out TaskState? state))
{
state = new JSR166TestCase.TaskState(scheduler);
state = new TaskState(scheduler);
_taskFactories.Add(scheduler, state);
}

Expand All @@ -460,7 +460,7 @@ public static void Execute(this TaskScheduler scheduler, Action action)

public static bool AwaitTermination(this TaskScheduler scheduler, TimeSpan timeout)
{
if (_taskFactories.TryGetValue(scheduler, out JSR166TestCase.TaskState? state))
if (_taskFactories.TryGetValue(scheduler, out TaskState? state))
{
return state.JoinAll(timeout);
}
Expand All @@ -470,7 +470,7 @@ public static bool AwaitTermination(this TaskScheduler scheduler, TimeSpan timeo

public static int GetActiveCount(this TaskScheduler scheduler)
{
if (_taskFactories.TryGetValue(scheduler, out JSR166TestCase.TaskState? state))
if (_taskFactories.TryGetValue(scheduler, out TaskState? state))
{
// Approximate the number of running threads, which shouldn't exceed the concurrency level
return Math.Min(scheduler.MaximumConcurrencyLevel, state.ActiveCount);
Expand All @@ -481,7 +481,7 @@ public static int GetActiveCount(this TaskScheduler scheduler)

public static int GetCompletedTaskCount(this TaskScheduler scheduler)
{
if (_taskFactories.TryGetValue(scheduler, out JSR166TestCase.TaskState? state))
if (_taskFactories.TryGetValue(scheduler, out TaskState? state))
{
return state.CompletedCount;
}
Expand All @@ -491,7 +491,7 @@ public static int GetCompletedTaskCount(this TaskScheduler scheduler)

public static int GetTaskCount(this TaskScheduler scheduler)
{
if (_taskFactories.TryGetValue(scheduler, out JSR166TestCase.TaskState? state))
if (_taskFactories.TryGetValue(scheduler, out TaskState? state))
{
return state.TaskCount;
}
Expand All @@ -510,7 +510,7 @@ public static void Shutdown(this TaskScheduler scheduler)
public static bool IsTerminated(this TaskScheduler scheduler)
{
if (scheduler is LimitedConcurrencyLevelTaskScheduler lcl
&& _taskFactories.TryGetValue(scheduler, out JSR166TestCase.TaskState? state))
&& _taskFactories.TryGetValue(scheduler, out TaskState? state))
{
return lcl.IsShutdown && state.AllCompleted;
}
Expand Down

0 comments on commit 400939d

Please sign in to comment.