public void MultipleWaitAsync_Set_OnlyOneIsCompleted() { AsyncContext.Run(async () => { var are = new AsyncAutoResetEvent(true); var task1 = are.WaitAsync(); var task2 = are.WaitAsync(); Assert.IsTrue(task1.IsCompleted); await AssertEx.NeverCompletesAsync(task2); }); }
public void MultipleWaitAsync_AfterSet_OnlyOneIsCompleted() { Test.Async(async () => { var are = new AsyncAutoResetEvent(); are.Set(); var task1 = are.WaitAsync(); var task2 = are.WaitAsync(); Assert.IsTrue(task1.IsCompleted); await AssertEx.NeverCompletesAsync(task2); }); }
public void WaitAsync_Set_CompletesSynchronously() { var are = new AsyncAutoResetEvent(true); var task = are.WaitAsync(); Assert.IsTrue(task.IsCompleted); }
public void WaitAsync_Unset_IsNotCompleted() { AsyncContext.Run(async () => { var are = new AsyncAutoResetEvent(); var task = are.WaitAsync(); await AssertEx.NeverCompletesAsync(task); }); }
public async Task CanRecoverFromError() { var resetEvent = new AsyncAutoResetEvent(false); int hits = 0; Func<Task<DateTime?>> callback = () => { Interlocked.Increment(ref hits); _logger.Info("Callback called for the #{time} time", hits); if (hits == 1) throw new Exception("Error in callback"); resetEvent.Set(); return Task.FromResult<DateTime?>(null); }; using (var timer = new ScheduledTimer(callback, loggerFactory: Log)) { timer.ScheduleNext(); await resetEvent.WaitAsync(new CancellationTokenSource(500).Token); Assert.Equal(2, hits); } }
public virtual async Task WontKeepMessagesWithNoSubscribers() { var messageBus = GetMessageBus(); if (messageBus == null) return; using (messageBus) { await messageBus.PublishAsync(new SimpleMessageA { Data = "Hello" }); await SystemClock.SleepAsync(100); var resetEvent = new AsyncAutoResetEvent(false); messageBus.Subscribe<SimpleMessageA>(msg => { Assert.Equal("Hello", msg.Data); resetEvent.Set(); }); await Assert.ThrowsAsync<TaskCanceledException>(async () => await resetEvent.WaitAsync(TimeSpan.FromMilliseconds(100))); } }
public void WaitAsync_PreCancelled_Set_SynchronouslyCompletesWait() { var are = new AsyncAutoResetEvent(true); var token = new CancellationToken(true); var task = are.WaitAsync(token); Assert.IsTrue(task.IsCompleted); Assert.IsFalse(task.IsCanceled); Assert.IsFalse(task.IsFaulted); }
public void WaitAsync_Cancelled_ThrowsException() { AsyncContext.Run(async () => { var are = new AsyncAutoResetEvent(); var cts = new CancellationTokenSource(); cts.Cancel(); var task = are.WaitAsync(cts.Token); await AssertEx.ThrowsExceptionAsync<OperationCanceledException>(task); }); }
public void WaitAsyncFromCustomSynchronizationContext_PreCancelled_Unset_SynchronouslyCancels() { AsyncContext.Run(() => { var are = new AsyncAutoResetEvent(false); var token = new CancellationToken(true); var task = are.WaitAsync(token); Assert.IsTrue(task.IsCompleted); Assert.IsTrue(task.IsCanceled); Assert.IsFalse(task.IsFaulted); }); }
public void WaitAsync_Cancelled_DoesNotAutoReset() { AsyncContext.Run(async () => { var are = new AsyncAutoResetEvent(); var cts = new CancellationTokenSource(); cts.Cancel(); var task1 = are.WaitAsync(cts.Token); task1.WaitWithoutException(); are.Set(); var task2 = are.WaitAsync(); await task2; }); }
public static async Task<SocketError> ExecuteAsync(this Socket socket, Func<Socket, Func<SocketAsyncEventArgs, bool>> methodPredicate, SocketAsyncEventArgs socketAsyncEventArgs, CancellationToken cancellationToken) { var asyncAutoResetEvent = new AsyncAutoResetEvent(false); socketAsyncEventArgs.Completed += (sender, args) => { asyncAutoResetEvent.Set(); }; var method = methodPredicate.Invoke(socket); var async = method.Invoke(socketAsyncEventArgs); if (!async) { asyncAutoResetEvent.Set(); } await asyncAutoResetEvent.WaitAsync(cancellationToken); return socketAsyncEventArgs.SocketError; }