/
TimeoutTests.cs
229 lines (205 loc) · 9.88 KB
/
TimeoutTests.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
// ***********************************************************************
// Copyright (c) 2012-2018 Charlie Poole, Rob Prouse
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// ***********************************************************************
using System;
using System.Globalization;
using System.Linq;
using System.Threading;
using NUnit.Framework;
using NUnit.Framework.Interfaces;
using NUnit.Framework.Internal;
using NUnit.TestData;
using NUnit.TestUtilities;
namespace NUnit.Framework.Attributes
{
[NonParallelizable]
public class TimeoutTests : ThreadingTests
{
[Test, Timeout(500), SetCulture("fr-BE"), SetUICulture("es-BO")]
public void TestWithTimeoutRespectsCulture()
{
Assert.That(CultureInfo.CurrentCulture.Name, Is.EqualTo("fr-BE"));
Assert.That(CultureInfo.CurrentUICulture.Name, Is.EqualTo("es-BO"));
}
[Test, Timeout(500)]
public void TestWithTimeoutCurrentContextIsNotAnAdhocContext()
{
Assert.That(TestExecutionContext.CurrentContext, Is.Not.TypeOf<TestExecutionContext.AdhocContext>());
}
#if PLATFORM_DETECTION && THREAD_ABORT
[Test, Timeout(500)]
public void TestWithTimeoutRunsOnSameThread()
{
Assert.That(Thread.CurrentThread, Is.EqualTo(ParentThread));
}
[Test, Timeout(500)]
public void TestWithTimeoutRunsSetUpAndTestOnSameThread()
{
Assert.That(Thread.CurrentThread, Is.EqualTo(SetupThread));
}
[Test]
[Platform(Exclude = "Mono", Reason = "Runner hangs at end when this is run")]
public void TestTimesOutAndTearDownIsRun()
{
TimeoutFixture fixture = new TimeoutFixture();
TestSuite suite = TestBuilder.MakeFixture(fixture);
TestMethod testMethod = (TestMethod)TestFinder.Find("InfiniteLoopWith50msTimeout", suite, false);
ITestResult result = TestBuilder.RunTest(testMethod, fixture);
Assert.That(result.ResultState, Is.EqualTo(ResultState.Failure));
Assert.That(result.Message, Does.Contain("50ms"));
Assert.That(fixture.TearDownWasRun, "TearDown was not run");
}
[Test]
public void SetUpTimesOutAndTearDownIsRun()
{
TimeoutFixture fixture = new TimeoutFixtureWithTimeoutInSetUp();
TestSuite suite = TestBuilder.MakeFixture(fixture);
TestMethod testMethod = (TestMethod)TestFinder.Find("Test1", suite, false);
ITestResult result = TestBuilder.RunTest(testMethod, fixture);
Assert.That(result.ResultState, Is.EqualTo(ResultState.Failure));
Assert.That(result.Message, Does.Contain("50ms"));
Assert.That(fixture.TearDownWasRun, "TearDown was not run");
}
[Test]
[Platform(Exclude = "Mono", Reason = "Test never aborts on Mono (tested 5.4–5.12)")]
public void TearDownTimesOutAndNoFurtherTearDownIsRun()
{
TimeoutFixture fixture = new TimeoutFixtureWithTimeoutInTearDown();
TestSuite suite = TestBuilder.MakeFixture(fixture);
TestMethod testMethod = (TestMethod)TestFinder.Find("Test1", suite, false);
ITestResult result = TestBuilder.RunTest(testMethod, fixture);
Assert.That(result.ResultState, Is.EqualTo(ResultState.Failure));
Assert.That(result.Message, Does.Contain("50ms"));
Assert.That(fixture.TearDownWasRun, "Base TearDown should not have been run but was");
}
[Test]
[Platform(Exclude = "Mono", Reason = "Runner hangs at end when this is run")]
public void TimeoutCanBeSetOnTestFixture()
{
ITestResult suiteResult = TestBuilder.RunTestFixture(typeof(TimeoutFixtureWithTimeoutOnFixture));
Assert.That(suiteResult.ResultState, Is.EqualTo(ResultState.ChildFailure));
Assert.That(suiteResult.Message, Is.EqualTo(TestResult.CHILD_ERRORS_MESSAGE));
Assert.That(suiteResult.ResultState.Site, Is.EqualTo(FailureSite.Child));
ITestResult result = TestFinder.Find("Test2WithInfiniteLoop", suiteResult, false);
Assert.That(result.ResultState, Is.EqualTo(ResultState.Failure));
Assert.That(result.Message, Does.Contain("50ms"));
}
[Test]
public void TestTimeoutNotElapsed()
{
TimeoutTestCaseFixture fixture = new TimeoutTestCaseFixture();
TestSuite suite = TestBuilder.MakeFixture(fixture);
TestMethod testMethod = (TestMethod)TestFinder.Find("TestTimeOutNotElapsed", suite, false);
ITestResult result = TestBuilder.RunTest(testMethod, fixture);
Assert.That(result.ResultState, Is.EqualTo(ResultState.Success));
}
[Test]
public void TestTimeoutElapsed()
{
TimeoutTestCaseFixture fixture = new TimeoutTestCaseFixture();
TestSuite suite = TestBuilder.MakeFixture(fixture);
TestMethod testMethod = (TestMethod)TestFinder.Find("TestTimeOutElapsed", suite, false);
ITestResult result = TestBuilder.RunTest(testMethod, fixture);
Assert.That(result.ResultState, Is.EqualTo(ResultState.Failure));
Assert.That(result.Message, Does.Contain("100ms"));
}
[Explicit("Tests that demonstrate Timeout failure")]
public class ExplicitTests
{
[Test, Timeout(50)]
public void TestTimesOut()
{
while (true) ;
}
[Test, Timeout(50), RequiresThread]
public void TestTimesOutUsingRequiresThread()
{
while (true) ;
}
[Test, Timeout(50), Apartment(ApartmentState.STA)]
public void TestTimesOutInSTA()
{
while (true) ;
}
// TODO: The test in TimeoutTestCaseFixture work as expected when run
// directly by NUnit. It's only when run via TestBuilder as a second
// level test that the result is incorrect. We need to fix this.
[Test]
public void TestTimeOutTestCaseWithOutElapsed()
{
TimeoutTestCaseFixture fixture = new TimeoutTestCaseFixture();
TestSuite suite = TestBuilder.MakeFixture(fixture);
ParameterizedMethodSuite methodSuite = (ParameterizedMethodSuite)TestFinder.Find("TestTimeOutTestCase", suite, false);
ITestResult result = TestBuilder.RunTest(methodSuite, fixture);
Assert.That(result.ResultState, Is.EqualTo(ResultState.Failure), "Suite result");
Assert.That(result.Children.ToArray()[0].ResultState, Is.EqualTo(ResultState.Success), "First test");
Assert.That(result.Children.ToArray()[1].ResultState, Is.EqualTo(ResultState.Failure), "Second test");
}
}
[Test, Platform("Win")]
public void TimeoutWithMessagePumpShouldAbort()
{
ITestResult result = TestBuilder.RunTest(
TestBuilder.MakeTestFromMethod(typeof(TimeoutFixture), nameof(TimeoutFixture.TimeoutWithMessagePumpShouldAbort)),
new TimeoutFixture());
Assert.That(result.ResultState, Is.EqualTo(ResultState.Failure));
Assert.That(result.Message, Is.EqualTo("Test exceeded Timeout value of 500ms"));
}
#endif
#if !THREAD_ABORT
[Timeout(50)]
public void TestTimeoutAndReportsTimeoutFailure()
{
Thread.Sleep(Timeout.Infinite);
}
[Test, Timeout(100)]
public void TestTimeoutDoesNotStopCompletion()
{
Thread.Sleep(20);
Assert.True(true);
}
[Timeout(100)]
public void TestTimeoutWhichThrowsTestException()
{
throw new ArgumentException($"{nameof(ArgumentException)} was thrown.");
}
[Test]
public void TestTimesOut()
{
var testMethod = TestBuilder.MakeTestCase(GetType(), nameof(TestTimeoutAndReportsTimeoutFailure));
var testResult = TestBuilder.RunTest(testMethod, this);
Assert.That(testResult?.ResultState.Status, Is.EqualTo(TestStatus.Failed));
Assert.That(testResult?.ResultState.Site, Is.EqualTo(FailureSite.Test));
Assert.That(testResult?.ResultState.Label, Is.EqualTo("Test exceeded Timeout value 50ms."));
}
[Test]
public void TestTimeoutWithExceptionThrown()
{
var testMethod = TestBuilder.MakeTestCase(GetType(), nameof(TestTimeoutWhichThrowsTestException));
var testResult = TestBuilder.RunTest(testMethod, this);
Assert.That(testResult?.ResultState.Status, Is.EqualTo(TestStatus.Failed));
Assert.That(testResult?.ResultState.Site, Is.EqualTo(FailureSite.Test));
Assert.That(testResult?.ResultState.Label, Is.EqualTo("Error"));
}
#endif
}
}