• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

f2calv / CasCap.Common / 22972729257

11 Mar 2026 08:22PM UTC coverage: 3.947% (-0.1%) from 4.045%
22972729257

push

github

web-flow
Merge pull request #248 from f2calv/f2calv/2026-03-updates

F2calv/2026 03 updates

4 of 342 branches covered (1.17%)

Branch coverage included in aggregate %.

0 of 29 new or added lines in 5 files covered. (0.0%)

24 existing lines in 3 files now uncovered.

32 of 570 relevant lines covered (5.61%)

0.37 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/CasCap.Common.Extensions/Models/FixedSizeQueue.cs
1
namespace CasCap.Models;
2

3
/// <summary>
4
/// A fixed-size queue that automatically dequeues the oldest items when the limit is reached.
5
/// </summary>
6
/// <remarks>See <see href="https://stackoverflow.com/questions/5852863/fixed-size-queue-which-automatically-dequeues-old-values-upon-new-enques"/>.</remarks>
7
/// <typeparam name="T">The type of elements in the queue.</typeparam>
8
[Serializable]
9
[DebuggerDisplay("Count = {" + nameof(Count) + "}, Limit = {" + nameof(Limit) + "}")]
10
public class FixedSizedQueue<T> : IReadOnlyCollection<T>
11
{
12
    private readonly Queue<T> _queue = new();
×
13
    private readonly object _lock = new();
×
14

15
    /// <inheritdoc/>
16
    public int Count { get { lock (_lock) { return _queue.Count; } } }
×
17

18
    /// <summary>
19
    /// The maximum number of items the queue can hold.
20
    /// </summary>
UNCOV
21
    public int Limit { get; }
×
22

23
    /// <summary>
24
    /// Initializes a new instance of the <see cref="FixedSizedQueue{T}"/> class with the specified limit.
25
    /// </summary>
26
    /// <param name="limit">The maximum number of items the queue can hold.</param>
UNCOV
27
    public FixedSizedQueue(int limit)
×
28
    {
29
#if NET8_0_OR_GREATER
30
        ArgumentOutOfRangeException.ThrowIfLessThan(limit, 1);
×
31
#endif
32
        Limit = limit;
×
33
    }
×
34

35
    /// <summary>
36
    /// Initializes a new instance of the <see cref="FixedSizedQueue{T}"/> class with the specified limit and initial collection.
37
    /// </summary>
38
    /// <param name="limit">The maximum number of items the queue can hold.</param>
39
    /// <param name="collection">The initial collection to populate the queue.</param>
NEW
40
    public FixedSizedQueue(int limit, IEnumerable<T> collection)
×
41
    {
42
#if NET8_0_OR_GREATER
43
        ArgumentOutOfRangeException.ThrowIfLessThan(limit, 1);
×
44
#endif
45
        if (collection is null || !collection.Any())
×
46
            throw new ArgumentException("Can not initialize the Queue with a null or empty collection", nameof(collection));
×
47

48
        _queue = new Queue<T>(collection);
×
49
        Limit = limit;
×
50
    }
×
51

52
    /// <summary>
53
    /// Initializes a new instance of the <see cref="FixedSizedQueue{T}"/> class from the specified collection, using its count as the limit.
54
    /// </summary>
55
    /// <param name="collection">The initial collection to populate the queue.</param>
UNCOV
56
    public FixedSizedQueue(IEnumerable<T> collection)
×
57
    {
58
        if (collection is null || !collection.Any())
×
59
            throw new ArgumentException("Can not initialize the Queue with a null or empty collection", nameof(collection));
×
60

61
        _queue = new Queue<T>(collection);
×
62
        Limit = _queue.Count;
×
63
    }
×
64

65
    /// <summary>
66
    /// Adds an item to the queue, dequeuing the oldest items if the limit is exceeded.
67
    /// </summary>
68
    /// <param name="obj">The item to enqueue.</param>
69
    public void Enqueue(T obj)
70
    {
71
        lock (_lock)
×
72
        {
73
            _queue.Enqueue(obj);
×
74

75
            while (_queue.Count > Limit)
×
76
                _queue.Dequeue();
×
77
        }
×
78
    }
×
79

80
    /// <summary>
81
    /// Removes all items from the queue.
82
    /// </summary>
83
    public void Clear()
84
    {
85
        lock (_lock)
×
86
            _queue.Clear();
×
87
    }
×
88

89
    /// <inheritdoc/>
90
    public IEnumerator<T> GetEnumerator()
91
    {
92
        lock (_lock)
×
93
            return new List<T>(_queue).GetEnumerator();
×
94
    }
×
95

96
    /// <inheritdoc/>
UNCOV
97
    IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
×
98
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc