Skip to content

Commit 5776656

Browse files
authored
fixed unit testing and issue of (#586)
1 parent 6fe6bff commit 5776656

File tree

3 files changed

+57
-7
lines changed

3 files changed

+57
-7
lines changed

src/TickerQ/Src/TickerExecutionTaskHandler.cs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,15 @@ private async Task RunContextFunctionAsync(InternalFunctionContext context, bool
257257
catch (Exception ex)
258258
{
259259
lastException = ex;
260+
261+
context.SetProperty(x => x.ExceptionDetails, SerializeException(ex));
262+
263+
if (_serviceProvider.GetService(typeof(ITickerExceptionHandler)) is ITickerExceptionHandler handler)
264+
await handler.HandleExceptionAsync(ex, context.TickerId, context.Type);
265+
266+
await _internalTickerManager.UpdateTickerAsync(context, cancellationToken);
267+
268+
context.ResetUpdateProps();
260269
}
261270
}
262271

@@ -292,11 +301,6 @@ private async Task RunContextFunctionAsync(InternalFunctionContext context, bool
292301
_tickerQInstrumentation.LogJobFailed(context.TickerId, context.FunctionName, lastException, context.RetryCount);
293302
_tickerQInstrumentation.LogJobCompleted(context.TickerId, context.FunctionName, stopWatch.ElapsedMilliseconds, false);
294303

295-
var handler = _serviceProvider.GetService(typeof(ITickerExceptionHandler)) as ITickerExceptionHandler;
296-
297-
if (handler != null)
298-
await handler.HandleExceptionAsync(lastException, context.TickerId, context.Type);
299-
300304
await _internalTickerManager.UpdateTickerAsync(context, cancellationToken);
301305
}
302306

tests/TickerQ.Tests/SoftSchedulerNotifyDebounceTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ public void NotifySafely_AlwaysInvokes_ForZeroValue()
9999
debounce.NotifySafely(0);
100100
debounce.NotifySafely(0);
101101

102-
callCount.Should().BeGreaterOrEqualTo(2);
102+
callCount.Should().BeGreaterThanOrEqualTo(2);
103103
}
104104

105105
[Fact]

tests/TickerQ.Tests/TickerExecutionTaskHandlerTests.cs

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public async Task ExecuteTaskAsync_SetsElapsedTime_OnSuccess()
7575

7676
await _handler.ExecuteTaskAsync(context, isDue: false);
7777

78-
context.ElapsedTime.Should().BeGreaterOrEqualTo(0);
78+
context.ElapsedTime.Should().BeGreaterThanOrEqualTo(0);
7979
}
8080

8181
[Fact]
@@ -136,6 +136,52 @@ await exceptionHandler.Received(1).HandleExceptionAsync(
136136
Arg.Is(context.Type));
137137
}
138138

139+
[Fact]
140+
public async Task ExecuteTaskAsync_CallsExceptionHandler_ForEachFailedAttempt()
141+
{
142+
var exceptionHandler = Substitute.For<ITickerExceptionHandler>();
143+
var services = new ServiceCollection();
144+
services.AddSingleton(_internalManager);
145+
services.AddSingleton(_instrumentation);
146+
services.AddSingleton(exceptionHandler);
147+
var sp = services.BuildServiceProvider();
148+
var handler = new TickerExecutionTaskHandler(sp, _clock, _instrumentation, _internalManager);
149+
150+
var context = CreateContext(ct: (_, _, _) => throw new InvalidOperationException("boom"));
151+
context.Retries = 2;
152+
context.RetryIntervals = [0, 0];
153+
154+
await handler.ExecuteTaskAsync(context, isDue: false);
155+
156+
await exceptionHandler.Received(3).HandleExceptionAsync(
157+
Arg.Any<Exception>(),
158+
Arg.Is(context.TickerId),
159+
Arg.Is(context.Type));
160+
}
161+
162+
[Fact]
163+
public async Task ExecuteTaskAsync_UpdatesExceptionDetails_OnFailedAttemptBeforeRetriesComplete()
164+
{
165+
var context = CreateContext(ct: (_, _, _) => throw new InvalidOperationException("boom"));
166+
context.Retries = 1;
167+
context.RetryIntervals = [0];
168+
var observedUpdates = new List<(TickerStatus Status, string ExceptionDetails)>();
169+
170+
_internalManager
171+
.When(x => x.UpdateTickerAsync(Arg.Any<InternalFunctionContext>(), Arg.Any<CancellationToken>()))
172+
.Do(callInfo =>
173+
{
174+
var ctx = callInfo.Arg<InternalFunctionContext>();
175+
observedUpdates.Add((ctx.Status, ctx.ExceptionDetails));
176+
});
177+
178+
await _handler.ExecuteTaskAsync(context, isDue: false);
179+
180+
observedUpdates.Should().Contain(x =>
181+
x.Status == TickerStatus.InProgress &&
182+
!string.IsNullOrWhiteSpace(x.ExceptionDetails));
183+
}
184+
139185
#endregion
140186

141187
#region Cancellation Path

0 commit comments

Comments
 (0)