Skip to content

Commit 1b382e2

Browse files
author
John Salem
authored
simplify gh53564 test (#54081)
1 parent c14ac48 commit 1b382e2

File tree

1 file changed

+20
-10
lines changed

1 file changed

+20
-10
lines changed

src/tests/tracing/eventcounter/gh53564.cs

+20-10
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ public RuntimeCounterListener(){}
2020

2121
private DateTime? setToZeroTimestamp = null;
2222
private DateTime? mostRecentTimestamp = null;
23+
private ManualResetEvent setToZero = new ManualResetEvent(initialState: false);
2324
public ManualResetEvent ReadyToVerify { get; } = new ManualResetEvent(initialState: false);
2425

2526
protected override void OnEventSourceCreated(EventSource source)
@@ -28,7 +29,7 @@ protected override void OnEventSourceCreated(EventSource source)
2829
{
2930
Dictionary<string, string> refreshInterval = new Dictionary<string, string>();
3031

31-
Console.WriteLine($"[{DateTime.Now:hh:mm:ss.fff}] Setting interval to 1");
32+
Console.WriteLine($"[{DateTime.UtcNow:hh:mm:ss.fff}] OnEventSourceCreated :: Setting interval to 1");
3233
// first set interval to 1 seconds
3334
refreshInterval["EventCounterIntervalSec"] = "1";
3435
EnableEvents(source, EventLevel.Informational, (EventKeywords)(-1), refreshInterval);
@@ -37,32 +38,42 @@ protected override void OnEventSourceCreated(EventSource source)
3738
Thread.Sleep(TimeSpan.FromSeconds(3));
3839

3940
// then set interval to 0
40-
Console.WriteLine($"[{DateTime.Now:hh:mm:ss.fff}] Setting interval to 0");
41+
Console.WriteLine($"[{DateTime.UtcNow:hh:mm:ss.fff}] OnEventSourceCreated :: Setting interval to 0");
4142
refreshInterval["EventCounterIntervalSec"] = "0";
4243
EnableEvents(source, EventLevel.Informational, (EventKeywords)(-1), refreshInterval);
43-
setToZeroTimestamp = DateTime.Now + TimeSpan.FromSeconds(1); // Stash timestamp 1 second after setting to 0
44+
setToZeroTimestamp = DateTime.UtcNow + TimeSpan.FromSeconds(1); // Stash timestamp 1 second after setting to 0
45+
setToZero.Set();
4446

4547
// then attempt to set interval back to 1
4648
Thread.Sleep(TimeSpan.FromSeconds(3));
47-
Console.WriteLine($"[{DateTime.Now:hh:mm:ss.fff}] Setting interval to 1");
49+
Console.WriteLine($"[{DateTime.UtcNow:hh:mm:ss.fff}] OnEventSourceCreated :: Setting interval to 1");
4850
refreshInterval["EventCounterIntervalSec"] = "1";
4951
EnableEvents(source, EventLevel.Informational, (EventKeywords)(-1), refreshInterval);
50-
Thread.Sleep(TimeSpan.FromSeconds(3));
51-
Console.WriteLine($"[{DateTime.Now:hh:mm:ss.fff}] Setting ReadyToVerify");
52-
ReadyToVerify.Set();
5352
}
5453
}
5554

5655
protected override void OnEventWritten(EventWrittenEventArgs eventData)
5756
{
58-
mostRecentTimestamp = eventData.TimeStamp;
57+
if (!ReadyToVerify.WaitOne(0))
58+
{
59+
mostRecentTimestamp = eventData.TimeStamp;
60+
if (setToZero.WaitOne(0) && mostRecentTimestamp > setToZeroTimestamp)
61+
{
62+
Console.WriteLine($"[{DateTime.UtcNow:hh:mm:ss.fff}] OnEventWritten :: Setting ReadyToVerify");
63+
ReadyToVerify.Set();
64+
}
65+
}
5966
}
6067

6168
public bool Verify()
6269
{
6370
if (!ReadyToVerify.WaitOne(0))
6471
return false;
6572

73+
Console.WriteLine($"[{DateTime.UtcNow:hh:mm:ss.fff}] Verify :: Verifying");
74+
Console.WriteLine($"[{DateTime.UtcNow:hh:mm:ss.fff}] setToZeroTimestamp = {setToZeroTimestamp?.ToString("hh:mm:ss.fff") ?? "NULL"}");
75+
Console.WriteLine($"[{DateTime.UtcNow:hh:mm:ss.fff}] mostRecentTimestamp = {mostRecentTimestamp?.ToString("hh:mm:ss.fff") ?? "NULL"}");
76+
6677
return (setToZeroTimestamp is null || mostRecentTimestamp is null) ? false : setToZeroTimestamp < mostRecentTimestamp;
6778
}
6879
}
@@ -74,9 +85,8 @@ public static int Main(string[] args)
7485
// Create an EventListener.
7586
using (RuntimeCounterListener myListener = new RuntimeCounterListener())
7687
{
77-
if (myListener.ReadyToVerify.WaitOne(TimeSpan.FromSeconds(15)))
88+
if (myListener.ReadyToVerify.WaitOne(TimeSpan.FromSeconds(30)))
7889
{
79-
Console.WriteLine($"[{DateTime.Now:hh:mm:ss.fff}] Ready to verify");
8090
if (myListener.Verify())
8191
{
8292
Console.WriteLine("Test passed");

0 commit comments

Comments
 (0)