예제 #1
0
 public void PointToPointPerfTestWithInt()
 {
     var executor = new BoundedQueue(new PerfExecutor()) { MaxDepth = 10000, MaxEnqueueWaitTimeInMs = 1000 };
     using (var fiber = new ThreadFiber(executor))
     {
         fiber.Start();
         var channel = new Channel<int>();
         const int max = 5000000;
         var reset = new AutoResetEvent(false);
         Action<int> onMsg = delegate(int count)
                                 {
                                     if (count == max)
                                     {
                                         reset.Set();
                                     }
                                 };
         channel.Subscribe(fiber, onMsg);
         using (new PerfTimer(max))
         {
             for (var i = 0; i <= max; i++)
             {
                 channel.Publish(i);
             }
             Assert.IsTrue(reset.WaitOne(30000, false));
         }
     }
 }
        public void CheckPutBlockingOnAFullBoundedQueue()
        {
            var  bq          = new BoundedQueue <int>(1);
            bool interrupted = false;

            Thread t = new Thread(() =>
            {
                try {
                    bq.Put(1);
                    // will block
                    bq.Put(2);
                }
                catch (ThreadInterruptedException)
                {
                    interrupted = true;
                }
            });

            t.Start();
            // Give a security margin to thread t1 execution.
            // Not really rocket science, but...
            Thread.Sleep(3000);

            t.Interrupt();
            t.Join();
            // success if the previous interruption
            // removes thread from blocking in second Put operation
            Assert.IsTrue(interrupted);
        }
예제 #3
0
        private static void RunBoundedQueue()
        {
            var executor = new BoundedQueue(new PerfExecutor())
            {
                MaxDepth = 10000, MaxEnqueueWaitTimeInMs = 1000
            };

            using (var fiber = new ThreadFiber(executor))
            {
                fiber.Start();
                var                channel = new Channel <MsgStruct>();
                const int          max     = 5000000;
                var                reset   = new AutoResetEvent(false);
                Action <MsgStruct> onMsg   = delegate(MsgStruct count)
                {
                    if (count.count == max)
                    {
                        reset.Set();
                    }
                };
                channel.Subscribe(fiber, onMsg);
                using (new PerfTimer(max))
                {
                    for (var i = 0; i <= max; i++)
                    {
                        channel.Publish(new MsgStruct {
                            count = i
                        });
                    }
                    Assert.IsTrue(reset.WaitOne(30000, false));
                }
            }
        }
예제 #4
0
        public void PointToPointPerfTestWithObject()
        {
            var executor = new BoundedQueue(new PerfExecutor())
            {
                MaxDepth = 100000, MaxEnqueueWaitTimeInMs = 1000
            };

            using (var fiber = new ThreadFiber(executor))
            {
                fiber.Start();
                var             channel = new Channel <object>();
                const int       max     = 5000000;
                var             reset   = new AutoResetEvent(false);
                var             end     = new object();
                Action <object> onMsg   = delegate(object msg)
                {
                    if (msg == end)
                    {
                        reset.Set();
                    }
                };
                channel.Subscribe(fiber, onMsg);
                using (new PerfTimer(max))
                {
                    var msg = new object();
                    for (var i = 0; i <= max; i++)
                    {
                        channel.Publish(msg);
                    }
                    channel.Publish(end);
                    Assert.IsTrue(reset.WaitOne(30000, false));
                }
            }
        }
예제 #5
0
        public void OneProdOneClear()
        {
            BoundedQueue <int> queue = new BoundedQueue <int>(2);

            ParallelTasks ptasks = new ParallelTasks();

            // PRODUCER thread inserts 4 elements
            ptasks.Add("Producer", () =>
            {
                for (int i = 0; i < 4; i++)
                {
                    queue.Enqueue(i);
                }
            });

            // CLEAR thread clears queue once it reaches size 2
            ptasks.Add("Clear", () =>
            {
                while (queue.Size() < 2)
                {
                    Thread.Sleep(0);
                }
                queue.Clear();
            });

            ptasks.Execute();
        }
예제 #6
0
        public static ILoggingBuilder AddMemoryLogger(this ILoggingBuilder builder, BoundedQueue <string> queue)
        {
            builder.AddProvider(new MemoryLoggerProvider(queue));
            builder.AddFilter <MemoryLoggerProvider>(x => true);

            return(builder);
        }
