Skip to content

Commit

Permalink
Fix bug and add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
zharkovstas committed Sep 28, 2023
1 parent a85407d commit ef5c920
Show file tree
Hide file tree
Showing 2 changed files with 119 additions and 23 deletions.
140 changes: 118 additions & 22 deletions Vostok.Applications.Scheduled.Tests/ScheduledActionRunner_Tests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Threading;
using System.Threading.Tasks;
using FluentAssertions;
using NUnit.Framework;
using Vostok.Commons.Time;
Expand All @@ -9,82 +10,76 @@
namespace Vostok.Applications.Scheduled.Tests;

[TestFixture]
[Parallelizable(ParallelScope.All)]
internal class ScheduledActionRunner_Tests
{
private const string ActionName = "TestAction";

[Test]
public void RunAsync_should_throw_when_CrashOnPayloadException_true()
public void RunAsync_should_throw_payload_exception_when_CrashOnPayloadException_true()
{
var options = new ScheduledActionOptions
{
CrashOnPayloadException = true
};

var action = new ScheduledAction(
"ExceptionThrower",
ActionName,
Scheduler.Periodical(50.Milliseconds()),
options,
_ => throw new InvalidOperationException("Error"));

var runner = new ScheduledActionRunner(action, new SilentLog(), new DevNullTracer(), diagnostics: null);

var cts = new CancellationTokenSource();
cts.CancelAfter(1.Seconds());

var run = async () => await runner.RunAsync(cts.Token);
var run = async () => await runner.RunAsync(GetCancellationToken());

run.Should().Throw<InvalidOperationException>();
}

[Test]
public void RunAsync_should_not_throw_when_CrashOnPayloadException_false()
public void RunAsync_should_not_throw_payload_exception_when_CrashOnPayloadException_false()
{
var options = new ScheduledActionOptions
{
CrashOnPayloadException = false
};

var action = new ScheduledAction(
"ExceptionThrower",
ActionName,
Scheduler.Periodical(50.Milliseconds()),
options,
_ => throw new InvalidOperationException("Error"));

var runner = new ScheduledActionRunner(action, new SilentLog(), new DevNullTracer(), diagnostics: null);

var cts = new CancellationTokenSource();
cts.CancelAfter(1.Seconds());

var run = async () => await runner.RunAsync(cts.Token);
var run = async () => await runner.RunAsync(GetCancellationToken());

run.Should().NotThrow();
}

[Test]
public void RunAsync_should_throw_OutOfMemoryException_even_when_CrashOnPayloadException_false()
public void RunAsync_should_throw_payload_OutOfMemoryException_even_when_CrashOnPayloadException_false()
{
var options = new ScheduledActionOptions
{
CrashOnPayloadException = false
};

var action = new ScheduledAction(
"ExceptionThrower",
ActionName,
Scheduler.Periodical(50.Milliseconds()),
options,
_ => throw new OutOfMemoryException());

var runner = new ScheduledActionRunner(action, new SilentLog(), new DevNullTracer(), diagnostics: null);

var cts = new CancellationTokenSource();
cts.CancelAfter(1.Seconds());

var run = async () => await runner.RunAsync(cts.Token);
var run = async () => await runner.RunAsync(GetCancellationToken());

run.Should().Throw<OutOfMemoryException>();
}

[Test]
public void RunAsync_should_not_throw_OutOfMemoryException_when_CrashOnPayloadOutOfMemoryException_false()
public void RunAsync_should_not_throw_payload_OutOfMemoryException_when_CrashOnPayloadOutOfMemoryException_false()
{
var options = new ScheduledActionOptions
{
Expand All @@ -93,18 +88,119 @@ public void RunAsync_should_not_throw_OutOfMemoryException_when_CrashOnPayloadOu
};

var action = new ScheduledAction(
"ExceptionThrower",
ActionName,
Scheduler.Periodical(50.Milliseconds()),
options,
_ => throw new OutOfMemoryException());

var runner = new ScheduledActionRunner(action, new SilentLog(), new DevNullTracer(), diagnostics: null);

var run = async () => await runner.RunAsync(GetCancellationToken());

run.Should().NotThrow();
}

[Test]
public void RunAsync_should_throw_scheduler_exception_when_CrashOnSchedulerException_true()
{
var options = new ScheduledActionOptions
{
CrashOnSchedulerException = true
};

var action = new ScheduledAction(
ActionName,
new ExceptionThrowingScheduler(new InvalidOperationException("Error")),
options,
_ => Task.CompletedTask);

var runner = new ScheduledActionRunner(action, new SilentLog(), new DevNullTracer(), diagnostics: null);

var run = async () => await runner.RunAsync(GetCancellationToken());

run.Should().Throw<InvalidOperationException>();
}

[Test]
public void RunAsync_should_not_throw_scheduler_exception_when_CrashOnSchedulerException_false()
{
var options = new ScheduledActionOptions
{
CrashOnSchedulerException = false
};

var action = new ScheduledAction(
ActionName,
new ExceptionThrowingScheduler(new InvalidOperationException("Error")),
options,
_ => Task.CompletedTask);

var runner = new ScheduledActionRunner(action, new SilentLog(), new DevNullTracer(), diagnostics: null);

var run = async () => await runner.RunAsync(GetCancellationToken());

run.Should().NotThrow();
}

[Test]
public void RunAsync_should_throw_scheduler_OutOfMemoryException_even_when_CrashOnSchedulerException_false()
{
var options = new ScheduledActionOptions
{
CrashOnSchedulerException = false
};

var action = new ScheduledAction(
ActionName,
new ExceptionThrowingScheduler(new OutOfMemoryException()),
options,
_ => Task.CompletedTask);

var runner = new ScheduledActionRunner(action, new SilentLog(), new DevNullTracer(), diagnostics: null);

var run = async () => await runner.RunAsync(GetCancellationToken());

run.Should().Throw<OutOfMemoryException>();
}

[Test]
public void RunAsync_should_not_throw_scheduler_OutOfMemoryException_when_CrashOnSchedulerOutOfMemoryException_false()
{
var options = new ScheduledActionOptions
{
CrashOnSchedulerException = false,
CrashOnSchedulerOutOfMemoryException = false
};

var action = new ScheduledAction(
ActionName,
new ExceptionThrowingScheduler(new OutOfMemoryException()),
options,
_ => Task.CompletedTask);

var runner = new ScheduledActionRunner(action, new SilentLog(), new DevNullTracer(), diagnostics: null);

var run = async () => await runner.RunAsync(GetCancellationToken());

run.Should().NotThrow();
}

private static CancellationToken GetCancellationToken()
{
var cts = new CancellationTokenSource();
cts.CancelAfter(1.Seconds());
return cts.Token;
}

var run = async () => await runner.RunAsync(cts.Token);
private class ExceptionThrowingScheduler : IScheduler
{
private readonly Exception exception;

run.Should().NotThrow();
public ExceptionThrowingScheduler(Exception exception)
{
this.exception = exception;
}

public DateTimeOffset? ScheduleNext(DateTimeOffset from) => throw exception;
}
}
2 changes: 1 addition & 1 deletion Vostok.Applications.Scheduled/ScheduledActionRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ async Task ExecutePayload()
{
return action.Scheduler.ScheduleNextWithSource(from);
}
catch (OutOfMemoryException) when (action.Options.CrashOnPayloadOutOfMemoryException)
catch (OutOfMemoryException) when (action.Options.CrashOnSchedulerOutOfMemoryException)
{
throw;
}
Expand Down

0 comments on commit ef5c920

Please sign in to comment.