-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow multiple EventCounters sessions and make sure EventListeners al…
…ways send a Disable command (#82970)
- Loading branch information
Showing
5 changed files
with
313 additions
and
21 deletions.
There are no files selected for viewing
90 changes: 90 additions & 0 deletions
90
src/libraries/System.Diagnostics.Tracing/tests/BasicEventSourceTest/TestCallbacks.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using Xunit; | ||
#if USE_MDT_EVENTSOURCE | ||
using Microsoft.Diagnostics.Tracing; | ||
#else | ||
using System.Diagnostics.Tracing; | ||
#endif | ||
using System.Reflection; | ||
|
||
namespace BasicEventSourceTests | ||
{ | ||
public class TestsEventSourceCallbacks | ||
{ | ||
/// <summary> | ||
/// Validates that the EventProvider AppDomain.ProcessExit handler does not keep the EventProvider instance | ||
/// alive. | ||
/// </summary> | ||
[Fact] | ||
public void Test_EventSource_Lifetime() | ||
{ | ||
using (var source = new CallbacksTestEventSource()) | ||
{ | ||
bool isDisabledInDelegate = false; | ||
source.EventCommandExecuted += (sender, args) => | ||
{ | ||
if (args.Command == EventCommand.Disable) | ||
{ | ||
EventSource eventSource = (EventSource)sender; | ||
isDisabledInDelegate = !eventSource.IsEnabled(); | ||
} | ||
}; | ||
|
||
using (var listener = new CallbacksEventListener()) | ||
{ | ||
source.Event(); | ||
} | ||
|
||
if (!source._isDisabledInCallback) | ||
{ | ||
Assert.Fail("EventSource was still enabled in OnEventCommand callback"); | ||
} | ||
|
||
if (!isDisabledInDelegate) | ||
{ | ||
Assert.Fail("EventSource was still enabled in EventCommandExecuted delegate"); | ||
} | ||
} | ||
} | ||
|
||
private class CallbacksEventListener : EventListener | ||
{ | ||
protected override void OnEventSourceCreated(EventSource eventSource) | ||
{ | ||
base.OnEventSourceCreated(eventSource); | ||
|
||
if (eventSource.Name.Equals("TestsEventSourceCallbacks.CallbacksTestEventSource")) | ||
{ | ||
EnableEvents(eventSource, EventLevel.Verbose); | ||
} | ||
} | ||
} | ||
|
||
[EventSource(Name = "TestsEventSourceCallbacks.CallbacksTestEventSource")] | ||
private class CallbacksTestEventSource : EventSource | ||
{ | ||
internal bool _isDisabledInCallback; | ||
|
||
[Event(1)] | ||
public void Event() | ||
{ | ||
WriteEvent(1); | ||
} | ||
|
||
[NonEvent] | ||
protected override void OnEventCommand(EventCommandEventArgs command) | ||
{ | ||
base.OnEventCommand(command); | ||
|
||
_isDisabledInCallback = !IsEnabled(); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
106 changes: 106 additions & 0 deletions
106
src/tests/tracing/eventlistener/eventlistenerenabledisable.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System; | ||
using System.IO; | ||
using System.Diagnostics.Tracing; | ||
using System.Threading; | ||
using Tracing.Tests.Common; | ||
|
||
namespace Tracing.Tests | ||
{ | ||
[EventSource(Name = "Tracing.Tests.EnableDisableEventSource")] | ||
class EnableDisableEventSource : EventSource | ||
{ | ||
internal int _enables; | ||
internal int _disables; | ||
|
||
public EnableDisableEventSource() : base(true) { } | ||
|
||
[Event(1)] | ||
internal void TestEvent() { this.WriteEvent(1); } | ||
|
||
[NonEvent] | ||
protected override void OnEventCommand(EventCommandEventArgs command) | ||
{ | ||
base.OnEventCommand(command); | ||
|
||
if (command.Command == EventCommand.Enable) | ||
{ | ||
Interlocked.Increment(ref _enables); | ||
} | ||
else if (command.Command == EventCommand.Disable) | ||
{ | ||
Interlocked.Increment(ref _disables); | ||
} | ||
else | ||
{ | ||
throw new Exception($"Saw unexpected command {command.Command} in OnEventCommand"); | ||
} | ||
} | ||
} | ||
|
||
internal sealed class EnableDisableListener : EventListener | ||
{ | ||
private EventSource? _target; | ||
|
||
protected override void OnEventSourceCreated(EventSource source) | ||
{ | ||
if (source.Name.Equals("Tracing.Tests.EnableDisableEventSource")) | ||
{ | ||
_target = source; | ||
EnableEvents(_target, EventLevel.Verbose); | ||
} | ||
} | ||
|
||
public void Disable() | ||
{ | ||
DisableEvents(_target); | ||
} | ||
|
||
public void Dispose(bool disableEvents) | ||
{ | ||
base.Dispose(); | ||
|
||
if (disableEvents) | ||
{ | ||
// Dispose should call DisableEvents for us, this should be ignored after Dispose() | ||
Disable(); | ||
} | ||
} | ||
} | ||
|
||
class EventListenerEnableDisableTest | ||
{ | ||
static int Main() | ||
{ | ||
bool pass = false; | ||
using(var source = new EnableDisableEventSource()) | ||
{ | ||
// Testing three scenarios: | ||
// listener1 calls EnableEvents but never calls DisableEvents | ||
// listener2 calls EnableEvents and calls DisableEvents outside of Dispose | ||
// listener3 calls EnableEvents and calls DisableEvents inside of Dispose | ||
// | ||
// We should get an Enable and Disable for all of them | ||
using (var listener1 = new EnableDisableListener()) | ||
using (var listener2 = new EnableDisableListener()) | ||
{ | ||
var listener3 = new EnableDisableListener(); | ||
source.TestEvent(); | ||
listener3.Dispose(true); | ||
|
||
listener2.Disable(); | ||
} | ||
|
||
if (source._enables == 3 && source._disables == 3) | ||
{ | ||
return 100; | ||
} | ||
|
||
Console.WriteLine($"Unexpected enable/disable count _enables={source._enables} _disables={source._disables}"); | ||
return -1; | ||
} | ||
} | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
src/tests/tracing/eventlistener/eventlistenerenabledisable.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<AllowUnsafeBlocks>true</AllowUnsafeBlocks> | ||
</PropertyGroup> | ||
<ItemGroup> | ||
<Compile Include="eventlistenerenabledisable.cs" /> | ||
<ProjectReference Include="../common/common.csproj" /> | ||
</ItemGroup> | ||
</Project> |