public void Start_ThrowsForDisposesObject()
        {
            var thread = new DedicatedAsynchronousProcessingThread(_defaultTimeSpan);

            thread.Dispose();
            Assert.Throws <ObjectDisposedException>(() => thread.Start());
        }
 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 Dispose_IsIdempotent()
 {
     using (var thread = new DedicatedAsynchronousProcessingThread(_defaultTimeSpan))
     {
         thread.Dispose();
         thread.Dispose();
     }
 }
 public void Start_ThrowsForAlreadyStartedObject()
 {
     using (var thread = new DedicatedAsynchronousProcessingThread(_defaultTimeSpan))
     {
         thread.Start();
         var exception = Assert.Throws <InvalidOperationException>(() => thread.Start());
         exception.Message.Should().Be("The processing thread is already started.");
     }
 }
        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);
         }
 }
Exemplo n.º 7
0
 /// <summary>
 /// Requests from the processing methods provided in this set are handled on a dedicated thread.
 /// </summary>
 /// <param name="fastProcessingMethods"></param>
 public InboundRequestProcessingHandler(IEnumerable <MessageMethod> fastProcessingMethods)
 {
     if (fastProcessingMethods == null)
     {
         throw new ArgumentNullException(nameof(fastProcessingMethods));
     }
     _fastProccessingMethods = new HashSet <MessageMethod>(fastProcessingMethods);
     // Lazily initialize the processing thread. It is not needed if there are no time critical methods.
     _processingThread = new Lazy <DedicatedAsynchronousProcessingThread>(() =>
     {
         var thread = new DedicatedAsynchronousProcessingThread(TimeSpan.FromMilliseconds(50));
         thread.Start();
         return(thread);
     });
 }
        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);
                }
        }