-
Notifications
You must be signed in to change notification settings - Fork 4.8k
/
LoggingTest.cs
131 lines (117 loc) · 5.36 KB
/
LoggingTest.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Collections.Concurrent;
using System.Diagnostics.Tracing;
using System.Linq;
using System.Net.Sockets;
using System.Threading.Tasks;
using Microsoft.DotNet.XUnitExtensions;
using Xunit;
namespace System.Net.NameResolution.Tests
{
using Configuration = System.Net.Test.Common.Configuration;
[Collection("NoParallelTests")]
public class LoggingTest
{
[Fact]
public static void EventSource_ExistsWithCorrectId()
{
Type esType = typeof(Dns).Assembly.GetType("System.Net.NetEventSource", throwOnError: true, ignoreCase: false);
Assert.NotNull(esType);
Assert.Equal("Private.InternalDiagnostics.System.Net.NameResolution", EventSource.GetName(esType));
Assert.Equal(Guid.Parse("460a591a-715b-5647-5264-944bef811147"), EventSource.GetGuid(esType));
Assert.NotEmpty(EventSource.GenerateManifest(esType, "assemblyPathToIncludeInManifest"));
}
[ConditionalFact]
public void GetHostEntry_InvalidHost_LogsError()
{
using (var listener = new TestEventListener("Private.InternalDiagnostics.System.Net.NameResolution", EventLevel.Error))
{
var events = new ConcurrentQueue<EventWrittenEventArgs>();
listener.RunWithCallback(ev => events.Enqueue(ev), () =>
{
try
{
Dns.GetHostEntry(Configuration.Sockets.InvalidHost);
throw new SkipTestException("GetHostEntry should fail but it did not.");
}
catch (SocketException e) when (e.SocketErrorCode == SocketError.HostNotFound)
{
}
catch (Exception e)
{
throw new SkipTestException($"GetHostEntry failed unexpectedly: {e.Message}");
}
});
Assert.True(events.Count > 0, "events.Count should be > 0");
foreach (EventWrittenEventArgs ev in events)
{
Assert.True(ev.Payload.Count >= 3);
Assert.NotNull(ev.Payload[0]);
Assert.NotNull(ev.Payload[1]);
Assert.NotNull(ev.Payload[2]);
}
}
}
[ConditionalFact]
[PlatformSpecific(~TestPlatforms.Windows)] // Unreliable on Windows.
public async Task GetHostEntryAsync_InvalidHost_LogsError()
{
using (var listener = new TestEventListener("Private.InternalDiagnostics.System.Net.NameResolution", EventLevel.Error))
{
var events = new ConcurrentQueue<EventWrittenEventArgs>();
await listener.RunWithCallbackAsync(ev => events.Enqueue(ev), async () =>
{
try
{
await Dns.GetHostEntryAsync(Configuration.Sockets.InvalidHost).ConfigureAwait(false);
throw new SkipTestException("GetHostEntryAsync should fail but it did not.");
}
catch (SocketException e) when (e.SocketErrorCode == SocketError.HostNotFound)
{
// Wait a bit to let the event source write it's log
await Task.Delay(100).ConfigureAwait(false);
}
catch (Exception e)
{
throw new SkipTestException($"GetHostEntryAsync failed unexpectedly: {e.Message}");
}
}).ConfigureAwait(false);
Assert.True(events.Count > 0, "events.Count should be > 0");
foreach (EventWrittenEventArgs ev in events)
{
Assert.True(ev.Payload.Count >= 3);
Assert.NotNull(ev.Payload[0]);
Assert.NotNull(ev.Payload[1]);
Assert.NotNull(ev.Payload[2]);
}
}
}
[ConditionalFact]
public void GetHostEntry_ValidName_NoErrors()
{
using (var listener = new TestEventListener("Private.InternalDiagnostics.System.Net.NameResolution", EventLevel.Verbose))
{
var events = new ConcurrentQueue<EventWrittenEventArgs>();
listener.RunWithCallback(ev => events.Enqueue(ev), () =>
{
try
{
Dns.GetHostEntryAsync("localhost").GetAwaiter().GetResult();
Dns.GetHostEntryAsync(IPAddress.Loopback).GetAwaiter().GetResult();
Dns.GetHostEntry("localhost");
Dns.GetHostEntry(IPAddress.Loopback);
}
catch (Exception e)
{
throw new SkipTestException($"Localhost lookup failed unexpectedly: {e.Message}");
}
});
// We get some traces.
Assert.True(events.Count() > 0);
// No errors or warning for successful query.
Assert.True(events.Count(ev => (int)ev.Level > (int)EventLevel.Informational) == 0);
}
}
}
}