Skip to content
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
32 changes: 28 additions & 4 deletions src/LogicLooper/LogicLooperPool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,17 @@ public sealed partial class LogicLooperPool : ILogicLooperPool, IDisposable
{
private readonly PooledLogicLooper[] _loopers;
private readonly ILogicLooperPoolBalancer _balancer;
private readonly CancellationTokenSource _shutdownTokenSource = new();
private bool _isDisposed;

/// <inheritdoc />
public IReadOnlyList<ILogicLooper> Loopers => _loopers;
public IReadOnlyList<ILogicLooper> Loopers
{
get
{
ThrowIfDisposed();
return _loopers;
}
}

/// <summary>
/// Initialize the looper pool with specified configurations.
Expand Down Expand Up @@ -101,15 +108,25 @@ public Task RegisterActionAsync<TState>(LogicLooperAsyncActionWithStateDelegate<
/// <inheritdoc />
public async Task ShutdownAsync(TimeSpan shutdownDelay)
{
ThrowIfDisposed();
await Task.WhenAll(_loopers.Select(x => x.WrappedLooper.ShutdownAsync(shutdownDelay)));
}

/// <inheritdoc />
public ILogicLooper GetLooper()
=> _balancer.GetPooledLooper(_loopers);
{
ThrowIfDisposed();
return _balancer.GetPooledLooper(_loopers);
}

public void Dispose()
{
if (_isDisposed)
{
return;
}
_isDisposed = true;

foreach (var looper in _loopers)
{
try
Expand All @@ -120,7 +137,14 @@ public void Dispose()
{
}
}
_shutdownTokenSource.Cancel();
}

private void ThrowIfDisposed()
{
if (_isDisposed)
{
throw new ObjectDisposedException(nameof(LogicLooperPool));
}
}

private class PooledLogicLooper : ILogicLooper
Expand Down
28 changes: 28 additions & 0 deletions test/LogicLooper.Test/LogicLooperPoolTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,34 @@ public async Task PooledLogicLooper_ShutdownAsyncFromLooperPool()
Assert.True(looperFactory.CreatedLoopers[3].IsShutdownRequested);
}

[Fact]
public async Task OperationsAfterDispose_ThrowObjectDisposedException()
{
var looperFactory = new FakeLogicLooperPoolLooperFactory();
var pool = new LogicLooperPool(60, 4, RoundRobinLogicLooperPoolBalancer.Instance, looperFactory);

pool.Dispose();

LogicLooperActionDelegate action = (in LogicLooperActionContext _) => false;
LogicLooperActionWithStateDelegate<int> actionWithState = (in LogicLooperActionContext _, int _) => false;
LogicLooperAsyncActionDelegate asyncAction = _ => ValueTask.FromResult(false);
LogicLooperAsyncActionWithStateDelegate<int> asyncActionWithState = (_, _) => ValueTask.FromResult(false);

Assert.Throws<ObjectDisposedException>(() => { _ = pool.RegisterActionAsync(action); });
Assert.Throws<ObjectDisposedException>(() => { _ = pool.RegisterActionAsync(action, LooperActionOptions.Default); });
Assert.Throws<ObjectDisposedException>(() => { _ = pool.RegisterActionAsync(actionWithState, 0); });
Assert.Throws<ObjectDisposedException>(() => { _ = pool.RegisterActionAsync(actionWithState, 0, LooperActionOptions.Default); });
Assert.Throws<ObjectDisposedException>(() => { _ = pool.RegisterActionAsync(asyncAction); });
Assert.Throws<ObjectDisposedException>(() => { _ = pool.RegisterActionAsync(asyncAction, LooperActionOptions.Default); });
Assert.Throws<ObjectDisposedException>(() => { _ = pool.RegisterActionAsync(asyncActionWithState, 0); });
Assert.Throws<ObjectDisposedException>(() => { _ = pool.RegisterActionAsync(asyncActionWithState, 0, LooperActionOptions.Default); });
Assert.Throws<ObjectDisposedException>(() => pool.GetLooper());
Assert.Throws<ObjectDisposedException>(() => _ = pool.Loopers);
await Assert.ThrowsAsync<ObjectDisposedException>(() => pool.ShutdownAsync(TimeSpan.Zero));

pool.Dispose();
}

class FakeSequentialLogicLooperPoolBalancer : ILogicLooperPoolBalancer
{
private int _count;
Expand Down
Loading