Skip to content

Commit ad1be79

Browse files
mmollaverdisvc-squareup-copybara
authored andcommitted
Make it possible to reuse injector in misk test when
startService is false GitOrigin-RevId: 36cb8dbb1df37f1638e985249bfa8a6235216da3
1 parent 401782a commit ad1be79

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

misk-testing/src/main/kotlin/misk/testing/MiskTestExtension.kt

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,23 +89,27 @@ internal class MiskTestExtension : BeforeEachCallback, AfterEachCallback {
8989
}
9090

9191
class StartServicesBeforeEach @Inject constructor() : BeforeEachCallback {
92-
@Inject lateinit var serviceManager: ServiceManager
92+
@com.google.inject.Inject(optional = true) var serviceManager: ServiceManager? = null
9393

9494
override fun beforeEach(context: ExtensionContext) {
9595
if (context.startService()) {
96+
if (serviceManager == null) {
97+
throw IllegalStateException("This test is configured with `startService` set to true, " +
98+
"but no ServiceManager is bound. Did you forget to install MiskTestingServiceModule?")
99+
}
96100
if (context.reuseInjector() && runningServices.contains(context.getActionTestModules())) {
97101
return
98102
}
99103
try {
100104
try {
101-
serviceManager.startAsync().awaitHealthy(60, TimeUnit.SECONDS)
105+
serviceManager!!.startAsync().awaitHealthy(60, TimeUnit.SECONDS)
102106
} catch (e: Exception) {
103107
if (context.reuseInjector()) {
104108
// The `ServiceManager` requires services to be in a NEW state when starting them,
105109
// so if services fail to start, we need to stop them and remove the injector from the cache,
106110
// so that the next test can start fresh.
107111
try {
108-
serviceManager.stop(context)
112+
serviceManager!!.stop(context)
109113
} catch (stopError: Exception) {
110114
e.addSuppressed(stopError)
111115
}
@@ -117,7 +121,7 @@ internal class MiskTestExtension : BeforeEachCallback, AfterEachCallback {
117121
if (context.reuseInjector()) {
118122
Runtime.getRuntime().addShutdownHook(
119123
thread(start = false) {
120-
serviceManager.stop(context)
124+
serviceManager!!.stop(context)
121125
}
122126
)
123127
}

0 commit comments

Comments
 (0)