-
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.
Add test for EventListener consuming ThreadPool events (#48487)
- Loading branch information
Showing
4 changed files
with
106 additions
and
9 deletions.
There are no files selected for viewing
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
80 changes: 80 additions & 0 deletions
80
src/tests/tracing/eventlistener/EventListenerThreadPool.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,80 @@ | ||
// 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.Diagnostics.Tracing; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace Tracing.Tests | ||
{ | ||
internal sealed class RuntimeEventListener : EventListener | ||
{ | ||
public volatile int TPWorkerThreadStartCount = 0; | ||
public volatile int TPWorkerThreadStopCount = 0; | ||
public volatile int TPWorkerThreadWaitCount = 0; | ||
|
||
public ManualResetEvent TPWaitEvent = new ManualResetEvent(false); | ||
|
||
protected override void OnEventSourceCreated(EventSource source) | ||
{ | ||
if (source.Name.Equals("Microsoft-Windows-DotNETRuntime")) | ||
{ | ||
EnableEvents(source, EventLevel.Informational, (EventKeywords)0x10000); | ||
} | ||
} | ||
|
||
protected override void OnEventWritten(EventWrittenEventArgs eventData) | ||
{ | ||
if (eventData.EventName.Equals("ThreadPoolWorkerThreadStart")) | ||
{ | ||
Interlocked.Increment(ref TPWorkerThreadStartCount); | ||
TPWaitEvent.Set(); | ||
} | ||
else if (eventData.EventName.Equals("ThreadPoolWorkerThreadStop")) | ||
{ | ||
Interlocked.Increment(ref TPWorkerThreadStopCount); | ||
TPWaitEvent.Set(); | ||
} | ||
else if (eventData.EventName.Equals("ThreadPoolWorkerThreadWait")) | ||
{ | ||
Interlocked.Increment(ref TPWorkerThreadWaitCount); | ||
TPWaitEvent.Set(); | ||
} | ||
} | ||
} | ||
|
||
class EventListenerThreadPool | ||
{ | ||
static int Main(string[] args) | ||
{ | ||
using (RuntimeEventListener listener = new RuntimeEventListener()) | ||
{ | ||
int someNumber = 0; | ||
Task[] tasks = new Task[100]; | ||
for (int i = 0; i < tasks.Length; i++) | ||
{ | ||
tasks[i] = Task.Run(() => { someNumber += 1; }); | ||
} | ||
|
||
listener.TPWaitEvent.WaitOne(TimeSpan.FromMinutes(3)); | ||
|
||
if (listener.TPWorkerThreadStartCount > 0 || | ||
listener.TPWorkerThreadStopCount > 0 || | ||
listener.TPWorkerThreadWaitCount > 0) | ||
{ | ||
Console.WriteLine("Test Passed."); | ||
return 100; | ||
} | ||
else | ||
{ | ||
Console.WriteLine("Test Failed: Did not see any of the expected events."); | ||
Console.WriteLine($"ThreadPoolWorkerThreadStartCount: {listener.TPWorkerThreadStartCount}"); | ||
Console.WriteLine($"ThreadPoolWorkerThreadStopCount: {listener.TPWorkerThreadStopCount}"); | ||
Console.WriteLine($"ThreadPoolWorkerThreadWaitCount: {listener.TPWorkerThreadWaitCount}"); | ||
return -1; | ||
} | ||
} | ||
} | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
src/tests/tracing/eventlistener/EventListenerThreadPool.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,13 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<CLRTestKind>BuildAndRun</CLRTestKind> | ||
<AllowUnsafeBlocks>true</AllowUnsafeBlocks> | ||
<CLRTestPriority>0</CLRTestPriority> | ||
<DisableProjectBuild Condition="'$(RuntimeFlavor)' == 'mono'">true</DisableProjectBuild> | ||
</PropertyGroup> | ||
<ItemGroup> | ||
<Compile Include="EventListenerThreadPool.cs" /> | ||
<ProjectReference Include="../common/common.csproj" /> | ||
</ItemGroup> | ||
</Project> |