From 0d703d052668e11a3fb419995568c8c9fcf70c8d Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Wed, 11 Dec 2019 18:58:25 +0100 Subject: [PATCH] =?utf8?q?=E2=99=BB=EF=B8=8F=20Move=20test=20executor=20to?= =?utf8?q?=20its=20own=20file?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../sone/web/notification/SoneLockedHandlerTest.kt | 24 ++++---------- .../pterodactylus/sone/web/notification/Testing.kt | 37 ++++++++++++++++++++++ 2 files changed, 43 insertions(+), 18 deletions(-) create mode 100644 src/test/kotlin/net/pterodactylus/sone/web/notification/Testing.kt diff --git a/src/test/kotlin/net/pterodactylus/sone/web/notification/SoneLockedHandlerTest.kt b/src/test/kotlin/net/pterodactylus/sone/web/notification/SoneLockedHandlerTest.kt index 191168e..86e456d 100644 --- a/src/test/kotlin/net/pterodactylus/sone/web/notification/SoneLockedHandlerTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/web/notification/SoneLockedHandlerTest.kt @@ -62,14 +62,14 @@ class SoneLockedHandlerTest { @Test fun `notification is added to notification manager from command`() { eventBus.post(SoneLockedEvent(sone)) - executor.scheduledDelay.single().command.run() + executor.scheduleds.single().command.run() assertThat(notificationManager.notifications, contains(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))) } } @@ -78,7 +78,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 @@ -91,7 +91,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.run() eventBus.post(SoneUnlockedEvent(sone)) assertThat(notification.elements, emptyIterable()) } @@ -100,28 +100,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() - - override fun schedule(command: Runnable, delay: Long, unit: TimeUnit): ScheduledFuture<*> = - super.schedule(command, delay, unit) - .also { scheduledDelay += Scheduled(command, delay, unit, it) } - -} diff --git a/src/test/kotlin/net/pterodactylus/sone/web/notification/Testing.kt b/src/test/kotlin/net/pterodactylus/sone/web/notification/Testing.kt new file mode 100644 index 0000000..12088d0 --- /dev/null +++ b/src/test/kotlin/net/pterodactylus/sone/web/notification/Testing.kt @@ -0,0 +1,37 @@ +/** + * Sone - Testing.kt - Copyright © 2019 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 + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package net.pterodactylus.sone.web.notification + +import java.util.concurrent.* + +/** Information about a scheduled runnable. */ +data class Scheduled(val command: Runnable, val delay: Long, val timeUnit: TimeUnit, val future: ScheduledFuture<*>) + +/** + * [ScheduledThreadPoolExecutor] extension that stores parameters and return + * values for the [ScheduledThreadPoolExecutor.schedule] method. + */ +class TestScheduledThreadPoolExecutor : ScheduledThreadPoolExecutor(1) { + + val scheduleds = mutableListOf() + + override fun schedule(command: Runnable, delay: Long, unit: TimeUnit): ScheduledFuture<*> = + super.schedule(command, delay, unit) + .also { scheduleds += Scheduled(command, delay, unit, it) } + +} -- 2.7.4