Skip to content

Commit

Permalink
Enhanced ConcatAsyncOperationExecutor tests to ensure exceptions are …
Browse files Browse the repository at this point in the history
…thrown correctly
  • Loading branch information
le-nn committed Jul 22, 2024
1 parent 10eb4f5 commit 8ba5505
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 13 deletions.
15 changes: 6 additions & 9 deletions src/Yolu/Executors/ConcatAsyncOperationExecutor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public Task<T> ExecuteAsync<T>(Func<Task<T>> operation) {
var source = new TaskCompletionSource<T>();
_operations.Enqueue(new Operation<T>(operation, source));

Hadle();
Handle();

return source.Task;
}
Expand All @@ -35,12 +35,12 @@ public Task ExecuteAsync(Func<Task> operation) {
return 0;
}, source));

Hadle();
Handle();

return source.Task;
}

private async void Hadle() {
private async void Handle() {
if (_processingCount is not 0) {
return;
}
Expand All @@ -63,16 +63,13 @@ private interface IOperation {
}

private readonly struct Operation<T>(Func<Task<T>> func, TaskCompletionSource<T> taskSource) : IOperation {
private readonly Func<Task<T>> _func = func;
private readonly TaskCompletionSource<T> _taskSource = taskSource;

public async Task HandleAsync() {
try {
var result = await _func.Invoke();
_taskSource.SetResult(result);
var result = await func.Invoke();
taskSource.SetResult(result);
}
catch (Exception ex) {
_taskSource.SetException(ex);
taskSource.SetException(ex);
}
}
}
Expand Down
20 changes: 16 additions & 4 deletions test/Yolu.Test/Executors/ConcatOperationExecutorTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -164,9 +164,21 @@ public async Task Ensure_OrderOfExecution_With_TaskWhenAll() {
}

[Fact]
public async Task Test2() {
foreach (var i in 0..1000) {
await Task.Delay(2);
}
public async Task Ensure_Exception() {
var executor = new ConcatAsyncOperationExecutor();
var results = new MutableArray<int>();

await Assert.ThrowsAsync<Exception>(async () => {
await executor.ExecuteAsync(() => {
throw new Exception("Test");
});
});

await Assert.ThrowsAsync<InvalidOperationException>(async () => {
await executor.ExecuteAsync(() => {
throw new InvalidOperationException("Test");
});
});

}
}

0 comments on commit 8ba5505

Please sign in to comment.