33
44using System ;
55using System . Collections . Generic ;
6+ using System . Diagnostics ;
67using System . IO ;
78using System . Linq ;
89using System . Text ;
10+ using System . Threading ;
911#if USE_MDT_EVENTSOURCE
1012using Microsoft . Diagnostics . Tracing ;
1113#else
1416using Xunit ;
1517
1618using SdtEventSources ;
17- using System . Diagnostics ;
18- using System . Threading ;
1919using Microsoft . Diagnostics . Tracing . Session ;
2020using Microsoft . DotNet . RemoteExecutor ;
2121using Microsoft . Diagnostics . Tracing ;
@@ -35,42 +35,38 @@ public void Test_EventSource_EtwManifestGeneration()
3535 {
3636 var pid = Process . GetCurrentProcess ( ) . Id ;
3737 var etlFileName = $ "file.{ pid } .etl";
38- var tracesession = new TraceEventSession ( "testname" , etlFileName ) ;
39-
40- tracesession . EnableProvider ( nameof ( SimpleEventSource ) ) ;
4138
42- RemoteInvokeOptions localOptions = new RemoteInvokeOptions { TimeOut = 300_000 /* ms */ } ;
43- using ( RemoteInvokeHandle handle = RemoteExecutor . Invoke ( ( ) =>
44- {
45- var es = new SimpleEventSource ( ) ;
46- for ( var i = 0 ; i < 100 ; i ++ )
47- {
48- es . WriteSimpleInt ( i ) ;
49- Thread . Sleep ( 100 ) ;
50- }
51- } , localOptions ) )
39+ // Start the trace session
40+ using ( var traceSession = new TraceEventSession ( nameof ( Test_EventSource_EtwManifestGeneration ) , etlFileName ) )
5241 {
53- Thread . Sleep ( TimeSpan . FromSeconds ( 5 ) ) ;
54-
55- tracesession . Flush ( ) ;
42+ // Enable the provider of interest.
43+ traceSession . EnableProvider ( nameof ( SimpleEventSource ) ) ;
5644
57- tracesession . DisableProvider ( nameof ( SimpleEventSource ) ) ;
58- tracesession . Dispose ( ) ;
59-
60- var manifestExists = false ;
61- var max_retries = 50 ;
62-
63- for ( int i = 0 ; i < max_retries ; i ++ )
45+ // Launch the target process to collect data
46+ using ( RemoteInvokeHandle handle = RemoteExecutor . Invoke ( ( ) =>
6447 {
65- if ( VerifyManifestAndRemoveFile ( etlFileName ) )
48+ using var es = new SimpleEventSource ( ) ;
49+
50+ // 50 * 100 = 5 seconds
51+ for ( var i = 0 ; i < 50 ; i ++ )
6652 {
67- manifestExists = true ;
68- break ;
53+ es . WriteSimpleInt ( i ) ;
54+ Thread . Sleep ( 100 ) ;
6955 }
70- Thread . Sleep ( 1000 ) ;
56+ } ) )
57+ {
58+ handle . Process . WaitForExit ( ) ;
7159 }
72- Assert . True ( manifestExists ) ;
60+
61+ // Flush session and disable the provider.
62+ traceSession . Flush ( ) ;
63+ traceSession . DisableProvider ( nameof ( SimpleEventSource ) ) ;
7364 }
65+
66+ // Wait for the ETL file to flush to disk
67+ Thread . Sleep ( TimeSpan . FromSeconds ( 2 ) ) ;
68+
69+ Assert . True ( VerifyManifestAndRemoveFile ( etlFileName ) ) ;
7470 }
7571
7672 [ ConditionalFact ( nameof ( IsProcessElevatedAndNotWindowsNanoServerAndRemoteExecutorSupported ) ) ]
@@ -80,87 +76,85 @@ public void Test_EventSource_EtwManifestGenerationRollover()
8076 var pid = Process . GetCurrentProcess ( ) . Id ;
8177 var initialFileName = $ "initialFile.{ pid } .etl";
8278 var rolloverFileName = $ "rolloverFile.{ pid } .etl";
83- var tracesession = new TraceEventSession ( "testname" , initialFileName ) ;
84-
85- tracesession . EnableProvider ( nameof ( SimpleEventSource ) ) ;
8679
87- using ( RemoteInvokeHandle handle = RemoteExecutor . Invoke ( ( ) =>
80+ // Start the trace session
81+ using ( var traceSession = new TraceEventSession ( nameof ( Test_EventSource_EtwManifestGenerationRollover ) , initialFileName ) )
8882 {
89- var es = new SimpleEventSource ( ) ;
90- for ( var i = 0 ; i < 100 ; i ++ )
91- {
92- es . WriteSimpleInt ( i ) ;
93- Thread . Sleep ( 100 ) ;
94- }
95- } ) )
96- {
97- Thread . Sleep ( TimeSpan . FromSeconds ( 5 ) ) ;
83+ // Enable the provider of interest.
84+ traceSession . EnableProvider ( nameof ( SimpleEventSource ) ) ;
9885
99- tracesession . Flush ( ) ;
86+ // Launch the target process to collect data
87+ using ( RemoteInvokeHandle handle = RemoteExecutor . Invoke ( ( ) =>
88+ {
89+ using var es = new SimpleEventSource ( ) ;
10090
101- tracesession . SetFileName ( rolloverFileName ) ;
91+ // 100 * 100 = 10 seconds
92+ for ( var i = 0 ; i < 100 ; i ++ )
93+ {
94+ es . WriteSimpleInt ( i ) ;
95+ Thread . Sleep ( 100 ) ;
96+ }
97+ } ) )
98+ {
99+ // Wait for some time to collect events
100+ Thread . Sleep ( TimeSpan . FromSeconds ( 5 ) ) ;
102101
103- Thread . Sleep ( TimeSpan . FromSeconds ( 5 ) ) ;
102+ traceSession . Flush ( ) ;
104103
105- tracesession . Flush ( ) ;
104+ traceSession . SetFileName ( rolloverFileName ) ;
106105
107- tracesession . DisableProvider ( nameof ( SimpleEventSource ) ) ;
108- tracesession . Dispose ( ) ;
106+ // Wait for some time to collect events
107+ Thread . Sleep ( TimeSpan . FromSeconds ( 5 ) ) ;
109108
110- bool initialFileHasManifest = false ;
111- bool rollOverFileHasManifest = false ;
109+ // Wait for the target process to exit.
110+ handle . Process . WaitForExit ( ) ;
112111
113- var max_retries = 50 ;
114- for ( int i = 0 ; i < max_retries ; i ++ )
115- {
116- if ( VerifyManifestAndRemoveFile ( initialFileName ) )
117- {
118- initialFileHasManifest = true ;
119- break ;
120- }
121- Thread . Sleep ( 1000 ) ;
112+ // Flush session and disable the provider.
113+ traceSession . Flush ( ) ;
114+ traceSession . DisableProvider ( nameof ( SimpleEventSource ) ) ;
122115 }
123- for ( int i = 0 ; i < max_retries ; i ++ )
124- {
125- if ( VerifyManifestAndRemoveFile ( rolloverFileName ) )
126- {
127- rollOverFileHasManifest = true ;
128- break ;
129- }
130- Thread . Sleep ( 1000 ) ;
131- }
132- Assert . True ( initialFileHasManifest ) ;
133- Assert . True ( rollOverFileHasManifest ) ;
134116 }
117+
118+ // Wait for the ETL files to flush to disk
119+ Thread . Sleep ( TimeSpan . FromSeconds ( 2 ) ) ;
120+
121+ Assert . True ( VerifyManifestAndRemoveFile ( initialFileName ) ) ;
122+ Assert . True ( VerifyManifestAndRemoveFile ( rolloverFileName ) ) ;
135123 }
136124
137125 private bool VerifyManifestAndRemoveFile ( string fileName )
138126 {
139127 Assert . True ( File . Exists ( fileName ) ) ;
140128
141- using var source = new ETWTraceEventSource ( fileName ) ;
142-
143129 Dictionary < string , int > providers = new Dictionary < string , int > ( ) ;
144130 int eventCount = 0 ;
145131 var sawManifestData = false ;
146- source . Dynamic . All += ( eventData ) =>
132+
133+ using ( var source = new ETWTraceEventSource ( fileName ) )
147134 {
148- eventCount ++ ;
149- if ( ! providers . ContainsKey ( eventData . ProviderName ) )
135+ source . Dynamic . All += ( eventData ) =>
150136 {
151- providers [ eventData . ProviderName ] = 0 ;
152- }
153- providers [ eventData . ProviderName ] ++ ;
137+ eventCount ++ ;
138+ if ( ! providers . ContainsKey ( eventData . ProviderName ) )
139+ {
140+ providers [ eventData . ProviderName ] = 0 ;
141+ }
142+ providers [ eventData . ProviderName ] ++ ;
154143
155- if ( eventData . ProviderName . Equals ( nameof ( SimpleEventSource ) ) && eventData . EventName . Equals ( "ManifestData" ) )
156- {
157- sawManifestData = true ;
158- }
159- } ;
160- source . Process ( ) ;
161- //File.Delete(fileName);
144+ if ( eventData . ProviderName . Equals ( nameof ( SimpleEventSource ) ) && eventData . EventName . Equals ( "ManifestData" ) )
145+ {
146+ sawManifestData = true ;
147+ }
148+ } ;
149+ source . Process ( ) ;
150+ }
162151
163- if ( ! sawManifestData )
152+ if ( sawManifestData )
153+ {
154+ // Delete file if successfully processed.
155+ File . Delete ( fileName ) ;
156+ }
157+ else
164158 {
165159 Console . WriteLine ( $ "Did not see ManifestData event from { nameof ( SimpleEventSource ) } , test will fail. Additional info:") ;
166160 Console . WriteLine ( $ " file name { fileName } ") ;
0 commit comments