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
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,15 @@ public void SchedulePeriodicStopsAfterDisposeOfSubscriber()
inner.AdvanceBy(TimeSpan.FromMinutes(1).Ticks);
called.Should().Be(3);
}

[Fact]
public void DisposeTwice_NoException()
{
var (_, disposableScheduler) = CreateScheduler();

disposableScheduler.Dispose();
disposableScheduler.Dispose();
}

private (TestScheduler inner, DisposableScheduler disposableScheduler) CreateScheduler()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System.Threading;
using NetDaemon.Extensions.Scheduler;
using Xunit;

namespace NetDaemon.Extensions.Scheduling.Tests;

public class DisposableTimerTest
{
[Fact]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion:
Because this is really a helper class that should be internal it does not seem to deserve its own test class. You could move this test to SchedulerExtensionTest.SchedulePeriodicStopsAfterDisposeOfSubscriber() and dispose the result of disposable.RunEvery there twice

public void DisposeTwice_NoException()
{
var timer = new DisposableTimer(CancellationToken.None);

timer.Dispose();
timer.Dispose();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,11 @@ public IDisposable Schedule<TState>(TState state, DateTimeOffset dueTime, Func<I
/// <inheritdoc />
public void Dispose()
{
_isDisposed = true;

_cancellationTokenSource.Cancel();
_cancellationTokenSource.Dispose();
if (!_isDisposed)
{
_cancellationTokenSource.Cancel();
_cancellationTokenSource.Dispose();
_isDisposed = true;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ public sealed class DisposableTimer : IDisposable
{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this class should have been internal actually

private readonly CancellationTokenSource _combinedToken;
private readonly CancellationTokenSource _internalToken;
private bool _disposed;

/// <summary>
/// Constructor
/// </summary>
Expand All @@ -29,8 +31,11 @@ public DisposableTimer(CancellationToken token)
/// </summary>
public void Dispose()
{
_internalToken.Cancel();
_combinedToken.Dispose();
_internalToken.Dispose();
if (!_disposed){
_internalToken.Cancel();
_combinedToken.Dispose();
_internalToken.Dispose();
_disposed = true;
}
}
}