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
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ private string GetSessionFilePath()
}

/// <summary>
/// Add a static TestEventListener which will redirect SDK logging
/// Add a static <see cref="Diagnostics.AzureEventSourceListener"/> which will redirect SDK logging
/// to Console.Out for easy debugging.
/// </summary>
private static TestLogger Logger { get; set; }
Expand Down
23 changes: 23 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 = 1000;

public IEnumerable<EventWrittenEventArgs> EventData => _events;

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

if (!_disposed)
{
if (_events.Count >= _maxEventCount)
{
throw new Exception($"Number of events has exceeded {_maxEventCount}. Create {typeof(TestEventListener)} with a larger 'maxEventCount'.");
}

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

/// <summary>
/// Creates an instance of <see cref="TestEventListener"/>.
/// </summary>
public TestEventListener() : this(DefaultMaxEventCount)
{ }

/// <summary>
/// Creates an instance of <see cref="TestEventListener"/>.
/// </summary>
/// <param name="maxEventCount">Maximum number of events that the listener can store in <see cref="EventData"/>.
/// <para>If the number of events exceeds the value, an <see cref="Exception"/> is thrown.</para></param>
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
2 changes: 1 addition & 1 deletion sdk/core/Azure.Core/tests/RetryPolicyTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ public async Task MsHeadersArePreferredOverRetryAfter(string headerName)
public async Task RetryingEmitsEventSourceEvent()
{
var responseClassifier = new MockResponseClassifier(retriableCodes: new[] { 500 });
var listener = new TestEventListener();
using var listener = new TestEventListener();
listener.EnableEvents(AzureCoreEventSource.Singleton, EventLevel.Informational);

(HttpPipelinePolicy policy, AsyncGate<TimeSpan, object> gate) = CreateRetryPolicy(maxRetries: 2);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ public void Setup()
_listener.EnableEvents(AzureIdentityEventSource.Singleton, EventLevel.Verbose);
}

[TearDown]
public void TearDown()
{
_listener.Dispose();
}

[Test]
public void MatchesNameAndGuid()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,26 +22,11 @@ namespace Azure.Search.Documents.Tests
[NonParallelizable]
public class BatchingTests : SearchTestBase
{
private TestEventListener _listener;

public BatchingTests(bool async, SearchClientOptions.ServiceVersion serviceVersion)
: base(async, serviceVersion, null /* RecordedTestMode.Record /* to re-record */)
{
}

[SetUp]
public void Setup()
{
_listener = new TestEventListener();
_listener.EnableEvents(AzureSearchDocumentsEventSource.Instance, EventLevel.Verbose);
}

[TearDown]
public void TearDown()
{
_listener.Dispose();
}

#region Utilities
private const int BatchSize = SearchIndexingBufferedSenderOptions<object>.DefaultInitialBatchActionCount;
private readonly TimeSpan EventDelay = TimeSpan.FromMilliseconds(250);
Expand Down Expand Up @@ -1067,10 +1052,13 @@ public async Task Behavior_Split()

client.SplitNextBatch = true;

using var listener = new TestEventListener();
listener.EnableEvents(AzureSearchDocumentsEventSource.Instance, EventLevel.Verbose);

await indexer.UploadDocumentsAsync(data);
await indexer.FlushAsync();

List<EventWrittenEventArgs> eventData = _listener.EventData.ToList();
List<EventWrittenEventArgs> eventData = listener.EventData.ToList();

Assert.AreEqual(10, eventData.Count);
Assert.AreEqual("PendingQueueResized", eventData[0].EventName); // 1. All events are pushed into the pending queue.
Expand Down