📄 Update year in file headers
[Sone.git] / src / test / kotlin / net / pterodactylus / sone / web / notification / SoneLockedHandlerTest.kt
index 2daf5c9..4003dbb 100644 (file)
@@ -1,5 +1,5 @@
 /**
- * Sone - SoneLockedHandlerTest.kt - Copyright Â© 2019 David â€˜Bombe’ Roden
+ * Sone - SoneLockedHandlerTest.kt - Copyright Â© 2019–2020 David â€˜Bombe’ Roden
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -22,6 +22,7 @@ import net.pterodactylus.sone.core.event.*
 import net.pterodactylus.sone.data.*
 import net.pterodactylus.sone.data.impl.*
 import net.pterodactylus.sone.notify.*
+import net.pterodactylus.sone.utils.*
 import net.pterodactylus.util.notify.*
 import net.pterodactylus.util.template.*
 import org.hamcrest.MatcherAssert.*
@@ -44,30 +45,32 @@ class SoneLockedHandlerTest {
                SoneLockedHandler(notificationManager, notification, executor).also(eventBus::register)
        }
 
+       @AfterTest
+       fun shutdownExecutor() = executor.shutdown()
+
        @Test
-       fun `notification is not added during the first five minutes`() {
+       fun `notification is not added before the command is run`() {
                eventBus.post(SoneLockedEvent(sone))
                assertThat(notificationManager.notifications, emptyIterable())
        }
 
        @Test
-       fun `sone is added to notification from command`() {
+       fun `sone is added to notification immediately`() {
                eventBus.post(SoneLockedEvent(sone))
-               executor.scheduledDelay.single().command.run()
                assertThat(notification.elements, contains(sone))
        }
 
        @Test
        fun `notification is added to notification manager from command`() {
                eventBus.post(SoneLockedEvent(sone))
-               executor.scheduledDelay.single().command.run()
+               executor.scheduleds.single().command()
                assertThat(notificationManager.notifications, contains<Any>(notification))
        }
 
        @Test
        fun `command is registered with a delay of five minutes`() {
                eventBus.post(SoneLockedEvent(sone))
-               with(executor.scheduledDelay.single()) {
+               with(executor.scheduleds.single()) {
                        assertThat(timeUnit.toNanos(delay), equalTo(TimeUnit.MINUTES.toNanos(5)))
                }
        }
@@ -76,7 +79,7 @@ class SoneLockedHandlerTest {
        fun `unlocking sone after locking will cancel the future`() {
                eventBus.post(SoneLockedEvent(sone))
                eventBus.post(SoneUnlockedEvent(sone))
-               assertThat(executor.scheduledDelay.first().future.isCancelled, equalTo(true))
+               assertThat(executor.scheduleds.first().future.isCancelled, equalTo(true))
        }
 
        @Test
@@ -89,7 +92,7 @@ class SoneLockedHandlerTest {
        @Test
        fun `unlocking sone after showing the notification will remove the sone from the notification`() {
                eventBus.post(SoneLockedEvent(sone))
-               executor.scheduledDelay.single().command.run()
+               executor.scheduleds.single().command()
                eventBus.post(SoneUnlockedEvent(sone))
                assertThat(notification.elements, emptyIterable())
        }
@@ -98,28 +101,16 @@ class SoneLockedHandlerTest {
        fun `locking two sones will cancel the first command`() {
                eventBus.post(SoneLockedEvent(sone))
                eventBus.post(SoneLockedEvent(sone))
-               assertThat(executor.scheduledDelay.first().future.isCancelled, equalTo(true))
+               assertThat(executor.scheduleds.first().future.isCancelled, equalTo(true))
        }
 
        @Test
        fun `locking two sones will schedule a second command`() {
                eventBus.post(SoneLockedEvent(sone))
                eventBus.post(SoneLockedEvent(sone))
-               assertThat(executor.scheduledDelay[1], notNullValue())
+               assertThat(executor.scheduleds[1], notNullValue())
        }
 
 }
 
 private val sone: Sone = IdOnlySone("sone")
-
-private data class Scheduled(val command: Runnable, val delay: Long, val timeUnit: TimeUnit, val future: ScheduledFuture<*>)
-
-private class TestScheduledThreadPoolExecutor : ScheduledThreadPoolExecutor(1) {
-
-       val scheduledDelay = mutableListOf<Scheduled>()
-
-       override fun schedule(command: Runnable, delay: Long, unit: TimeUnit): ScheduledFuture<*> =
-                       super.schedule(command, delay, unit)
-                                       .also { scheduledDelay += Scheduled(command, delay, unit, it) }
-
-}