예제 #7
0
        public void OneProdOneCons()
        {
            BoundedQueue <int> queue = new BoundedQueue <int>(2);

            ParallelTasks ptasks = new ParallelTasks();

            // PRODUCER thread inserts 4 elements
            ptasks.Add("Producer", () =>
            {
                for (int i = 0; i < 4; i++)
                {
                    queue.Enqueue(i);
                }
            });

            // CONSUMER thread removes 4 elements
            ptasks.Add("Consumer", () =>
            {
                for (int i = 0; i < 4; i++)
                {
                    Assert.AreEqual(i, queue.Dequeue());
                }
            });

            ptasks.Execute();

            // check size: should be zero
            Assert.AreEqual(queue.Size(), 0);
        }
예제 #8
0
 public StreamBuffer(int maxBufferSize, int maxLiveBufferSize, int initialSequence, bool startInHistory)
 {
     Live             = !startInHistory;
     _initialSequence = initialSequence;
     _maxBufferSize   = maxBufferSize;
     _liveBuffer      = new BoundedQueue <OutstandingMessage>(maxLiveBufferSize);
 }
예제 #9
0
        public void BoundedQueueTest()
        {
            BoundedQueue <int> queue = new BoundedQueue <int>(2);

            ParallelTasks ptasks = new ParallelTasks();

            // PRODUCER thread 1 inserts 3 elements
            ptasks.Add("Producer1", () => {
                for (int i = 0; i < 3; i++)
                {
                    queue.Enqueue(i);
                }
            });

            // PRODUCER thread 2 inserts 3 elements
            ptasks.Add("Producer2", () => {
                for (int i = 0; i < 3; i++)
                {
                    queue.Enqueue(i);
                }
            });

            // CONSUMER thread removes 4 elements
            ptasks.Add("Consumer", () => {
                for (int i = 0; i < 4; i++)
                {
                    System.Diagnostics.Debug.Assert(i >= queue.Dequeue());
                }
            });

            ptasks.Execute();

            // check size: should be two
            Assert.AreEqual(2, queue.Size(), "The final size of the queue.");
        }
        public void TestTransfer1To10ViaBoundedQueue()
        {
            int[] sendingNumbers  = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
            int[] receivedNumbers = new int[10];
            using (var bb = new BoundedQueue <int>(1))
            {
                Thread producer = new Thread(() =>
                {
                    foreach (int i in sendingNumbers)
                    {
                        bb.Put(i);
                    }
                });

                Thread consumer = new Thread(() =>
                {
                    for (int i = 0; i < 10; ++i)
                    {
                        int val            = bb.Get();
                        receivedNumbers[i] = val;
                    }
                });

                consumer.Start();
                producer.Start();
                producer.Join();
                consumer.Join();
                Assert.AreEqual(sendingNumbers, receivedNumbers);
            }
        }
예제 #11
0
 public StreamBuffer(int maxBufferSize, int maxLiveBufferSize, int initialSequence, bool startInHistory)
 {
     Live = !startInHistory;
     _initialSequence = initialSequence;
     _maxBufferSize = maxBufferSize;
     _liveBuffer = new BoundedQueue<OutstandingMessage>(maxLiveBufferSize);
 }
예제 #12
0
        public void BurstProducerManyConsumersTest()
        {
            const int CAPACITY = 100;
            var       queue    = new BoundedQueue <int>(CAPACITY);


            BurstProducerManyConsumersTestImpl(queue);
        }
예제 #13
0
        public void IfEnqueueIsEqualDequeue()
        {
            var queue = new BoundedQueue <int>(2);

            queue.Enqueue(1);

            Assert.That(queue.Dequeue(), Is.EqualTo(1), "Some useful error message");
        }
