-
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.
- Loading branch information
Showing
2 changed files
with
99 additions
and
3 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