Skip to content

Commit

Permalink
Remove some timeouts and reorganize test (#97115)
Browse files Browse the repository at this point in the history
Ensure target process runs to completion.
Simplify steps in test and dispose of additional classes.
  • Loading branch information
AaronRobinsonMSFT authored Jan 20, 2024
1 parent 62d33ee commit ca1b161
Showing 1 changed file with 81 additions and 87 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
#if USE_MDT_EVENTSOURCE
using Microsoft.Diagnostics.Tracing;
#else
Expand All @@ -14,8 +16,6 @@
using Xunit;

using SdtEventSources;
using System.Diagnostics;
using System.Threading;
using Microsoft.Diagnostics.Tracing.Session;
using Microsoft.DotNet.RemoteExecutor;
using Microsoft.Diagnostics.Tracing;
Expand All @@ -35,42 +35,38 @@ public void Test_EventSource_EtwManifestGeneration()
{
var pid = Process.GetCurrentProcess().Id;
var etlFileName = $"file.{pid}.etl";
var tracesession = new TraceEventSession("testname", etlFileName);

tracesession.EnableProvider(nameof(SimpleEventSource));

RemoteInvokeOptions localOptions = new RemoteInvokeOptions { TimeOut = 300_000 /* ms */ };
using (RemoteInvokeHandle handle = RemoteExecutor.Invoke(() =>
{
var es = new SimpleEventSource();
for (var i = 0; i < 100; i++)
{
es.WriteSimpleInt(i);
Thread.Sleep(100);
}
}, localOptions))
// Start the trace session
using (var traceSession = new TraceEventSession(nameof(Test_EventSource_EtwManifestGeneration), etlFileName))
{
Thread.Sleep(TimeSpan.FromSeconds(5));

tracesession.Flush();
// Enable the provider of interest.
traceSession.EnableProvider(nameof(SimpleEventSource));

tracesession.DisableProvider(nameof(SimpleEventSource));
tracesession.Dispose();

var manifestExists = false;
var max_retries = 50;

for (int i = 0; i < max_retries; i++)
// Launch the target process to collect data
using (RemoteInvokeHandle handle = RemoteExecutor.Invoke(() =>
{
if (VerifyManifestAndRemoveFile(etlFileName))
using var es = new SimpleEventSource();
// 50 * 100 = 5 seconds
for (var i = 0; i < 50; i++)
{
manifestExists = true;
break;
es.WriteSimpleInt(i);
Thread.Sleep(100);
}
Thread.Sleep(1000);
}))
{
handle.Process.WaitForExit();
}
Assert.True(manifestExists);

// Flush session and disable the provider.
traceSession.Flush();
traceSession.DisableProvider(nameof(SimpleEventSource));
}

// Wait for the ETL file to flush to disk
Thread.Sleep(TimeSpan.FromSeconds(2));

Assert.True(VerifyManifestAndRemoveFile(etlFileName));
}

[ConditionalFact(nameof(IsProcessElevatedAndNotWindowsNanoServerAndRemoteExecutorSupported))]
Expand All @@ -80,87 +76,85 @@ public void Test_EventSource_EtwManifestGenerationRollover()
var pid = Process.GetCurrentProcess().Id;
var initialFileName = $"initialFile.{pid}.etl";
var rolloverFileName = $"rolloverFile.{pid}.etl";
var tracesession = new TraceEventSession("testname", initialFileName);

tracesession.EnableProvider(nameof(SimpleEventSource));

using (RemoteInvokeHandle handle = RemoteExecutor.Invoke(() =>
// Start the trace session
using (var traceSession = new TraceEventSession(nameof(Test_EventSource_EtwManifestGenerationRollover), initialFileName))
{
var es = new SimpleEventSource();
for (var i = 0; i < 100; i++)
{
es.WriteSimpleInt(i);
Thread.Sleep(100);
}
}))
{
Thread.Sleep(TimeSpan.FromSeconds(5));
// Enable the provider of interest.
traceSession.EnableProvider(nameof(SimpleEventSource));

tracesession.Flush();
// Launch the target process to collect data
using (RemoteInvokeHandle handle = RemoteExecutor.Invoke(() =>
{
using var es = new SimpleEventSource();
tracesession.SetFileName(rolloverFileName);
// 100 * 100 = 10 seconds
for (var i = 0; i < 100; i++)
{
es.WriteSimpleInt(i);
Thread.Sleep(100);
}
}))
{
// Wait for some time to collect events
Thread.Sleep(TimeSpan.FromSeconds(5));

Thread.Sleep(TimeSpan.FromSeconds(5));
traceSession.Flush();

tracesession.Flush();
traceSession.SetFileName(rolloverFileName);

tracesession.DisableProvider(nameof(SimpleEventSource));
tracesession.Dispose();
// Wait for some time to collect events
Thread.Sleep(TimeSpan.FromSeconds(5));

bool initialFileHasManifest = false;
bool rollOverFileHasManifest = false;
// Wait for the target process to exit.
handle.Process.WaitForExit();

var max_retries = 50;
for (int i = 0; i < max_retries; i++)
{
if (VerifyManifestAndRemoveFile(initialFileName))
{
initialFileHasManifest = true;
break;
}
Thread.Sleep(1000);
// Flush session and disable the provider.
traceSession.Flush();
traceSession.DisableProvider(nameof(SimpleEventSource));
}
for (int i = 0; i < max_retries; i++)
{
if (VerifyManifestAndRemoveFile(rolloverFileName))
{
rollOverFileHasManifest = true;
break;
}
Thread.Sleep(1000);
}
Assert.True(initialFileHasManifest);
Assert.True(rollOverFileHasManifest);
}

// Wait for the ETL files to flush to disk
Thread.Sleep(TimeSpan.FromSeconds(2));

Assert.True(VerifyManifestAndRemoveFile(initialFileName));
Assert.True(VerifyManifestAndRemoveFile(rolloverFileName));
}

private bool VerifyManifestAndRemoveFile(string fileName)
{
Assert.True(File.Exists(fileName));

using var source = new ETWTraceEventSource(fileName);

Dictionary<string, int> providers = new Dictionary<string, int>();
int eventCount = 0;
var sawManifestData = false;
source.Dynamic.All += (eventData) =>

using (var source = new ETWTraceEventSource(fileName))
{
eventCount++;
if (!providers.ContainsKey(eventData.ProviderName))
source.Dynamic.All += (eventData) =>
{
providers[eventData.ProviderName] = 0;
}
providers[eventData.ProviderName]++;
eventCount++;
if (!providers.ContainsKey(eventData.ProviderName))
{
providers[eventData.ProviderName] = 0;
}
providers[eventData.ProviderName]++;
if (eventData.ProviderName.Equals(nameof(SimpleEventSource)) && eventData.EventName.Equals("ManifestData"))
{
sawManifestData = true;
}
};
source.Process();
//File.Delete(fileName);
if (eventData.ProviderName.Equals(nameof(SimpleEventSource)) && eventData.EventName.Equals("ManifestData"))
{
sawManifestData = true;
}
};
source.Process();
}

if (!sawManifestData)
if (sawManifestData)
{
// Delete file if successfully processed.
File.Delete(fileName);
}
else
{
Console.WriteLine($"Did not see ManifestData event from {nameof(SimpleEventSource)}, test will fail. Additional info:");
Console.WriteLine($" file name {fileName}");
Expand Down

0 comments on commit ca1b161

Please sign in to comment.