Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit 986ffdb

Browse files
Liudmila Molkovadanmoseley
authored andcommitted
Use RemoteEchoServer in DiagnosticSource HttpHandlerDiagnosticListenerTests (#19863)
1 parent 40f4ddd commit 986ffdb

File tree

2 files changed

+118
-48
lines changed

2 files changed

+118
-48
lines changed

src/System.Diagnostics.DiagnosticSource/tests/HttpHandlerDiagnosticListenerTests.cs

Lines changed: 105 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@
1010

1111
namespace System.Diagnostics.Tests
1212
{
13-
public class HttpHandlerDiagnosticListenerTests
13+
using Configuration = System.Net.Test.Common.Configuration;
14+
15+
public class HttpHandlerDiagnosticListenerTests : RemoteExecutorTestBase
1416
{
1517
/// <summary>
1618
/// A simple test to make sure the Http Diagnostic Source is added into the list of DiagnosticListeners.
@@ -42,11 +44,14 @@ public void TestReflectInitializationViaSubscription1()
4244
using (var eventRecords = new EventObserverAndRecorder())
4345
{
4446
// Send a random Http request to generate some events
45-
new HttpClient().GetAsync("http://www.bing.com").Wait();
47+
using (var client = new HttpClient())
48+
{
49+
client.GetAsync(Configuration.Http.RemoteEchoServer).Result.Dispose();
50+
}
4651

4752
// Just make sure some events are written, to confirm we successfully subscribed to it. We should have
4853
// at least two events, one for request send, and one for response receive
49-
Assert.True(eventRecords.Records.Count >= 2,
54+
Assert.True(eventRecords.Records.Count == 2,
5055
"Didn't get two or more events from Http Diagnostic Listener. Something is wrong.");
5156
}
5257
}
@@ -61,11 +66,14 @@ public void TestReflectInitializationViaSubscription2()
6166
using (var eventRecords = new EventObserverAndRecorder(eventName => true))
6267
{
6368
// Send a random Http request to generate some events
64-
new HttpClient().GetAsync("http://www.bing.com").Wait();
69+
using (var client = new HttpClient())
70+
{
71+
client.GetAsync(Configuration.Http.RemoteEchoServer).Result.Dispose();
72+
}
6573

6674
// Just make sure some events are written, to confirm we successfully subscribed to it. We should have
6775
// at least two events, one for request send, and one for response receive
68-
Assert.True(eventRecords.Records.Count >= 2,
76+
Assert.True(eventRecords.Records.Count == 2,
6977
"Didn't get two or more events from Http Diagnostic Listener. Something is wrong.");
7078
}
7179
}
@@ -80,7 +88,10 @@ public void TestReflectInitializationViaSubscription3()
8088
using (var eventRecords = new EventObserverAndRecorder((eventName, arg1, arg2) => true))
8189
{
8290
// Send a random Http request to generate some events
83-
new HttpClient().GetAsync("http://www.bing.com").Wait();
91+
using (var client = new HttpClient())
92+
{
93+
client.GetAsync(Configuration.Http.RemoteEchoServer).Result.Dispose();
94+
}
8495

8596
// Just make sure some events are written, to confirm we successfully subscribed to it. We should have
8697
// at least two events, one for request send, and one for response receive
@@ -97,7 +108,12 @@ public async Task TestBasicReceiveAndResponseEvents()
97108
using (var eventRecords = new EventObserverAndRecorder())
98109
{
99110
// Send a random Http request to generate some events
100-
await new HttpClient().GetAsync("http://www.bing.com");
111+
using (var client = new HttpClient())
112+
{
113+
using (await client.GetAsync(Configuration.Http.RemoteEchoServer))
114+
{
115+
}
116+
}
101117

102118
// Just make sure some events are written, to confirm we successfully subscribed to it. We should have
103119
// at least two events, one for request send, and one for response receive
@@ -121,6 +137,30 @@ public async Task TestBasicReceiveAndResponseEvents()
121137
}
122138
}
123139

140+
/// <summary>
141+
/// Test that if request is redirected, it gets only one Start and one Stop event
142+
/// </summary>
143+
[Fact]
144+
public async Task TestRedirectedRequest()
145+
{
146+
using (var eventRecords = new EventObserverAndRecorder())
147+
{
148+
using (var client = new HttpClient())
149+
{
150+
var uriWithRedirect =
151+
Configuration.Http.RedirectUriForDestinationUri(true, 302, Configuration.Http.RemoteEchoServer, 10);
152+
using (await client.GetAsync(uriWithRedirect))
153+
{
154+
}
155+
}
156+
157+
// Just make sure some events are written, to confirm we successfully subscribed to it. We should have
158+
// at least two events, one for request send, and one for response receive
159+
Assert.Equal(1, eventRecords.Records.Count(rec => rec.Key.EndsWith("Start")));
160+
Assert.Equal(1, eventRecords.Records.Count(rec => rec.Key.EndsWith("Stop")));
161+
}
162+
}
163+
124164
/// <summary>
125165
/// Test exception in request processing: exception should have expected type/status and now be swallowed by reflection hook
126166
/// </summary>
@@ -154,8 +194,11 @@ public async Task TestCanceledRequest()
154194
CancellationTokenSource cts = new CancellationTokenSource(TimeSpan.FromSeconds(10));
155195
using (var eventRecords = new EventObserverAndRecorder( _ => { cts.Cancel();}))
156196
{
157-
var ex = await Assert.ThrowsAnyAsync<Exception>(() => new HttpClient().GetAsync("http://bing.com", cts.Token));
158-
Assert.True(ex is TaskCanceledException || ex is WebException);
197+
using (var client = new HttpClient())
198+
{
199+
var ex = await Assert.ThrowsAnyAsync<Exception>(() => client.GetAsync(Configuration.Http.RemoteEchoServer, cts.Token));
200+
Assert.True(ex is TaskCanceledException || ex is WebException);
201+
}
159202

160203
// Just make sure some events are written, to confirm we successfully subscribed to it. We should have
161204
// 1 start event and no stop events
@@ -173,7 +216,12 @@ public async Task TestActivityIsCreated()
173216
var parentActivity = new Activity("parent").AddBaggage("k1", "v1").AddBaggage("k2", "v2").Start();
174217
using (var eventRecords = new EventObserverAndRecorder())
175218
{
176-
await new HttpClient().GetAsync("http://www.bing.com");
219+
using (var client = new HttpClient())
220+
{
221+
using (await client.GetAsync(Configuration.Http.RemoteEchoServer))
222+
{
223+
}
224+
}
177225

178226
Assert.Equal(1, eventRecords.Records.Count(rec => rec.Key.EndsWith("Start")));
179227
Assert.Equal(1, eventRecords.Records.Count(rec => rec.Key.EndsWith("Stop")));
@@ -217,7 +265,12 @@ bool IsEnabled(string evnt, object arg1, object arg2)
217265

218266
using (new EventObserverAndRecorder(IsEnabled))
219267
{
220-
await new HttpClient().GetAsync("http://www.bing.com");
268+
using (var client = new HttpClient())
269+
{
270+
using (await client.GetAsync(Configuration.Http.RemoteEchoServer))
271+
{
272+
}
273+
}
221274
Assert.Equal(2, eventNumber);
222275
}
223276
}
@@ -230,7 +283,12 @@ public async Task TestIsEnabledAllOff()
230283
{
231284
using (var eventRecords = new EventObserverAndRecorder((evnt, arg1, arg2) => false))
232285
{
233-
await new HttpClient().GetAsync("http://www.bing.com");
286+
using (var client = new HttpClient())
287+
{
288+
using (await client.GetAsync(Configuration.Http.RemoteEchoServer))
289+
{
290+
}
291+
}
234292

235293
Assert.Equal(0, eventRecords.Records.Count);
236294
}
@@ -246,33 +304,43 @@ bool IsEnabled(string evnt, object arg1, object arg2)
246304
{
247305
if (evnt == "System.Net.Http.Desktop.HttpRequestOut")
248306
{
249-
return !(arg1 as WebRequest).RequestUri.ToString().Contains("bing");
307+
return (arg1 as WebRequest).RequestUri.Scheme == "https";
250308
}
251309
return true;
252310
}
253311

254312
using (var eventRecords = new EventObserverAndRecorder(IsEnabled))
255313
{
256-
await new HttpClient().GetAsync("http://www.bing.com");
257-
Assert.Equal(0, eventRecords.Records.Count);
314+
using (var client = new HttpClient())
315+
{
316+
using (await client.GetAsync(Configuration.Http.RemoteEchoServer))
317+
{
318+
Assert.Equal(0, eventRecords.Records.Count);
319+
}
258320

259-
await new HttpClient().GetAsync("http://www.microsoft.com");
260-
Assert.True(eventRecords.Records.Count > 0);
321+
using (await client.GetAsync(Configuration.Http.SecureRemoteEchoServer))
322+
{
323+
Assert.True(eventRecords.Records.Count > 0);
324+
}
325+
}
261326
}
262327
}
263328

264329
/// <summary>
265330
/// Test to make sure every event record has the right dynamic properties.
266331
/// </summary>
267332
[Fact]
268-
public void TestDynamicPropertiesOnReceiveAndResponseEvents()
333+
public async Task TestDynamicPropertiesOnReceiveAndResponseEvents()
269334
{
270335
using (var eventRecords = new EventObserverAndRecorder())
271336
{
272-
long beginTimestamp = Stopwatch.GetTimestamp();
273-
274337
// Send a random Http request to generate some events
275-
HttpResponseMessage message = new HttpClient().GetAsync("http://www.bing.com").Result;
338+
using (var client = new HttpClient())
339+
{
340+
using (await client.GetAsync(Configuration.Http.RemoteEchoServer))
341+
{
342+
}
343+
}
276344

277345
// Just make sure some events are written, to confirm we successfully subscribed to it. We should have
278346
// at least two events, one for request send, and one for response receive
@@ -313,34 +381,18 @@ public void TestMultipleConcurrentRequests()
313381
var parentActivity = new Activity("parent").Start();
314382
using (var eventRecords = new EventObserverAndRecorder())
315383
{
316-
long beginTimestamp = Stopwatch.GetTimestamp();
317-
318384
Dictionary<string, Tuple<WebRequest, WebResponse>> requestData =
319-
new Dictionary<string, Tuple<WebRequest, WebResponse>>
320-
{
321-
{"http://www.microsoft.com", null},
322-
{"http://www.bing.com", null},
323-
{"http://www.xbox.com", null},
324-
{"http://www.office.com", null},
325-
{"http://www.microsoftstore.com", null},
326-
{"http://www.msn.com", null},
327-
{"http://outlook.live.com", null},
328-
{"http://build.microsoft.com", null},
329-
{"http://azure.microsoft.com", null},
330-
{"http://www.nuget.org", null},
331-
{"http://support.microsoft.com", null},
332-
{"http://www.visualstudio.com", null},
333-
{"http://msdn.microsoft.com", null},
334-
{"http://onedrive.live.com", null},
335-
{"http://community.dynamics.com", null},
336-
{"http://login.live.com", null},
337-
{"http://www.skype.com", null},
338-
{"http://channel9.msdn.com", null}
339-
};
385+
new Dictionary<string, Tuple<WebRequest, WebResponse>>();
386+
for (int i = 0; i < 10; i++)
387+
{
388+
var uriWithRedirect =
389+
Configuration.Http.RedirectUriForDestinationUri(true, 302, new Uri($"{Configuration.Http.RemoteEchoServer}?q={i}"), 3);
390+
391+
requestData[uriWithRedirect.ToString()] = null;
392+
}
340393

341394
// Issue all requests simultaneously
342395
HttpClient httpClient = new HttpClient();
343-
344396
Dictionary<string, Task<HttpResponseMessage>> tasks = new Dictionary<string, Task<HttpResponseMessage>>();
345397

346398
CancellationTokenSource cts = new CancellationTokenSource(TimeSpan.FromSeconds(10));
@@ -350,15 +402,21 @@ public void TestMultipleConcurrentRequests()
350402
}
351403

352404
// wait up to 10 sec for all requests and suppress exceptions
353-
Task.WhenAll(tasks.Select(t => t.Value).ToArray()).ContinueWith(tt => {}).Wait();
405+
Task.WhenAll(tasks.Select(t => t.Value).ToArray()).ContinueWith(tt =>
406+
{
407+
foreach (var task in tasks)
408+
{
409+
task.Value.Result?.Dispose();
410+
}
411+
}).Wait();
354412

355413
// Examine the result. Make sure we got all successful requests.
356414

357415
// Just make sure some events are written, to confirm we successfully subscribed to it. We should have
358416
// exactly 1 Start event per request and exaclty 1 Stop event per response (if request succeeded)
359417
var successfulTasks = tasks.Where(t => t.Value.Status == TaskStatus.RanToCompletion);
360418

361-
Assert.Equal(tasks.Count(), eventRecords.Records.Count(rec => rec.Key.EndsWith("Start")));
419+
Assert.Equal(tasks.Count, eventRecords.Records.Count(rec => rec.Key.EndsWith("Start")));
362420
Assert.Equal(successfulTasks.Count(), eventRecords.Records.Count(rec => rec.Key.EndsWith("Stop")));
363421

364422
// Check to make sure: We have a WebRequest and a WebResponse for each successful request

src/System.Diagnostics.DiagnosticSource/tests/System.Diagnostics.DiagnosticSource.Tests.csproj

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,19 @@
2525
</ItemGroup>
2626
<ItemGroup Condition=" '$(TargetGroup)' == 'netfx'">
2727
<Compile Include="HttpHandlerDiagnosticListenerTests.cs" />
28-
<Compile Include="ActivityDateTimeTests.cs" />
28+
<Compile Include="ActivityDateTimeTests.cs" />
29+
<Compile Include="$(CommonTestPath)\System\Net\Configuration.cs">
30+
<Link>Common\System\Net\Configuration.cs</Link>
31+
</Compile>
32+
<Compile Include="$(CommonTestPath)\System\Net\Configuration.Http.cs">
33+
<Link>Common\System\Net\Configuration.Http.cs</Link>
34+
</Compile>
35+
<Compile Include="$(CommonTestPath)\System\IO\FileCleanupTestBase.cs">
36+
<Link>Common\System\IO\FileCleanupTestBase.cs</Link>
37+
</Compile>
38+
<Compile Include="$(CommonTestPath)\System\Diagnostics\RemoteExecutorTestBase.cs">
39+
<Link>Common\System\Diagnostics\RemoteExecutorTestBase.cs</Link>
40+
</Compile>
2941
</ItemGroup>
3042
<Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), dir.targets))\dir.targets" />
3143
</Project>

0 commit comments

Comments
 (0)