Ejemplo n.º 1
0
        /// <summary>
        /// Add the specified ticked object to the queue, using currentTime as the time to use for the tick check.
        /// </summary>
        /// <param name='ticked'>
        /// The ITicked object.
        /// </param>
        /// <param name='currentTime'>
        /// Current time. Doesn't have to be the real time.
        /// </param>
        /// <param name='looped'>
        /// Sets whether the ticked item will be called once, or looped.
        /// </param>
        public void Add(ITicked ticked, DateTime currentTime, bool looped)
        {
            TickedQueueItem item = new TickedQueueItem(ticked, currentTime);

            item.Loop = looped;
            Add(item, currentTime);
        }
		public void TestTickTimeReset()
		{
			TickedObject obj = new TickedObject(null);
			obj.Priority = 6;
			obj.TickLength = 7;
			var now = DateTime.UtcNow;
			TickedQueueItem item = new TickedQueueItem(obj, now);
			Assert.AreEqual(item.NextTickTime, now.AddSeconds(obj.TickLength), "Initial next tick time did not match");
			
			var future = now.AddSeconds(3);
			item.ResetTickFromTime(future);
			Assert.AreEqual(item.NextTickTime, future.AddSeconds(obj.TickLength), "Next tick time did not match after reset");
		}
        public void TestTimeCompare()
        {
            var a = new TickedObject(Callback, 2, 0) {Priority = 2};
            var b = new TickedObject(Callback, 1, 1) {Priority = 2};

            var itemA = new TickedQueueItem(a, DateTime.UtcNow);
            var itemB = new TickedQueueItem(b, DateTime.UtcNow.AddSeconds(2));

            var comparer = new TickedQueueItemComparer();
            Assert.AreEqual(-1, comparer.Compare(itemA, itemB), "A should be lower due to earlier tick time");
            b.Priority = 1;
            itemB = new TickedQueueItem(b);
            Assert.AreEqual(1, comparer.Compare(itemA, itemB), "B should be sorted lower due to the priority");
        }
        public void TestTickTimeCompare()
        {
            var a = new TickedObject(Callback, 2, 0) {Priority = 2};
            var b = new TickedObject(Callback, 1, 1) {Priority = 2};

            var itemA = new TickedQueueItem(a);
            var itemB = new TickedQueueItem(b);

            var comparer = new TickedQueueItemComparer();
            Assert.AreEqual(1, comparer.Compare(itemA, itemB), "B should be lower due to lower tick time");
            a.Priority = 1;
            itemA = new TickedQueueItem(a);
            Assert.AreEqual(-1, comparer.Compare(itemA, itemB), "A should be sorted lower due to the priority");
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Add the specified item and currentTime.
        /// </summary>
        /// <param name='item'>
        /// The TickedQueueItem element to add to the list.
        /// </param>
        /// <param name='currentTime'>
        /// Current time. Doesn't have to be the real time.
        /// </param>
        /// <remarks>
        /// Notice that unlike the two public methods that receive an ITicked,
        /// this one expects a TickedQueueItem.  It was done to avoid having to
        /// discard a TickedQueueItem instance every time that a looped item is
        /// ticked and re-added to the queue.  As such, it expects to already
        /// have been configured for if to loop or not.
        /// </remarks>
        private void Add(TickedQueueItem item, DateTime currentTime)
        {
            item.ResetTickFromTime(currentTime);
            int index = _queue.BinarySearch(item, new TickedQueueItemComparer());

            //if the binary search doesn't find something identical, it'll return a
            //negative value signifying where the new item should reside, so bitflipping
            //that gives the new index
            if (index < 0)
            {
                index = ~index;
            }
            _queue.Insert(index, item);
        }
Ejemplo n.º 6
0
        public void TestTickTimeReset()
        {
            TickedObject obj = new TickedObject(null);

            obj.Priority   = 6;
            obj.TickLength = 7;
            var             now  = DateTime.UtcNow;
            TickedQueueItem item = new TickedQueueItem(obj, now);

            Assert.AreEqual(item.NextTickTime, now.AddSeconds(obj.TickLength), "Initial next tick time did not match");

            var future = now.AddSeconds(3);

            item.ResetTickFromTime(future);
            Assert.AreEqual(item.NextTickTime, future.AddSeconds(obj.TickLength), "Next tick time did not match after reset");
        }
        public void TestEqualTimeCompare()
        {
            var a = new TickedObject(Callback);
            var b = new TickedObject(Callback);

            var time = DateTime.UtcNow;
            //forcing the time used so that there isn't any difference in the two calls
            var itemA = new TickedQueueItem(a, time);
            var itemB = new TickedQueueItem(b, time);

            // For items with equal times and priorities, we will state that the
            // first item is always lower than the second, so that the second is
            // always placed later in the queue
            var comparer = new TickedQueueItemComparer();
            Assert.AreEqual(-1, comparer.Compare(itemA, itemB), "Comparison should yield lower for a");            
        }
		public void TestCase ()
		{
			TickedObject obj = new TickedObject(null);
			obj.Priority = 6;
			obj.TickLength = 7;
			TickedQueueItem item = new TickedQueueItem(obj);
			Assert.AreEqual(item.Priority, obj.Priority, "TickedQueueItem should report the same priority as the wrapped object");
			
			DateTime testTime = DateTime.UtcNow;
			Assert.IsFalse(item.CheckTickReady(testTime), "CheckTickReady should return false when time is before next scheduled tick");
			
			DateTime testTimePlus4 = testTime.AddSeconds(4);
			Assert.IsFalse(item.CheckTickReady(testTimePlus4), "CheckTickReady should return false when time is before next scheduled tick");
			
			DateTime testTimePlus8 = testTime.AddSeconds(8);
			Assert.IsTrue(item.CheckTickReady(testTimePlus8), "CheckTickReady should return true when time is after next scheduled tick");
		}
        public void TestTimeCompare()
        {
            TickedObject a = new TickedObject(Callback, 0);
            a.Priority = 2;
            a.TickLength = 2;
            TickedObject b = new TickedObject(Callback, 1);
            b.Priority = 2;
            b.TickLength = 1;

            TickedQueueItem itemA = new TickedQueueItem(a, DateTime.UtcNow);
            TickedQueueItem itemB = new TickedQueueItem(b, DateTime.UtcNow.AddSeconds(2));

            TickedQueueItemComparer comparer = new TickedQueueItemComparer();
            Assert.AreEqual(-1, comparer.Compare(itemA, itemB), "A should be lower due to earlier tick time");
            b.Priority = 1;
            itemB = new TickedQueueItem(b);
            Assert.AreEqual(1, comparer.Compare(itemA, itemB), "B should be sorted lower due to the priority");
        }
        public void TestPriorityCompare()
        {
            TickedObject a = new TickedObject(Callback, 0);
            a.Priority = 5;
            TickedObject b = new TickedObject(Callback, 1);
            b.Priority = 2;

            DateTime time = DateTime.UtcNow;
            //forcing the time used so that there isn't any difference in the two calls
            TickedQueueItem itemA = new TickedQueueItem(a, time);
            TickedQueueItem itemB = new TickedQueueItem(b, time);

            TickedQueueItemComparer comparer = new TickedQueueItemComparer();
            Assert.AreEqual(1, comparer.Compare(itemA, itemB), "Comparison should yield lower for a");
            Assert.AreEqual(-1, comparer.Compare(itemB, itemA), "Reverse comparison should yield lower for b");
            a.Priority = 2;
            itemA = new TickedQueueItem(a, time);
            Assert.AreEqual(0, comparer.Compare(itemA, itemB), "Identical priorities should give equal comparison - {0}  {1}", itemA);
        }
Ejemplo n.º 11
0
        public void TestCase()
        {
            TickedObject obj = new TickedObject(null);

            obj.Priority   = 6;
            obj.TickLength = 7;
            TickedQueueItem item = new TickedQueueItem(obj);

            Assert.AreEqual(item.Priority, obj.Priority, "TickedQueueItem should report the same priority as the wrapped object");

            DateTime testTime = DateTime.UtcNow;

            Assert.IsFalse(item.CheckTickReady(testTime), "CheckTickReady should return false when time is before next scheduled tick");

            DateTime testTimePlus4 = testTime.AddSeconds(4);

            Assert.IsFalse(item.CheckTickReady(testTimePlus4), "CheckTickReady should return false when time is before next scheduled tick");

            DateTime testTimePlus8 = testTime.AddSeconds(8);

            Assert.IsTrue(item.CheckTickReady(testTimePlus8), "CheckTickReady should return true when time is after next scheduled tick");
        }
        public void TestPriorityCompare()
        {
            var a = new TickedObject(Callback, 0, 0) {Priority = 5};
            var b = new TickedObject(Callback, 0, 1) {Priority = 2};

            var time = DateTime.UtcNow;
            //forcing the time used so that there isn't any difference in the two calls
            var itemA = new TickedQueueItem(a, time);
            var itemB = new TickedQueueItem(b, time);

            var comparer = new TickedQueueItemComparer();
            Assert.AreEqual(1, comparer.Compare(itemA, itemB), "Comparison should yield lower for a");
            Assert.AreEqual(-1, comparer.Compare(itemB, itemA), "Reverse comparison should yield lower for b");
            a.Priority = b.Priority;

            // For items with equal times and priorities, we will state that the
            // first item is always lower than the second, so that the second is
            // always placed later in the queue
            itemA = new TickedQueueItem(a, time);
            Assert.AreEqual(-1, comparer.Compare(itemA, itemB),
                "Expected ItemA would be deemed lower - {0}  {1}", itemA, itemB);
        }
Ejemplo n.º 13
0
		/// <summary>
		/// Add the specified item and currentTime.
		/// </summary>
		/// <param name='item'>
		/// The TickedQueueItem element to add to the list.
		/// </param>
		/// <param name='currentTime'>
		/// Current time. Doesn't have to be the real time.
		/// </param>
		/// <remarks>
		/// Notice that unlike the two public methods that receive an ITicked, 
		/// this one expects a TickedQueueItem.  It was done to avoid having to
		/// discard a TickedQueueItem instance every time that a looped item is
		/// ticked and re-added to the queue.  As such, it expects to already 
		/// have been configured for if to loop or not.
		/// </remarks>
		private void Add(TickedQueueItem item, DateTime currentTime)
		{
			item.ResetTickFromTime(currentTime);
			int index = _queue.BinarySearch(item, new TickedQueueItemComparer());
			
			//if the binary search doesn't find something identical, it'll return a
			//negative value signifying where the new item should reside, so bitflipping
			//that gives the new index
			if (index < 0) index = ~index;
			_queue.Insert(index, item);
		}
Ejemplo n.º 14
0
		/// <summary>
		/// Add the specified ticked object to the queue, using currentTime as the time to use for the tick check.
		/// </summary>
		/// <param name='ticked'>
		/// The ITicked object.
		/// </param>
		/// <param name='currentTime'>
		/// Current time. Doesn't have to be the real time.
		/// </param>
		/// <param name='looped'>
		/// Sets whether the ticked item will be called once, or looped.
		/// </param>
		public void Add(ITicked ticked, DateTime currentTime, bool looped)
		{
			var item = new TickedQueueItem(ticked, currentTime, looped);
			Add(item, currentTime);
		}
Ejemplo n.º 15
0
        /// <summary>
        /// Add the specified ticked object to the queue, using currentTime as the time to use for the tick check.
        /// </summary>
        /// <param name='ticked'>
        /// The ITicked object.
        /// </param>
        /// <param name='currentTime'>
        /// Current time. Doesn't have to be the real time.
        /// </param>
        /// <param name='looped'>
        /// Sets whether the ticked item will be called once, or looped.
        /// </param>
        public void Add(ITicked ticked, DateTime currentTime, bool looped)
        {
            var item = new TickedQueueItem(ticked, currentTime, looped);

            Add(item, currentTime);
        }
Ejemplo n.º 16
0
 /// <summary>
 /// Add the specified item and currentTime.
 /// </summary>
 /// <param name='item'>
 /// The TickedQueueItem element to add to the list.
 /// </param>
 /// <param name='currentTime'>
 /// Current time. Doesn't have to be the real time.
 /// </param>
 /// <remarks>
 /// Notice that unlike the two public methods that receive an ITicked, 
 /// this one expects a TickedQueueItem.  It was done to avoid having to
 /// discard a TickedQueueItem instance every time that a looped item is
 /// ticked and re-added to the queue.  As such, it expects to already 
 /// have been configured for if to loop or not.
 /// </remarks>
 private void Add(TickedQueueItem item, DateTime currentTime)
 {
     item.ResetTickFromTime(currentTime);
     _queue.Add(item);
 }