Skip to content

Commit 913a848

Browse files
One more try in test teardown for avoiding hiding exception...
1 parent 6ae4a1b commit 913a848

File tree

1 file changed

+25
-12
lines changed

1 file changed

+25
-12
lines changed

src/NHibernate.Test/TestCase.cs

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -148,31 +148,44 @@ public void TearDown()
148148
{
149149
var testResult = TestContext.CurrentContext.Result;
150150
var fail = false;
151+
var testOwnTearDownDone = false;
151152
string badCleanupMessage = null;
152153
try
153154
{
154155
try
155156
{
156157
OnTearDown();
158+
testOwnTearDownDone = true;
157159
}
158160
finally
159161
{
160-
var wereClosed = _sessionFactory.CheckSessionsWereClosed();
161-
var wasCleaned = CheckDatabaseWasCleaned();
162-
var wereConnectionsClosed = CheckConnectionsWereClosed();
163-
fail = !wereClosed || !wasCleaned || !wereConnectionsClosed;
164-
165-
if (fail)
162+
try
166163
{
167-
badCleanupMessage = "Test didn't clean up after itself. session closed: " + wereClosed + "; database cleaned: " +
168-
wasCleaned
169-
+ "; connection closed: " + wereConnectionsClosed;
170-
if (testResult != null && testResult.Outcome.Status == TestStatus.Failed)
164+
var wereClosed = _sessionFactory.CheckSessionsWereClosed();
165+
var wasCleaned = CheckDatabaseWasCleaned();
166+
var wereConnectionsClosed = CheckConnectionsWereClosed();
167+
fail = !wereClosed || !wasCleaned || !wereConnectionsClosed;
168+
169+
if (fail)
171170
{
172-
// Avoid hiding a test failure (asserts are usually not hidden, but other exception would be).
173-
badCleanupMessage = GetCombinedFailureMessage(testResult, badCleanupMessage, null);
171+
badCleanupMessage = "Test didn't clean up after itself. session closed: " + wereClosed + "; database cleaned: " +
172+
wasCleaned
173+
+ "; connection closed: " + wereConnectionsClosed;
174+
if (testResult != null && testResult.Outcome.Status == TestStatus.Failed)
175+
{
176+
// Avoid hiding a test failure (asserts are usually not hidden, but other exception would be).
177+
badCleanupMessage = GetCombinedFailureMessage(testResult, badCleanupMessage, null);
178+
}
174179
}
175180
}
181+
catch (Exception ex)
182+
{
183+
if (testOwnTearDownDone)
184+
throw;
185+
186+
// Do not hide the test own teardown failure.
187+
log.Error("TearDown cleanup failure, while test own teardown has failed. Logging cleanup failure", ex);
188+
}
176189
}
177190
}
178191
catch (Exception ex)

0 commit comments

Comments
 (0)