Esempio n. 1
0
        public void ShouldNotAlterOrder()
        {
            var    blockingQueue = new SimpleBlockingQueue();
            object _res1         = null;
            object _res2         = null;
            object _res3         = null;

            var thread1 = new Thread(() =>
            {
                _res1 = blockingQueue.Get();
                _res2 = blockingQueue.Get();
                _res3 = blockingQueue.Get();
            });

            thread1.Start();

            var thread2 = new Thread(() =>
            {
                blockingQueue.Put(1);
                blockingQueue.Put(10f);
                blockingQueue.Put("hello");
            });

            thread2.Start();

            thread1.Join();
            thread2.Join();

            Assert.AreEqual(1, _res1);
            Assert.AreEqual(10f, _res2);
            Assert.AreEqual("hello", _res3);
        }
Esempio n. 2
0
        public void ShouldNotDeadlock()
        {
            SimpleBlockingQueue _bq = new SimpleBlockingQueue();

            const int THREAD_COUNT = 50;

            Thread[] putThreads     = new Thread[THREAD_COUNT];
            Thread[] getThreads     = new Thread[THREAD_COUNT + (THREAD_COUNT / 2)];
            Thread[] disableThreads = new Thread[THREAD_COUNT / 2];

            for (int i = 0; i < getThreads.Length; i += 1)
            {
                getThreads[i] = new Thread(() =>
                {
                    _bq.Get();
                    _bq.Get();
                });
                getThreads[i].Start();
            }

            for (int i = 0; i < putThreads.Length; i += 1)
            {
                var capture = i;
                putThreads[i] = new Thread(() =>
                {
                    _bq.Put(capture);
                    _bq.Put(capture + putThreads.Length);
                });
                putThreads[i].Start();
            }

            for (int i = 0; i < disableThreads.Length; i += 1)
            {
                disableThreads[i] = new Thread(() =>
                {
                    _bq.Disable();
                });
                disableThreads[i].Start();
            }

            for (int i = 0; i < putThreads.Length; i += 1)
            {
                putThreads[i].Join();
            }

            for (int i = 0; i < getThreads.Length; i += 1)
            {
                getThreads[i].Join();
            }

            for (int i = 0; i < disableThreads.Length; i += 1)
            {
                disableThreads[i].Join();
            }

            // if we get here then there was no deadlock
        }
Esempio n. 3
0
        public void ShouldReturnNullAfterDisabling()
        {
            SimpleBlockingQueue target = new SimpleBlockingQueue();
            int testValue = 1;

            target.Put(testValue);
            var testValueBackBeforeDisable = target.Get();

            Assert.AreEqual(testValue, testValueBackBeforeDisable);

            target.Put(testValue);
            target.Disable();
            var testValueBackAfterDisable = target.Get();

            Assert.AreEqual(null, testValueBackAfterDisable);
        }
Esempio n. 4
0
 public void ShouldPutAndGet()
 {
     SimpleBlockingQueue target = new SimpleBlockingQueue();
     {
         object testValue = 1;
         target.Put(testValue);
         var testValueBack = target.Get();
         Assert.AreEqual(testValue, testValueBack);
     }
     {
         object testValue = "test";
         target.Put(testValue);
         var testValueBack = target.Get();
         Assert.AreEqual(testValue, testValueBack);
     }
 }
Esempio n. 5
0
        public async Task Baseline()
        {
            var       sut      = new SimpleBlockingQueue();
            var       queue1   = new Queue <int>();
            var       queue2   = new Queue <int>();
            const int testSize = 1000000;

#pragma warning disable HAA0302 // Display class allocation to capture closure

#pragma warning restore HAA0302 // Display class allocation to capture closure

#pragma warning disable HAA0301 // Closure Allocation Source
            var writerTask  = new Task(Write);
            var readerTask1 = new Task(() => Read(1));
            var readerTask2 = new Task(() => Read(2));
#pragma warning restore HAA0301 // Closure Allocation Source
            readerTask1.Start();
            writerTask.Start();
            readerTask2.Start();

            void Write()
            {
                for (var i = 0; i < testSize; i++)
                {
                    using var cancellationTokenSource = new CancellationTokenSource();
                    cancellationTokenSource.CancelAfter(TimeSpan.FromSeconds(5));
                    sut.Enqueue(i, TimeSpan.FromSeconds(5), cancellationTokenSource.Token);
                }
            }

            void Read(int queueNumber)
            {
                var currentQueue = queueNumber == 1 ? queue1 : queue2;

                while (queue1.Count + queue2.Count != testSize)
                {
                    using var cancellationTokenSource = new CancellationTokenSource();
                    cancellationTokenSource.CancelAfter(TimeSpan.FromSeconds(5));
                    int toEnqueue;

                    try
                    {
                        toEnqueue = sut.Dequeue(TimeSpan.FromSeconds(5), cancellationTokenSource.Token);
                    }
                    catch (OperationCanceledException)
                    {
                        return;
                    }

                    currentQueue.Enqueue(toEnqueue);
                }
            }

#pragma warning disable HAA0101 // Array allocation for params parameter
            await writerTask.ConfigureAwait(false);

            await Task.WhenAll(readerTask1, readerTask2).ConfigureAwait(false);

#pragma warning restore HAA0101 // Array allocation for params parameter

            Assert.True(!queue1.Intersect(queue2).ToList().Any());

            Assert.True(queue1.Distinct().Count() == queue1.Count);

            while (queue1.Count > 0)
            {
                var smallerDequeued = queue1.TryDequeue(out var smaller);
                var biggerDequeued  = queue1.TryDequeue(out var bigger);

                if (smallerDequeued && biggerDequeued)
                {
                    Assert.True(smaller < bigger);
                }
            }

            Assert.True(queue2.Distinct().Count() == queue2.Count);

            while (queue2.Count > 0)
            {
                var smallerDequeued = queue2.TryDequeue(out var smaller);
                var biggerDequeued  = queue2.TryDequeue(out var bigger);

                if (smallerDequeued && biggerDequeued)
                {
                    Assert.True(smaller < bigger);
                }
            }
        }
Esempio n. 6
0
 public void ShouldWaitOnEmptyQueue_TestMustFail()
 {
     SimpleBlockingQueue target = new SimpleBlockingQueue();
     var testValueBack          = target.Get();
 }