public void TestOrder()
        {
            const int numberOfElements = 10000;
            var       rnd = new System.Random();

            var queue = new ConcurrentTokenizedPriorityQueue <int, int, int>(1, x => x + 1);

            for (int i = 0; i < numberOfElements; ++i)
            {
                queue.Enqueue(100 + rnd.Next(numberOfElements), 23, out var token);
            }

            Assert.AreEqual(numberOfElements, queue.Count);

            int previous = int.MinValue;

            for (int i = 0; i < numberOfElements; ++i)
            {
                int curr = queue.Dequeue().Key;
                Assert.GreaterOrEqual(curr, previous);
                previous = curr;
            }

            Assert.AreEqual(0, queue.Count);
        }
Exemple #2
0
 /// <summary>
 /// Initializes a new instance of the <see cref="TimerQueue"/> class.
 /// </summary>
 /// <param name="clock">The underlying high resolution clock.</param>
 /// <exception cref="System.ArgumentNullException">Argument clock is null</exception>
 public TimerQueue(IHighResolutionClock clock)
 {
     _clock = clock ?? throw new ArgumentNullException("clock");
     _event = new AutoResetEvent(false);
     _items = new ConcurrentTokenizedPriorityQueue <TimeSpan, Action <object, TimeSpan>, object>();
     _task  = Task.Factory.StartNew(Run, TaskCreationOptions.LongRunning);
 }
Exemple #3
0
		/// <summary>
		/// Initializes a new instance of the <see cref="TimerQueue"/> class.
		/// </summary>
		/// <param name="clock">The underlying high resolution clock.</param>
		/// <exception cref="System.ArgumentNullException">Argument clock is null</exception>
		public TimerQueue(IHighResolutionClock clock)
		{
			if (null == clock)
				throw new ArgumentNullException("clock");

			_clock = clock;
			_event = new AutoResetEvent(false);
			_items = new ConcurrentTokenizedPriorityQueue<TimeSpan, Action<object, TimeSpan>, object>();
			_task = Task.Factory.StartNew(Run, TaskCreationOptions.LongRunning);
		}
		public void TestOrder()
		{
			const int numberOfElements = 10000;
			var rnd = new System.Random();

			var queue = new ConcurrentTokenizedPriorityQueue<int, int, int>(1, x => x + 1);
			int token;

			for (int i = 0; i < numberOfElements; ++i)
				queue.Enqueue(100 + rnd.Next(numberOfElements), 23, out token);

			Assert.AreEqual(numberOfElements, queue.Count);

			int previous = int.MinValue;
			for (int i = 0; i < numberOfElements; ++i)
			{
				int curr = queue.Dequeue().Key;
				Assert.GreaterOrEqual(curr, previous);
				previous = curr;
			}

			Assert.AreEqual(0, queue.Count);
		}
		public void Test002_Concurrency()
		{
			const int numberOfItems = 100000;

			var sourceItems = GetRandomListOfDistinctIntegers(numberOfItems);
			var sourceItemsSorted = new List<int>(sourceItems);
			sourceItemsSorted.Sort();

			var destinationItems = new ConcurrentBag<int>();

			var queueToTest = new ConcurrentTokenizedPriorityQueue<int, int, int>(1, x => x + 1);

			// 2 Enqueue tasks and 2 Dequeue tasks
			Parallel.Invoke(new Action[]
			{
				// Enqueue
 				() =>
				{
					int item, token;
					while (sourceItems.TryDequeue(out item))
						queueToTest.Enqueue(item, item + 13, out token);
				},
				// Enqueue
				() =>
				{
					int item, token;
					while (sourceItems.TryDequeue(out item))
						queueToTest.Enqueue(item, item + 13, out token);
				},
				// Dequeue
				()=>
					{
						int key, value, toke;
						int counter = 0;
						while(counter<100)
						{
						while(queueToTest.TryDequeue(out key, out value, out toke))
						{
							Assert.AreEqual(key + 13, value);
							destinationItems.Add(key);
							counter = 0;
						}
						System.Threading.Thread.Sleep(10);
						++counter;
						}
					},
				// Dequeue
					()=>
					{
						int key, value, toke;
						int counter = 0;
						while(counter<100)
						{
						while(queueToTest.TryDequeue(out key, out value, out toke))
						{
							Assert.AreEqual(key + 13, value);
							destinationItems.Add(key);
							counter = 0;
						}
						System.Threading.Thread.Sleep(10);
						++counter;
						}
					}
			});

			Assert.AreEqual(0, sourceItems.Count, "All source items should be moved into the queueToTest and then to the destinationItems collection");
			Assert.AreEqual(0, queueToTest.Count, "Queue should be empty, all items should be moved into the destinationItems collection");
			Assert.AreEqual(numberOfItems, destinationItems.Count);
			var list = new List<int>(destinationItems);

			list.Sort();

			for (int i = 0; i < numberOfItems; ++i)
			{
				Assert.AreEqual(sourceItemsSorted[i], list[i]);
			}
		}
        public void Test002_Concurrency()
        {
            const int numberOfItems = 100000;

            var sourceItems       = GetRandomListOfDistinctIntegers(numberOfItems);
            var sourceItemsSorted = new List <int>(sourceItems);

            sourceItemsSorted.Sort();

            var destinationItems = new ConcurrentBag <int>();

            var queueToTest = new ConcurrentTokenizedPriorityQueue <int, int, int>(1, x => x + 1);

            // 2 Enqueue tasks and 2 Dequeue tasks
            Parallel.Invoke(new Action[]
            {
                // Enqueue
                () =>
                {
                    int item, token;
                    while (sourceItems.TryDequeue(out item))
                    {
                        queueToTest.Enqueue(item, item + 13, out token);
                    }
                },
                // Enqueue
                () =>
                {
                    int item, token;
                    while (sourceItems.TryDequeue(out item))
                    {
                        queueToTest.Enqueue(item, item + 13, out token);
                    }
                },
                // Dequeue
                () =>
                {
                    int counter = 0;
                    while (counter < 100)
                    {
                        while (queueToTest.TryDequeue(out var key, out var value, out var toke))
                        {
                            Assert.AreEqual(key + 13, value);
                            destinationItems.Add(key);
                            counter = 0;
                        }
                        System.Threading.Thread.Sleep(10);
                        ++counter;
                    }
                },
                // Dequeue
                () =>
                {
                    int counter = 0;
                    while (counter < 100)
                    {
                        while (queueToTest.TryDequeue(out var key, out var value, out var toke))
                        {
                            Assert.AreEqual(key + 13, value);
                            destinationItems.Add(key);
                            counter = 0;
                        }
                        System.Threading.Thread.Sleep(10);
                        ++counter;
                    }
                }
            });