public async Task InvokeAsync_BaseEventArgsWithFaultedBackgroundTasks_ShouldCompleteFaulted() { // Arrange. var error = new Exception(); BaseEvent += (sender, e) => e.RegisterBackgroundTask(Task.CompletedTask); BaseEvent += (sender, e) => e.RegisterBackgroundTask(async cancellationToken => { await Task.Yield(); cancellationToken.ThrowIfCancellationRequested(); }); BaseEvent += (sender, e) => e.RegisterBackgroundTask(async cancellationToken => { await Task.Yield(); throw error; }); using (var cancellationSource = new CancellationTokenSource()) { cancellationSource.Cancel(); // Act. var args = new AsyncEventArgs(cancellationSource.Token); var thrown = await Assert.ThrowsAsync <Exception>(() => BaseEvent.InvokeAsync(this, args)); // Assert. Assert.Equal(error, thrown); } }
public async Task InvokeAsync_BaseEventArgsWithSucceededBackgroundTasks_ShouldCompleteSucceeded() { // Arrange. BaseEvent += (sender, e) => e.RegisterBackgroundTask(Task.CompletedTask); // Act. var args = new AsyncEventArgs(CancellationToken.None); await BaseEvent.InvokeAsync(this, args); }
public void InvokeAsync_BaseEventArgsWithNoSubscribers_ShouldReturnCompletedTask() { var task = BaseEvent.InvokeAsync(this, new AsyncEventArgs(CancellationToken.None)); Assert.Same(Task.CompletedTask, task); }