예제 #1
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");
        }
예제 #2
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());
        }
예제 #3
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);
            });
        }
예제 #4
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);
        }
예제 #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 void EnqueueTask(Action task)
 {
     if (!_taskQueue.Enqueue(task))
     {
         throw new ArgumentOutOfRangeException();
     }
 }
예제 #7
0
        public void IfEnqueueIsEqualDequeue()
        {
            var queue = new BoundedQueue <int>(2);

            queue.Enqueue(1);

            Assert.That(queue.Dequeue(), Is.EqualTo(1), "Some useful error message");
        }
예제 #8
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);
            }
        }
예제 #9
0
        public void TestBoundedQueue()
        {
            var q = new BoundedQueue();

            q.Stop();
            q.Enqueue(() => { });
            TestContext.WriteLine($"q.Count():{q.Count()}");
            Assert.AreEqual(0, q.Count());
            q.Run();
            q.Enqueue(() => { });
            TestContext.WriteLine($"q.Count():{q.Count()}");
            Assert.AreEqual(1, q.Count());
            var r = q.DequeueAll();

            TestContext.WriteLine($"q.Count():{q.Count()} r.Count():{r.Count}");
            Assert.AreEqual(0, q.Count());
            Assert.AreEqual(1, r.Count);
        }
예제 #10
0
        public async Task EnqueueAndDequeueMultiThreaded()
        {
            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));
            await timeout;

            Assert.That(t.IsCompleted, Is.False, "Some useful error message");

            queue.Dequeue();
            await t;

            Assert.That(t.IsCompleted, Is.True, "Some useful error message");
            Assert.That(queue.Count, Is.EqualTo(2), "Some useful error message");
        }
예제 #11
0
        //[TestProperty("ChessExpectedResult", "Deadlock")]
        public void NoConsumer()
        {
            BoundedQueue <int> queue = new BoundedQueue <int>(10);
            int i = 0;

            while (true)
            {
                queue.Enqueue(i++);
            }
        }
예제 #12
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);
        }
예제 #13
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);
        }
예제 #14
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);
            }
        }
예제 #15
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));
        }
예제 #16
0
 public void AddLiveMessage(OutstandingMessage ev)
 {
     if (Live)
     {
         if (_buffer.Count < _maxBufferSize)
         {
             _buffer.Enqueue(ev);
         }
         else
         {
             Live = false;
         }
     }
     _liveBuffer.Enqueue(ev);
 }
예제 #17
0
            /// <summary>
            /// Initializes a new instance of the <see cref="ResourceCurrentState"/> class.
            /// </summary>
            /// <param name="resourceMonitor">The resource monitor.</param>
            /// <param name="maxStatePerResource">The maximum state per resource.</param>
            /// <exception cref="ArgumentNullException"></exception>
            public ResourceCurrentState(IResourceMonitor resourceMonitor, int maxStatePerResource)
            {
                var boundedQueue = new BoundedQueue <ResourceMonitorEventArgs>(maxStatePerResource);

                if (resourceMonitor == null)
                {
                    throw new ArgumentNullException(nameof(resourceMonitor));
                }
                ResourceMonitor = resourceMonitor;
                _boundedQueue   = boundedQueue;

                _handler = (sender, @event) =>
                {
                    _boundedQueue.Enqueue(@event);
                };
            }
예제 #18
0
        public void Enqueue_EnumeratesItems()
        {
            var numbers = Enumerable.Range(1, 10).ToList();

            var maxSize = numbers.Count / 2;
            var target  = new BoundedQueue <int>(maxSize);

            for (int i = 0; i < numbers.Count; i++)
            {
                target.Enqueue(numbers[i]);

                // Exact number of enqueued or maxSize
                var expected = i + 1 <= maxSize ? i + 1 : maxSize;
                Assert.Equal(expected, target.Count);
            }
        }
예제 #19
0
        public void TryDeque()
        {
            var queue = new BoundedQueue <int>(2);

            queue.Enqueue(1);

            int retVal;
            var ok = queue.TryDequeue(100, out retVal);

            Assert.That(ok, Is.True);
            Assert.That(retVal, Is.EqualTo(1));

            ok = queue.TryDequeue(100, out retVal);

            Assert.That(ok, Is.False);
        }
예제 #20
0
        public void TestBoundedQueueNoAbsorb()
        {
            BoundedQueue <int> queue = new BoundedQueue <int>(10);

            for (int i = 0; i < 10; i++)
            {
                Assert.False(queue.IsFull);
                queue.Enqueue(i);
            }
            Assert.Equal(queue.Count, 10);
            Assert.True(queue.Contains(5));
            Assert.True(queue.IsFull);
            Assert.Equal(queue.Peek(), 0);
            Assert.Equal(queue.Dequeue(), 0);
            Assert.Equal(queue.Count, 9);
            Assert.False(queue.IsFull);
        }
        public void AddItem(ChatItem item, bool allowEdit = false)
        {
            var span = new Span();

            if (allowEdit)
            {
                span.MouseEnter += span_MouseEnter;
                span.MouseLeave += span_MouseLeave;
            }

            span.Tag = item;
            item.AddTo(span.Inlines);

            if (KeepHistory)
            {
                history.Enqueue(item);
            }

            Root.Inlines.Add(span);
            Root.Inlines.Add(new LineBreak());
            sentMessages.FindScrollViewer().ScrollToBottom();
        }
예제 #22
0
        public void BoundedQueueTest2()
        {
            BoundedQueue <int> queue = new BoundedQueue <int>(2);

            ParallelTasks ptasks = new ParallelTasks();

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

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

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

            ptasks.Execute();

            // check size: should be zero
            Assert.AreEqual(0, queue.Size(), "The final size of the queue.");
        }
예제 #23
0
        public async Task BigTest()
        {
            var list1 = new List <int> {
                1, 2, 3, 4, 5
            };
            var list2 = new List <int> {
                6, 7, 9, 10, 8
            };
            var expected = new List <int>(list1.Union(list2));

            expected.Sort();
            var queue = new BoundedQueue <int>(2);

            var results = new ConcurrentBag <int>();

            var t1 = new Task(() =>
            {
                for (int i = 0; i < 5; i++)
                {
                    queue.Enqueue(list1[i]);
                }
            });

            var t2 = new Task(() =>
            {
                for (int i = 0; i < 5; i++)
                {
                    queue.Enqueue(list2[i]);
                }
            });

            var t3 = new Task(() =>
            {
                while (results.Count < 10)
                {
                    results.Add(queue.Dequeue());
                }
            });

            var t4 = new Task(() =>
            {
                while (results.Count < 10)
                {
                    results.Add(queue.Dequeue());
                }
            });

            t1.Start();
            t2.Start();

            t3.Start();
            t4.Start();

            var t5 = Task.Delay(5000);
            await Task.WhenAny(t3, t4, t5);

            Assert.That(t5.IsCompleted, Is.EqualTo(false), "TimeOut");
            Assert.That(results.Count, Is.EqualTo(10), "Count is not 10");

            var sortedList = results.ToList();

            sortedList.Sort();

            Assert.That(expected.SequenceEqual(sortedList), "Sequence not equal");
        }
예제 #24
0
 public void Log <TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func <TState, Exception, string> formatter)
 {
     _boundedQueue.Enqueue($"{Name}: " + formatter(state, exception));
 }