public async Task ExecuteAsync_executes_Action() { const double updatedBalance = 1.23; var sale = new Sale(saleID: 5) { Balance = 0 }; var sut = new FunctionAction <Sale>((sale1, _) => Task.FromResult(sale1.Balance = updatedBalance)); await sut.ExecuteAsync(sale, request : null, cancellationToken : default(CancellationToken)); Assert.Equal(updatedBalance, sale.Balance); }
public void ExecuteAsync_can_be_cancelled() { using (var cancellationSource = new CancellationTokenSource()) { var sale = new Sale(saleID: 3); var wasCancelled = false; var mutex = new Mutex(initiallyOwned: false); var sut = new FunctionAction <Sale>((_, cancelToken) => { mutex.WaitOne(); do { Task.Delay(millisecondsDelay: 123).Wait(); } while (!cancelToken.IsCancellationRequested); mutex.ReleaseMutex(); wasCancelled = true; return(Task.FromCanceled(cancelToken)); }); Task.Factory.StartNew(async() => { await sut.ExecuteAsync(sale, cancellationSource.Token, request: null); }, TaskCreationOptions.LongRunning); try { Task.Delay(555).Wait(); cancellationSource.Cancel(); } catch { cancellationSource.Cancel(); } mutex.WaitOne(); Assert.True(wasCancelled); } }