-
Notifications
You must be signed in to change notification settings - Fork 406
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
inject-core: add 'runAfterAll' hook to IntegrationTestMixin
Problem The `EmbeddedTwitterServer` has a `closeOnExit` method for registering logical hooks for cleaning up test resources when the server shuts down, but we do not have a general utility for registering shutdown logic that is detached from the `EmbeddedTwitterServer` lifecycle *in a simple, consistent manner*. The current solution is to override ScalaTest `afterAll` directly, which comes with its own pitfalls. This can result in `super.afterAll` not being called by an inheritor and not handling exceptions as part of clean-up can result in unexpected TestSuite failures. Solution Introduce a `runAfterAll` method that can be called to register shutdown logic that will not fail the `TestSuite` as a result of an exception during clean-up. It should be highlighted that we do not prevent anyone from explicitly overriding `afterAll` for logic that should result in a test failure as a result of an exception. JIRA Issues: CSL-11136 Differential Revision: https://phabricator.twitter.biz/D707939
- Loading branch information
Kostas Pagratis
authored and
jenkins
committed
Nov 9, 2021
1 parent
61bc153
commit 42c17b8
Showing
4 changed files
with
127 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
...nject-server/src/test/scala/com/twitter/inject/server/tests/FeatureTestAfterAllTest.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package com.twitter.inject.server.tests | ||
|
||
import com.twitter.inject.server.EmbeddedTwitterServer | ||
import com.twitter.inject.server.FeatureTest | ||
import com.twitter.inject.server.TwitterServer | ||
|
||
/** Test the `afterAll`` hooks of the [[FeatureTest]] trait */ | ||
class FeatureTestAfterAllTest extends FeatureTest { | ||
|
||
private[this] var hasAfterAllRun = false | ||
private[this] var hasAfterAllSuppressedException = false | ||
runAfterAll { hasAfterAllRun = true } | ||
runAfterAll { throw new IllegalStateException("This shouldn't block other 'afterAll' hooks") } | ||
runAfterAll { hasAfterAllSuppressedException = true } | ||
|
||
override val server: EmbeddedTwitterServer = | ||
new EmbeddedTwitterServer( | ||
twitterServer = new TwitterServer {}, | ||
disableTestLogging = true | ||
).bind[String].toInstance("helloworld") | ||
|
||
/** | ||
* Explicitly start the server before all tests, close will be attempted by | ||
* [[com.twitter.inject.server.FeatureTestMixin]] in `afterAll`. | ||
*/ | ||
override def beforeAll(): Unit = { | ||
server.start() | ||
} | ||
|
||
/** | ||
* We explicitly verify that we have run our `afterAll` and custom hooks. | ||
* If any of these assertions fails, a [[org.scalatest.exceptions.TestFailedException]] | ||
* will be thrown and the test will be considered a failure. | ||
*/ | ||
override def afterAll(): Unit = { | ||
assert(hasAfterAllRun == false) | ||
assert(hasAfterAllSuppressedException == false) | ||
assert(server.closed == false) | ||
super.afterAll() // we run all of the `afterAll` hooks here | ||
assert(hasAfterAllRun == true) | ||
assert(hasAfterAllSuppressedException) | ||
assert(server.closed) | ||
} | ||
|
||
test("TwitterServer#starts up") { | ||
server.assertHealthy() | ||
} | ||
|
||
} |