public void TestRemove()
        {
            TickedQueue queue = new TickedQueue();
            TickedObject a = new TickedObject(Callback3, 0);
            a.TickLength = 1;

            queue.Add(a);

            queue.Update(DateTime.UtcNow.AddSeconds(2));
            Assert.AreEqual(1, test3, "Callback should have been called for added item");

            queue.Remove(a);
            test3 = -1;

            queue.Update(DateTime.UtcNow.AddSeconds(4));
            Assert.AreEqual(-1, test3, "Callback should not have been called for removed item");
        }
        public void TestEnumerator()
        {
            var queue = new TickedQueue();
            var a = new TickedObject(Callback3, 0);
            a.TickLength = 1;

            queue.Add(a);

            Assert.IsTrue(queue.Items.Any(), "There should be items on the queue");
            Assert.IsTrue(queue.Items.Contains(a), "Queue should contain the new item");
            Assert.AreEqual(1, queue.Items.Count(), "Queue should contain only one item");

            var b = new TickedObject(Callback3, 0);
            queue.Add(b);

            Assert.IsTrue(queue.Items.Contains(b), "Queue should contain the second item");
            Assert.AreEqual(2, queue.Items.Count(), "Queue should contain two items");

            queue.Remove(a);

            Assert.AreEqual(1, queue.Items.Count(), "Queue should contain only one item again");
            Assert.IsFalse(queue.Items.Contains(a), "Queue should not contain the original item");
        }
        public void TestInvalidRemove()
        {
            var queue = new TickedQueue();
            var a = new TickedObject(Callback3, 1);
            queue.Add(a);

            var b = new TickedObject(Callback3);

            var result = queue.Remove(b);

            Assert.IsFalse(result, "Call to remove the B should have returned false");
        }
        public void TestRemoveWhileEnqueued()
        {
            var queue = new TickedQueue();
            queue.MaxProcessedPerUpdate = 1;

            var aVal = 0;
            var a = new TickedObject((x => aVal++), 0);

            var bVal = 0;
            var b = new TickedObject((x => bVal++), 0);

            var cVal = 0;
            var c = new TickedObject((x => cVal++), 0);

            queue.Add(a, true);
            queue.Add(b, true);
            queue.Add(c, true);

            // Verify the queue works as expected
            queue.Update(DateTime.UtcNow.AddSeconds(0.5f));
            Assert.AreEqual(1, aVal, "Invalid aVal after the first update");
            Assert.AreEqual(0, bVal, "Invalid bVal after the first update");
            Assert.AreEqual(0, cVal, "Invalid cVal after the first update");

            Assert.IsTrue(queue.Remove(b), "Error removing B");

            queue.Update(DateTime.UtcNow.AddSeconds(1f));
            Assert.AreEqual(1, aVal, "Invalid aVal after the second update");
            Assert.AreEqual(0, bVal, "B should not have been ticked after being removed");
            Assert.AreEqual(1, cVal, "Invalid cVal after the second update");

            queue.Update(DateTime.UtcNow.AddSeconds(1.5f));
            Assert.AreEqual(2, aVal, "Invalid aVal after the third update");
            Assert.AreEqual(0, bVal, "B should not have been ticked after being removed");
            Assert.AreEqual(1, cVal, "Invalid cVal after the third update");
        }
        public void TestRemoveFromHandlerWhileInWorkQueue()
        {
            var queue = new TickedQueue();
            queue.MaxProcessedPerUpdate = 10;

            var aVal = 0;
            var a = new TickedObject((x => aVal++), 0);

            var bVal = 0;
            var b = new TickedObject((x => bVal++), 0);

            var cVal = 0;
            var c = new TickedObject((x =>
            {
                cVal += 2;
                queue.Remove(b);
            }), 0);

            queue.Add(a, true);
            queue.Add(c, true); // c will execute before B and remove it
            queue.Add(b, true);

            // Verify the queue works as expected
            queue.Update(DateTime.UtcNow.AddSeconds(0.5f));
            Assert.AreEqual(1, aVal, "Invalid aVal after the first update");
            Assert.AreEqual(2, cVal, "Invalid cVal after the first update");
            Assert.AreEqual(0, bVal, "b should not have executed");
            Assert.IsFalse(queue.Items.Contains(b), "b should not be on the queue");

            queue.Update(DateTime.UtcNow.AddSeconds(1f));
            Assert.AreEqual(2, aVal, "Invalid aVal after the second update");
            Assert.AreEqual(4, cVal, "Invalid cVal after the second update");
            Assert.AreEqual(0, bVal, "b should not have executed");
            Assert.IsFalse(queue.Items.Contains(b), "b should still not be on the queue");
        }
        public void TestRemoveFromHandlerAfterPassingInWorkQueue()
        {
            var queue = new TickedQueue {MaxProcessedPerUpdate = 10};

            var aVal = 0;
            var a = new TickedObject((x => aVal++));

            var bVal = 0;
            var b = new TickedObject((x => bVal++));

            var cVal = 0;
            var c = new TickedObject((x =>
            {
                cVal += 2;
                queue.Remove(b);
            }));

            var time = DateTime.UtcNow;
            queue.Add(a, time, true);
            queue.Add(b, time, true);
            queue.Add(c, time, true); // c will remove b, so it should execute only once

            // Verify the queue works as expected
            queue.Update(DateTime.UtcNow.AddSeconds(0.5f));
            Assert.AreEqual(1, aVal, "Invalid aVal after the first update");
            Assert.AreEqual(2, cVal, "Invalid cVal after the first update");
            Assert.AreEqual(1, bVal, "b should have executed the first time");
            Assert.IsFalse(queue.Items.Contains(b), "b should no longer be on the queue");

            queue.Update(DateTime.UtcNow.AddSeconds(1f));
            Assert.AreEqual(2, aVal, "Invalid aVal after the second update");
            Assert.AreEqual(4, cVal, "Invalid cVal after the second update");
            Assert.AreEqual(1, bVal, "b should not have executed again");
            Assert.IsFalse(queue.Items.Contains(b), "b should still not be on the queue");
        }
        public void TestRemove()
        {
            var queue = new TickedQueue();
            var a = new TickedObject(Callback3, 0);
            a.TickLength = 1;

            queue.Add(a);

            queue.Update(DateTime.UtcNow.AddSeconds(2));
            Assert.AreEqual(1, test3, "Callback should have been called for added item");

            var result = queue.Remove(a);
            test3 = -1;

            Assert.IsTrue(result, "Call to remove the item should have returned true");

            queue.Update(DateTime.UtcNow.AddSeconds(4));
            Assert.AreEqual(-1, test3, "Callback should not have been called for removed item");
        }