public async Task ShouldExecuteActionAsync() { // Arrange var semaphore = new SemaphoreSlim(1); // Act + Assert await semaphore.ExecuteAsync(async innerCt => await Task.Yield(), CancellationToken.None); }
public async Task ShouldExecuteFuncAsync() { // Arrange var semaphore = new SemaphoreSlim(1); // Act var result = await semaphore.ExecuteAsync(async innerCt => { await Task.Yield(); return(3); }, CancellationToken.None); // Assert result.Should().Be(3); }
/// <summary> /// Executes asynchronous action inside semaphore. /// </summary> /// <param name="semaphore">The semaphore.</param> /// <param name="func">The function.</param> /// <param name="cancellationToken">The cancellation token.</param> /// <returns></returns> public static Task ExecuteAsync(this SemaphoreSlim semaphore, Func <CancellationToken, Task> func, CancellationToken cancellationToken) { return(semaphore.ExecuteAsync(async(innerCt) => { await func(innerCt).ConfigureAwait(false); return 0; }, cancellationToken)); }