Skip to content

Commit

Permalink
[finatra/inject-app] EmbeddedApp configurable stage
Browse files Browse the repository at this point in the history
Problem: EmbeddedApp does not have a configurable guice stage which
can cause test failures.

Solution: Add configurable stage to EmbeddedApp defaulted as DEVELOPMENT

JIRA Issues: CSL-12342

Differential Revision: https://phabricator.twitter.biz/D983261
  • Loading branch information
Kostas Pagratis authored and jenkins committed Sep 27, 2022
1 parent e6c2f3c commit 117f36b
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 2 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ Note that ``RB_ID=#`` and ``PHAB_ID=#`` correspond to associated message in comm
Unreleased
----------

Changed
~~~~~~~

* inject-app: (BREAKING CHANGE) ``EmbeddedApp`` now sets com.google.inject.Stage to ``DEVELOPMENT``
``PHAB_ID=D983261``

22.7.0
------

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.twitter.inject.app

import com.google.inject.Module
import com.google.inject.Stage
import com.twitter.inject.Injector
import com.twitter.util.logging.Logging
import java.lang.annotation.Annotation
Expand All @@ -13,7 +14,16 @@ import scala.collection.JavaConverters._
*
* @param app The [[com.twitter.inject.app.App]] to be started for testing
*/
class EmbeddedApp(app: com.twitter.inject.app.App) extends BindDSL with Logging {
class EmbeddedApp(
app: com.twitter.inject.app.App,
stage: Stage = Stage.DEVELOPMENT)
extends BindDSL
with Logging {

def this(app: com.twitter.inject.app.App) =
this(app, Stage.DEVELOPMENT)

app.stage = stage

/** Note the Injector is ONLY available AFTER app.main() has been called */
lazy val injector: Injector = app.injector
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.twitter.inject.app.tests

import com.google.inject.Module
import com.google.inject.Provides
import com.google.inject.Stage
import com.twitter.inject.Test
import com.twitter.inject.TwitterModule
import com.twitter.inject.app.App
Expand All @@ -10,6 +12,7 @@ import com.twitter.inject.app.console.ConsoleWriter
import com.twitter.util.logging.Logging
import com.twitter.util.mock.Mockito
import javax.inject.Inject
import javax.inject.Singleton

class EmbeddedAppIntegrationTest extends Test with Mockito {

Expand Down Expand Up @@ -144,6 +147,15 @@ class EmbeddedAppIntegrationTest extends Test with Mockito {
assert(console.inspectOut() == "Hello, World: Part 2!\n")
assert(console.inspectErr() == "Oof!\n")
}

test("app#injector error") {
val sampleApp = new SampleApp
val app = new EmbeddedApp(sampleApp, stage = Stage.PRODUCTION)

intercept[Exception] {
app.main()
}.getCause.getMessage should be("Yikes")
}
}

object SampleAppMain extends SampleApp
Expand All @@ -162,7 +174,13 @@ class SampleApp extends App {

override val name = "sample-app"

override val modules: Seq[Module] = Seq.empty[Module]
override val modules: Seq[Module] = Seq(new TwitterModule() {
@Provides
@Singleton
def providesFoo: Integer = {
throw new Exception("Yikes")
}
})

override protected def run(): Unit = {
sampleServiceResponse = injector.instance[SampleManager].start()
Expand Down

0 comments on commit 117f36b

Please sign in to comment.