예제 #14
0
 public void TestBoundedQueueInvalidMaxSize()
 {
     Assert.Throws <ArgumentException>(
         () =>
     {
         BoundedQueue <int> queue = new BoundedQueue <int>(-10);
     });
 }
    public void QueueShouldNotDequeueEmptyQueue()
    {
        var queue = new BoundedQueue <int>(null);

        var result = queue.TryDequeue(out var item);

        result.ShouldBeFalse();
        item.ShouldBe(default);
예제 #16
0
        public void TwoInTwoOut()
        {
            BoundedQueue <int> queue = new BoundedQueue <int>(2);

            queue.Enqueue(1);
            queue.Enqueue(2);
            Assert.AreEqual(queue.Dequeue(), 1);
            Assert.AreEqual(queue.Dequeue(), 2);
        }
예제 #17
0
        //[TestProperty("ChessExpectedResult", "Deadlock")]
        public void NoProducer()
        {
            BoundedQueue <int> queue = new BoundedQueue <int>(10);

            while (true)
            {
                queue.Dequeue();
            }
        }
예제 #18
0
        internal Compressor(CompressionSettings settings, Stream stream)
        {
            ChunkSize      = settings.ChunkSize;
            m_threads      = new ThreadManager(settings.ThreadsCount);
            m_queue        = new BoundedQueue(settings.BufferCapacity);
            m_outputStream = new OutputStream(stream);

            m_threads.OnExceptionOccured += threads_OnExceptionOccured;
        }
예제 #19
0
 public void TestBoundedQueueCopyToNullArray()
 {
     Assert.Throws <ArgumentNullException>(
         () =>
     {
         BoundedQueue <int> queue = new BoundedQueue <int>(Enumerable.Range(0, 10), 5);
         queue.CopyTo(null, 0);
     });
 }
예제 #20
0
        //[TestProperty("ChessExpectedResult", "Deadlock")]
        public void NoConsumer()
        {
            BoundedQueue <int> queue = new BoundedQueue <int>(10);
            int i = 0;

            while (true)
            {
                queue.Enqueue(i++);
            }
        }
    public void QueueShouldNotEnqueueFullQueue()
    {
        var queue = new BoundedQueue <int>(1);

        var enqueueResult1 = queue.TryEnqueue(1);
        var enqueueResult2 = queue.TryEnqueue(2);

        enqueueResult1.ShouldBeTrue();
        enqueueResult2.ShouldBeFalse();
    }
예제 #22
0
        public void TestBoundedQueueAbsorbWithMaxsizeOne()
        {
            BoundedQueue <int> queue = new BoundedQueue <int>(1, true);

            queue.Enqueue(0);
            queue.Enqueue(10);
            Assert.True(queue.Count == 1);
            Assert.True(queue.Dequeue() == 10);
            Assert.True(queue.Count == 0);
        }
예제 #23
0
        public void TestBoundedQueueAbsorb()
        {
            BoundedQueue <int> queue = new BoundedQueue <int>(Enumerable.Range(0, 10), 6, true);

            Assert.True(queue.Count == 6);
            Assert.True(queue.Peek() == 0);
            queue.Enqueue(7);
            Assert.True(queue.Count == 6);
            Assert.True(queue.Peek() == 1);
        }
예제 #24
0
        public void TestBoundedQueueCollectionOperations()
        {
            var queue = new BoundedQueue <int>(2000);

            for (var i = 0; i < 1000; i++)
            {
                queue.Enqueue(i + 1);
            }
            queue.CollectionOperations <int>(1000);
        }
예제 #25
0
        public async Task TryEnqueueShouldTimeout()
        {
            var queue = new BoundedQueue <int>(2);

            queue.Enqueue(1);
            queue.Enqueue(2);
            var ok = queue.TryEnqueue(3, 500);

            Assert.That(ok, Is.False);
        }
예제 #26
0
        public void OneProducerOneConsumerBlockedOnGet()
        {
            const int CAPACITY          = 100;
            const int MAX_VAL           = 10000;
            const int CONCLUDED_TIMEOUT = 5000;

            ManualResetEvent   concluded = new ManualResetEvent(false);
            BoundedQueue <int> queue     = new BoundedQueue <int>(CAPACITY);
            bool result = true;

            ThreadStart producer = () => {
                for (int i = 1; i <= MAX_VAL; ++i)
                {
                    queue.Put(i);
                }
            };

            ThreadStart consumer = () => {
                int pred = 0;
                try {
                    // read one more than is produced
                    for (int i = 1; i <= MAX_VAL + 1; ++i)
                    {
                        int val = queue.Get();
                        if (val != pred + 1)
                        {
                            result = false;
                            return;
                        }
                        pred = val;
                    }
                }
                catch (Exception) {
                    result = false;
                }
                finally {
                    concluded.Set();
                }
            };

            Thread tprod = new Thread(producer);

            tprod.Start();

            Thread tcons = new Thread(consumer);

            tcons.Start();

            bool waitRes = concluded.WaitOne(CONCLUDED_TIMEOUT);

            concluded.Close();

            Assert.AreEqual(true, result, "Error on Get!");
            Assert.AreEqual(false, waitRes, "Timeout must occurr!");
        }
예제 #27
0
        public void Enqueue3Dequeue1()
        {
            var queue = new BoundedQueue <int>(2);

            queue.Enqueue(2);
            queue.Enqueue(3);
            var t       = Task.Run(() => queue.Enqueue(4));
            var timeout = Task.WhenAny(t, Task.Delay(100));

            Assert.That(t.IsCompleted, Is.False, "Some useful error message");
        }
예제 #28
0
        public async Task TryEnqueueShouldNQ()
        {
            var queue = new BoundedQueue <int>(2);

            queue.Enqueue(1);
            var ok = queue.TryEnqueue(3, 500);

            Assert.That(ok, Is.True);
            Assert.That(queue.Dequeue(), Is.EqualTo(1));
            Assert.That(queue.Dequeue(), Is.EqualTo(3));
        }
예제 #29
0
        public void TestBoundedQueueConstructedWithEnumrable()
        {
            BoundedQueue <int> queue = new BoundedQueue <int>(Enumerable.Range(0, 10), 5);

            Assert.True(queue.Count == 5);
            int[] array = new int[10];
            queue.CopyTo(array, 1);
            Assert.True(array[0] == 0);
            for (int i = 0; i < 5; i++)
            {
                Assert.True(array[i + 1] == i);
            }
        }
예제 #30
0
        public void Setup()
        {
            boundedQueue   = new BoundedQueue(OPERATIONS);
            unboundedQueue = new UnboundedQueue <ISignal>();

            var msg = Activated.Instance;

            for (int i = 0; i < OPERATIONS; i++)
            {
                boundedQueue.TryPush(msg);
                unboundedQueue.TryPush(msg);
            }
        }
예제 #31
0
 public static void Open(int nofThreads)
 {
     Contract.Requires(nofThreads > 0);
     threads = new Thread[nofThreads];
     var i = 0;
     while (i != nofThreads)
     {
         threads[i] = new Thread(Run);
         threads[i].Priority = ThreadPriority.Lowest;
         i = i + 1;
     }
     workItemQueue = new BoundedQueue { Capacity = nofThreads };
 }
예제 #32
0
        public void TestBoundedQueueNoAbsorbExceedLimit()
        {
            Assert.Throws <InvalidOperationException>(
                () =>
            {
                BoundedQueue <int> queue = new BoundedQueue <int>(10);
                for (int i = 0; i < 10; i++)
                {
                    queue.Enqueue(i);
                }

                queue.Enqueue(11);
            });
        }
예제 #33
0
        public void Enqueue_FullQueue_RemovesFirstItem()
        {
            const int maxSize = 1;
            var       first   = new object();
            var       second  = new object();

            var target = new BoundedQueue <object>(maxSize);

            target.Enqueue(first);
            target.Enqueue(second);

            Assert.Equal(maxSize, target.Count);
            Assert.Equal(second, target.Single());
        }
예제 #34
0
        public void MaxDepth()
        {
            var queue = new BoundedQueue();
            queue.MaxDepth = 2;
            queue.Enqueue(delegate { });
            queue.Enqueue(delegate { });

            try
            {
                queue.Enqueue(delegate { });
                Assert.Fail("failed");
            }
            catch (QueueFullException failed)
            {
                Assert.AreEqual(2, failed.Depth);
                Assert.AreEqual("Attempted to enqueue item into full queue: 2", failed.Message);
            }
        }
예제 #35
0
 public void PointToPointPerfTestWithObject()
 {
     var executor = new BoundedQueue(new PerfExecutor()) { MaxDepth = 100000, MaxEnqueueWaitTimeInMs = 1000 };
     using (var fiber = new ThreadFiber(executor))
     {
         fiber.Start();
         var channel = new Channel<object>();
         const int max = 5000000;
         var reset = new AutoResetEvent(false);
         var end = new object();
         Action<object> onMessage = delegate(object message)
                                    {
                                        if (message == end)
                                        {
                                            reset.Set();
                                        }
                                    };
         channel.Subscribe(fiber, onMessage);
         using (new PerfTimer(max))
         {
             var message = new object();
             for (var i = 0; i <= max; i++)
             {
                 channel.Publish(message);
             }
             channel.Publish(end);
             Assert.IsTrue(reset.WaitOne(30000, false));
         }
     }
 }
예제 #36
0
 private static void RunBoundedQueue()
 {
     var executor = new BoundedQueue(new PerfExecutor()) { MaxDepth = 10000, MaxEnqueueWaitTimeInMs = 1000 };
     using (var fiber = new ThreadFiber(executor))
     {
         fiber.Start();
         var channel = new Channel<MessageStruct>();
         const int max = 5000000;
         var reset = new AutoResetEvent(false);
         Action<MessageStruct> onMessage = delegate(MessageStruct count)
         {
             if (count.count == max)
             {
                 reset.Set();
             }
         };
         channel.Subscribe(fiber, onMessage);
         using (new PerfTimer(max))
         {
             for (var i = 0; i <= max; i++)
             {
                 channel.Publish(new MessageStruct { count = i });
             }
             Assert.IsTrue(reset.WaitOne(30000, false));
         }
     }
 }