diff --git a/ICD.Common.Utils.Tests/Collections/RateLimitedEventQueueTest.cs b/ICD.Common.Utils.Tests/Collections/RateLimitedEventQueueTest.cs deleted file mode 100644 index 62c20b2..0000000 --- a/ICD.Common.Utils.Tests/Collections/RateLimitedEventQueueTest.cs +++ /dev/null @@ -1,100 +0,0 @@ -using System.Collections.Generic; -using System.Linq; -using ICD.Common.Utils.Collections; -using ICD.Common.Utils.EventArguments; -using NUnit.Framework; - -namespace ICD.Common.Utils.Tests.Collections -{ - [TestFixture] - public sealed class RateLimitedEventQueueTest - { - [Test] - public void ItemDequeuedFeedbackTest() - { - List> callbacks = new List>(); - - using (RateLimitedEventQueue queue = new RateLimitedEventQueue { BetweenMilliseconds = 1000 }) - { - queue.OnItemDequeued += (sender, args) => callbacks.Add(args); - - queue.Enqueue(10); - queue.Enqueue(20); - queue.Enqueue(30); - - ThreadingUtils.Sleep(100); - - Assert.AreEqual(1, callbacks.Count, "Initial enqueue did not trigger a dequeue"); - - queue.OnItemDequeued += (sender, args) => { ThreadingUtils.Sleep(1000); }; - ThreadingUtils.Sleep(1000); - - Assert.AreEqual(2, callbacks.Count, "Second enqueue did not dequeue"); - - ThreadingUtils.Sleep(1000); - - Assert.AreEqual(2, callbacks.Count, "Third enqueue did not wait for process to complete"); - - ThreadingUtils.Sleep(1000); - - Assert.AreEqual(3, callbacks.Count); - } - } - - #region Properties - - [TestCase(1000)] - public void BetweenMillisecondsTest(long milliseconds) - { - using (RateLimitedEventQueue queue = new RateLimitedEventQueue { BetweenMilliseconds = milliseconds }) - Assert.AreEqual(milliseconds, queue.BetweenMilliseconds); - } - - [Test] - public void CountTest() - { - using (RateLimitedEventQueue queue = new RateLimitedEventQueue { BetweenMilliseconds = 100 * 1000 }) - { - queue.Enqueue(1); - queue.Enqueue(1); - queue.Enqueue(1); - - Assert.AreEqual(3, queue.Count); - } - } - - #endregion - - #region Methods - - [Test] - public void EnqueueTest() - { - using (RateLimitedEventQueue queue = new RateLimitedEventQueue { BetweenMilliseconds = 100 * 1000 }) - { - queue.Enqueue(10); - queue.Enqueue(20); - queue.Enqueue(30); - - Assert.True(queue.SequenceEqual(new[] { 10, 20, 30 })); - } - } - - [Test] - public void ClearTest() - { - using (RateLimitedEventQueue queue = new RateLimitedEventQueue { BetweenMilliseconds = 100 * 1000 }) - { - queue.Enqueue(1); - queue.Enqueue(1); - queue.Enqueue(1); - - queue.Clear(); - - Assert.AreEqual(0, queue.Count); - } - } - - #endregion - } -} diff --git a/ICD.Common.Utils/Collections/RateLimitedEventQueue.cs b/ICD.Common.Utils/Collections/RateLimitedEventQueue.cs deleted file mode 100644 index 15f7976..0000000 --- a/ICD.Common.Utils/Collections/RateLimitedEventQueue.cs +++ /dev/null @@ -1,204 +0,0 @@ -using System; -using System.Collections; -using System.Collections.Generic; -using ICD.Common.Properties; -using ICD.Common.Utils.EventArguments; -using ICD.Common.Utils.Extensions; -using ICD.Common.Utils.Timers; -#if !SIMPLSHARP -using System.Diagnostics; -#endif - -namespace ICD.Common.Utils.Collections -{ - /// - /// RateLimitedEventQueue provides features for enqueing items to be raised via an event at a controlled interval. - /// -#if !SIMPLSHARP - [DebuggerDisplay("Count = {Count}")] -#endif - public sealed class RateLimitedEventQueue : IEnumerable, ICollection, IDisposable - { - /// - /// Raised to handle to the next item in the queue. - /// - public event EventHandler> OnItemDequeued; - - private readonly SafeTimer m_DequeueTimer; - private readonly Queue m_Queue; - private readonly SafeCriticalSection m_QueueSection; - - #region Properties - - /// - /// Gets/sets the time between dequeues in milliseconds. - /// - public long BetweenMilliseconds { get; set; } - - public int Count { get { return m_QueueSection.Execute(() => m_Queue.Count); } } - - bool ICollection.IsSynchronized { get { return true; } } - - [NotNull] - object ICollection.SyncRoot { get { return this; } } - - #endregion - - /// - /// Constructor. - /// - public RateLimitedEventQueue() - { - m_Queue = new Queue(); - m_QueueSection = new SafeCriticalSection(); - - m_DequeueTimer = SafeTimer.Stopped(DequeueTimerCallback); - } - - #region Methods - - /// - /// Release resources. - /// - public void Dispose() - { - OnItemDequeued = null; - - m_QueueSection.Enter(); - - try - { - m_DequeueTimer.Dispose(); - } - finally - { - m_QueueSection.Leave(); - } - } - - /// - /// Enqueues the given item. - /// - /// - public void Enqueue([CanBeNull] T item) - { - m_QueueSection.Enter(); - - try - { - m_Queue.Enqueue(item); - - if (m_Queue.Count == 1) - SendNext(); - } - finally - { - m_QueueSection.Leave(); - } - } - - /// - /// Clears the queued items. - /// - public void Clear() - { - m_QueueSection.Enter(); - - try - { - m_DequeueTimer.Stop(); - m_Queue.Clear(); - } - finally - { - m_QueueSection.Leave(); - } - } - - #endregion - - #region Private Methods - - /// - /// Sends the next pulse in the queue. - /// - private void SendNext() - { - if (!m_QueueSection.TryEnter()) - return; - - try - { - if (m_Queue.Count == 0) - return; - - T item = m_Queue.Peek(); - - OnItemDequeued.Raise(this, new GenericEventArgs(item)); - - m_DequeueTimer.Reset(BetweenMilliseconds); - } - finally - { - m_QueueSection.Leave(); - } - } - - /// - /// Called when the dequeue timer elapses. - /// - private void DequeueTimerCallback() - { - m_QueueSection.Enter(); - - try - { - m_Queue.Dequeue(); - SendNext(); - } - finally - { - m_QueueSection.Leave(); - } - } - - #endregion - - #region IEnumerable/ICollection - - [NotNull] - public IEnumerator GetEnumerator() - { - return m_QueueSection.Execute(() => m_Queue.ToList(m_Queue.Count).GetEnumerator()); - } - - [NotNull] - IEnumerator IEnumerable.GetEnumerator() - { - return GetEnumerator(); - } - - void ICollection.CopyTo([NotNull] Array array, int index) - { - if (array == null) - throw new ArgumentNullException("array"); - - m_QueueSection.Enter(); - - try - { - foreach (T item in this) - { - array.SetValue(item, index); - index++; - } - } - finally - { - m_QueueSection.Leave(); - } - } - - #endregion - } -} diff --git a/ICD.Common.Utils/ICD.Common.Utils_SimplSharp.csproj b/ICD.Common.Utils/ICD.Common.Utils_SimplSharp.csproj index 75c17bb..b7d113c 100644 --- a/ICD.Common.Utils/ICD.Common.Utils_SimplSharp.csproj +++ b/ICD.Common.Utils/ICD.Common.Utils_SimplSharp.csproj @@ -84,7 +84,6 @@ -