import net.pterodactylus.sone.web.notification.*
import org.hamcrest.MatcherAssert.*
import org.hamcrest.Matchers.*
+import org.junit.experimental.categories.*
import org.mockito.Mockito.*
import java.io.*
import java.util.concurrent.atomic.*
* Unit test for [SonePlugin].
*/
@Dirty
+@Category(NotParallel::class)
class SonePluginTest {
- private val sonePlugin by lazy { SonePlugin { injector } }
+ private var sonePlugin = SonePlugin { injector }
private val pluginRespirator = deepMock<PluginRespirator>()
private val node = deepMock<Node>()
private val clientCore = deepMock<NodeClientCore>()
private fun runSonePluginWithRealInjector(injectorConsumer: (Injector) -> Unit = {}): Injector {
lateinit var injector: Injector
- val sonePlugin = SonePlugin {
+ sonePlugin = SonePlugin {
Guice.createInjector(*it).also {
injector = it
injectorConsumer(it)
assertThat(getInjected(NotificationHandler::class.java), notNullValue())
}
+ @Test
+ fun `ticker shutdown is being requested`() {
+ sonePlugin.runPlugin(pluginRespirator)
+ assertThat(getInjected(TickerShutdown::class.java), notNullValue())
+ }
+
private class FirstStartListener(private val firstStartReceived: AtomicBoolean) {
@Subscribe
- fun firstStart(firstStart: FirstStart) {
+ fun firstStart(@Suppress("UNUSED_PARAMETER") firstStart: FirstStart) {
firstStartReceived.set(true)
}
}
private class ConfigNotReadListener(private val configNotReadReceiver: AtomicBoolean) {
@Subscribe
- fun configNotRead(configNotRead: ConfigNotRead) {
+ fun configNotRead(@Suppress("UNUSED_PARAMETER") configNotRead: ConfigNotRead) {
configNotReadReceiver.set(true)
}
}
private class StartupListener(private val startupReceived: () -> Unit) {
@Subscribe
- fun startup(startup: Startup) {
+ fun startup(@Suppress("UNUSED_PARAMETER") startup: Startup) {
startupReceived()
}
}
assertThat(startupReceived.get(), equalTo(true))
}
+ private class ShutdownListener(private val shutdownReceived: () -> Unit) {
+ @Subscribe
+ fun shutdown(@Suppress("UNUSED_PARAMETER") shutdown: Shutdown) {
+ shutdownReceived()
+ }
+ }
+
+ @Test
+ fun `shutdown event is sent to event bus on terminate`() {
+ val shutdownReceived = AtomicBoolean()
+ runSonePluginWithRealInjector {
+ val eventBus = it.getInstance(EventBus::class.java)
+ eventBus.register(ShutdownListener { shutdownReceived.set(true) })
+ }
+ sonePlugin.terminate()
+ assertThat(shutdownReceived.get(), equalTo(true))
+ }
+
private fun <T> getInjected(clazz: Class<T>, annotation: Annotation? = null): T? =
injected[TypeLiteral.get(clazz) to annotation] as? T