Skip to content
This repository was archived by the owner on Dec 19, 2018. It is now read-only.

Commit b20e587

Browse files
committed
Added tests
1 parent d04421d commit b20e587

File tree

1 file changed

+198
-0
lines changed

1 file changed

+198
-0
lines changed

test/Microsoft.AspNetCore.Hosting.Tests/Internal/HostingEventSourceTests.cs

Lines changed: 198 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System;
55
using System.Diagnostics.Tracing;
66
using System.Reflection;
7+
using Microsoft.AspNetCore.Http;
78
using Xunit;
89

910
namespace Microsoft.AspNetCore.Hosting.Internal
@@ -25,5 +26,202 @@ public void MatchesNameAndGuid()
2526
Assert.Equal(Guid.Parse("9e620d2a-55d4-5ade-deb7-c26046d245a8"), EventSource.GetGuid(eventSourceType));
2627
Assert.NotEmpty(EventSource.GenerateManifest(eventSourceType, "assemblyPathToIncludeInManifest"));
2728
}
29+
30+
[Fact]
31+
public void HostStart()
32+
{
33+
// Arrange
34+
var expectedEventId = 1;
35+
var eventListener = new TestEventListener(expectedEventId);
36+
var hostingEventSource = HostingEventSource.Log;
37+
eventListener.EnableEvents(hostingEventSource, EventLevel.Informational);
38+
39+
// Act
40+
hostingEventSource.HostStart();
41+
42+
// Assert
43+
var eventData = eventListener.EventData;
44+
Assert.NotNull(eventData);
45+
Assert.Equal(expectedEventId, eventData.EventId);
46+
#if NETCOREAPP1_1
47+
Assert.Equal("HostStart", eventData.EventName);
48+
#endif
49+
Assert.Equal(EventLevel.Informational, eventData.Level);
50+
Assert.Same(hostingEventSource, eventData.EventSource);
51+
Assert.Null(eventData.Message);
52+
Assert.Empty(eventData.Payload);
53+
}
54+
55+
[Fact]
56+
public void HostStop()
57+
{
58+
// Arrange
59+
var expectedEventId = 2;
60+
var eventListener = new TestEventListener(expectedEventId);
61+
var hostingEventSource = HostingEventSource.Log;
62+
eventListener.EnableEvents(hostingEventSource, EventLevel.Informational);
63+
64+
// Act
65+
hostingEventSource.HostStop();
66+
67+
// Assert
68+
var eventData = eventListener.EventData;
69+
Assert.NotNull(eventData);
70+
Assert.Equal(expectedEventId, eventData.EventId);
71+
#if NETCOREAPP1_1
72+
Assert.Equal("HostStop", eventData.EventName);
73+
#endif
74+
Assert.Equal(EventLevel.Informational, eventData.Level);
75+
Assert.Same(hostingEventSource, eventData.EventSource);
76+
Assert.Null(eventData.Message);
77+
Assert.Empty(eventData.Payload);
78+
}
79+
80+
public static TheoryData<DefaultHttpContext, string[]> RequestStartData
81+
{
82+
get
83+
{
84+
var variations = new TheoryData<DefaultHttpContext, string[]>();
85+
86+
var context = new DefaultHttpContext();
87+
context.Request.Method = "GET";
88+
context.Request.Path = "/Home/Index";
89+
variations.Add(
90+
context,
91+
new string[]
92+
{
93+
"GET",
94+
"/Home/Index"
95+
});
96+
97+
context = new DefaultHttpContext();
98+
context.Request.Method = "POST";
99+
context.Request.Path = "/";
100+
variations.Add(
101+
context,
102+
new string[]
103+
{
104+
"POST",
105+
"/"
106+
});
107+
108+
return variations;
109+
}
110+
}
111+
112+
[Theory]
113+
[MemberData(nameof(RequestStartData))]
114+
public void RequestStart(DefaultHttpContext httpContext, string[] expected)
115+
{
116+
// Arrange
117+
var expectedEventId = 3;
118+
var eventListener = new TestEventListener(expectedEventId);
119+
var hostingEventSource = HostingEventSource.Log;
120+
eventListener.EnableEvents(hostingEventSource, EventLevel.Informational);
121+
122+
// Act
123+
hostingEventSource.RequestStart(httpContext.Request.Method, httpContext.Request.Path);
124+
125+
// Assert
126+
var eventData = eventListener.EventData;
127+
Assert.NotNull(eventData);
128+
Assert.Equal(expectedEventId, eventData.EventId);
129+
#if NETCOREAPP1_1
130+
Assert.Equal("RequestStart", eventData.EventName);
131+
#endif
132+
Assert.Equal(EventLevel.Informational, eventData.Level);
133+
Assert.Same(hostingEventSource, eventData.EventSource);
134+
Assert.Null(eventData.Message);
135+
136+
var payloadList = eventData.Payload;
137+
Assert.Equal(expected.Length, payloadList.Count);
138+
for (var i = 0; i < expected.Length; i++)
139+
{
140+
Assert.Equal(expected[i], payloadList[i]);
141+
}
142+
}
143+
144+
[Fact]
145+
public void RequestStop()
146+
{
147+
// Arrange
148+
var expectedEventId = 4;
149+
var eventListener = new TestEventListener(expectedEventId);
150+
var hostingEventSource = HostingEventSource.Log;
151+
eventListener.EnableEvents(hostingEventSource, EventLevel.Informational);
152+
153+
// Act
154+
hostingEventSource.RequestStop();
155+
156+
// Assert
157+
var eventData = eventListener.EventData;
158+
Assert.Equal(expectedEventId, eventData.EventId);
159+
#if NETCOREAPP1_1
160+
Assert.Equal("RequestStop", eventData.EventName);
161+
#endif
162+
Assert.Equal(EventLevel.Informational, eventData.Level);
163+
Assert.Same(hostingEventSource, eventData.EventSource);
164+
Assert.Null(eventData.Message);
165+
Assert.Equal(0, eventData.Payload.Count);
166+
}
167+
168+
[Fact]
169+
public void UnhandledException()
170+
{
171+
// Arrange
172+
var expectedEventId = 5;
173+
var eventListener = new TestEventListener(expectedEventId);
174+
var hostingEventSource = HostingEventSource.Log;
175+
eventListener.EnableEvents(hostingEventSource, EventLevel.Informational);
176+
177+
// Act
178+
hostingEventSource.UnhandledException();
179+
180+
// Assert
181+
var eventData = eventListener.EventData;
182+
Assert.Equal(expectedEventId, eventData.EventId);
183+
#if NETCOREAPP1_1
184+
Assert.Equal("UnhandledException", eventData.EventName);
185+
#endif
186+
Assert.Equal(EventLevel.Error, eventData.Level);
187+
Assert.Same(hostingEventSource, eventData.EventSource);
188+
Assert.Null(eventData.Message);
189+
Assert.Equal(0, eventData.Payload.Count);
190+
}
191+
192+
private static Exception GetException()
193+
{
194+
try
195+
{
196+
throw new InvalidOperationException("An invalid operation has occurred");
197+
}
198+
catch (Exception ex)
199+
{
200+
return ex;
201+
}
202+
}
203+
204+
private class TestEventListener : EventListener
205+
{
206+
private readonly int _eventId;
207+
208+
public TestEventListener(int eventId)
209+
{
210+
_eventId = eventId;
211+
}
212+
213+
public EventWrittenEventArgs EventData { get; private set; }
214+
215+
protected override void OnEventWritten(EventWrittenEventArgs eventData)
216+
{
217+
// The tests here run in parallel and since the single publisher instance (HostingEventingSource)
218+
// notifies all listener instances in these tests, capture the EventData that a test is explicitly
219+
// looking for and not give back other tests' data.
220+
if (eventData.EventId == _eventId)
221+
{
222+
EventData = eventData;
223+
}
224+
}
225+
}
28226
}
29227
}

0 commit comments

Comments
 (0)