public void Enqueue_ThrowsForNotStartedObject() { using (var thread = new DedicatedAsynchronousProcessingThread(_defaultTimeSpan)) { var exception = Assert.Throws <InvalidOperationException>(() => thread.Enqueue(() => Task.CompletedTask)); exception.Message.Should().Be("The processing thread is not started yet."); } }
public void Push_ThrowsForDisposesObject() { var thread = new DedicatedAsynchronousProcessingThread(_defaultTimeSpan); thread.Start(); thread.Dispose(); Assert.Throws <ObjectDisposedException>(() => thread.Enqueue(() => Task.CompletedTask)); }
public void Enqueue_ExecutesTaskAsynchronously() { using (var thread = new DedicatedAsynchronousProcessingThread(_defaultTimeSpan)) using (var handledEvent = new ManualResetEventSlim(initialState: false)) { thread.Start(); var executed = false; Func <Task> task = () => { executed = true; handledEvent.Set(); return(Task.CompletedTask); }; thread.Enqueue(task); handledEvent.Wait(); Assert.True(executed); } }
public void Enqueue_ExecutesTaskAsynchronouslyInSameOrderAsPush() { using (var countdownEvent = new CountdownEvent(3)) using (var thread = new DedicatedAsynchronousProcessingThread(_defaultTimeSpan)) { thread.Start(); var queue = new Queue <int>(); Func <Task> task1 = () => { queue.Enqueue(1); countdownEvent.Signal(); return(Task.CompletedTask); }; Func <Task> task2 = () => { queue.Enqueue(2); countdownEvent.Signal(); return(Task.CompletedTask); }; Func <Task> task3 = () => { queue.Enqueue(3); countdownEvent.Signal(); return(Task.CompletedTask); }; thread.Enqueue(task1); thread.Enqueue(task2); thread.Enqueue(task3); countdownEvent.Wait(); Assert.Equal(1, queue.Dequeue()); Assert.Equal(2, queue.Dequeue()); Assert.Equal(3, queue.Dequeue()); Assert.Empty(queue); } }