Skip to content

Commit

Permalink
Changes
Browse files Browse the repository at this point in the history
  • Loading branch information
davmason committed Mar 29, 2023
1 parent 9612947 commit 5249e7f
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 3 deletions.
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();
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,16 @@ private void OnEventSourceCommand(object? sender, EventCommandEventArgs e)
{
Debug.Assert(e.Arguments != null);

if (e.Arguments.TryGetValue("EventCounterIntervalSec", out string? valueStr)
&& float.TryParse(valueStr, out float value))
string? valueStr = null;
float value = 0.0f;
if (!e.Arguments.TryGetValue("EventCounterIntervalSec", out valueStr)
|| !float.TryParse(valueStr, out value))
{
intervalValue = value;
// Command is Enable but no EventCounterIntervalSec arg so ignore
return;
}

intervalValue = value;
}

if ((e.Command == EventCommand.Disable && !_eventSource.IsEnabled()) || intervalValue <= 0)
Expand All @@ -74,6 +79,7 @@ private void OnEventSourceCommand(object? sender, EventCommandEventArgs e)

Debug.Assert((s_counterGroupEnabledList == null && !_eventSource.IsEnabled())
|| (_eventSource.IsEnabled() && s_counterGroupEnabledList!.Contains(this))
|| (intervalValue <= 0 && !s_counterGroupEnabledList!.Contains(this))
|| (!_eventSource.IsEnabled() && !s_counterGroupEnabledList!.Contains(this)));
}
}
Expand Down

0 comments on commit 5249e7f

Please sign in to comment.