Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Limit number of events stored in TestEventListener #22263

Merged
merged 7 commits into from
Jul 5, 2021
15 changes: 15 additions & 0 deletions sdk/core/Azure.Core.TestFramework/src/TestEventListener.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ public class TestEventListener : EventListener
{
private volatile bool _disposed;
private readonly ConcurrentQueue<EventWrittenEventArgs> _events = new ConcurrentQueue<EventWrittenEventArgs>();
private uint _maxEventCount;
private const uint DefaultMaxEventCount = 100;
Mohit-Chakraborty marked this conversation as resolved.
Show resolved Hide resolved

public IEnumerable<EventWrittenEventArgs> EventData => _events;

Expand All @@ -27,12 +29,25 @@ protected override void OnEventWritten(EventWrittenEventArgs eventData)

if (!_disposed)
{
if (_events.Count >= _maxEventCount)
{
_events.TryDequeue(out _);
}

// Make sure we can format the event
EventSourceEventFormatting.Format(eventData);
_events.Enqueue(eventData);
}
}

public TestEventListener() : this(DefaultMaxEventCount)
{ }

public TestEventListener(uint maxEventCount)
Mohit-Chakraborty marked this conversation as resolved.
Show resolved Hide resolved
{
_maxEventCount = maxEventCount;
}

public EventWrittenEventArgs SingleEventById(int id, Func<EventWrittenEventArgs, bool> filter = null)
{
return EventsById(id).Single(filter ?? (_ => true));
Expand Down