diff --git a/README.md b/README.md index 8c25fe8..7c40458 100644 --- a/README.md +++ b/README.md @@ -164,7 +164,8 @@ the issue you want to contribute to before starting to work on it. ### Supporting ❤️ If you found this library helpful, you can support me by giving a small tip -via [GitHub Sponsors](https://github.com/sponsors/starry-shivam) and/or joing the list of [stargazers](https://github.com/starry-shivam/KtScheduler/stargazers) by leaving a star! 🌟 +via [GitHub Sponsors](https://github.com/sponsors/starry-shivam) and/or joing the list +of [stargazers](https://github.com/starry-shivam/KtScheduler/stargazers) by leaving a star! 🌟 ------ diff --git a/build.gradle.kts b/build.gradle.kts index d1eb6d4..0b8395e 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -3,6 +3,7 @@ import javax.xml.parsers.DocumentBuilderFactory plugins { kotlin("jvm") version "2.0.0" id("org.jetbrains.kotlinx.kover") version "0.8.1" + id("org.jetbrains.dokka") version "1.9.20" `maven-publish` } @@ -38,10 +39,35 @@ publishing { artifactId = "ktscheduler" version = version.toString() from(components["java"]) + + pom { + name.set("KtScheduler") + description.set("Coroutine-based task/job scheduler for Kotlin.") + url.set("https://github.com/Pool-Of-Tears/KtScheduler") + + licenses { + license { + name.set("Apache-2.0") + url.set("https://www.apache.org/licenses/LICENSE-2.0.txt") + } + } + } + + // Add Dokka and Javadoc artifacts. + artifact(tasks.dokkaJavadoc.get().outputDirectory) + artifact(tasks.dokkaHtml.get().outputDirectory) } } } +tasks.dokkaHtml.configure { + outputDirectory.set(buildDir.resolve("dokka")) +} + +tasks.dokkaJavadoc.configure { + outputDirectory.set(buildDir.resolve("javadoc")) +} + // Print line coverage percentage to console so we can generate badge in CI. tasks.register("printLineCoverage") { group = "verification" diff --git a/src/main/kotlin/scheduler/KtScheduler.kt b/src/main/kotlin/scheduler/KtScheduler.kt index 0941967..7638899 100644 --- a/src/main/kotlin/scheduler/KtScheduler.kt +++ b/src/main/kotlin/scheduler/KtScheduler.kt @@ -109,6 +109,10 @@ class KtScheduler( */ override fun shutdown() { logger.info("Shutting down scheduler") + // Check if the scheduler is running or not. + if (!::coroutineScope.isInitialized || !coroutineScope.isActive) { + throw IllegalStateException("Scheduler is not running") + } coroutineScope.cancel() logger.info("Scheduler shut down") } diff --git a/src/test/kotlin/KtSchedulerTest.kt b/src/test/kotlin/KtSchedulerTest.kt index fbf3f0c..d596a03 100644 --- a/src/test/kotlin/KtSchedulerTest.kt +++ b/src/test/kotlin/KtSchedulerTest.kt @@ -32,6 +32,7 @@ import java.time.ZonedDateTime import kotlin.test.assertEquals import kotlin.test.assertNotNull import kotlin.test.assertNull +import kotlin.test.fail @OptIn(ExperimentalCoroutinesApi::class) class KtSchedulerTest { @@ -42,12 +43,44 @@ class KtSchedulerTest { scheduler.start() try { scheduler.start() + fail("Should throw IllegalStateException") } catch (e: IllegalStateException) { assertEquals("Scheduler is already running", e.message) } scheduler.shutdown() } + @Test + fun `scheduler should throw exception when shutting down if not running`() { + val scheduler = KtScheduler() + try { + scheduler.shutdown() + fail("Should throw IllegalStateException") + } catch (e: IllegalStateException) { + assertEquals("Scheduler is not running", e.message) + } + } + + @Test + fun `scheduler should block main thread when idle`() { + val thread = Thread { + try { + val scheduler = KtScheduler() + scheduler.start() + scheduler.idle() + fail("Should not reach here") + } catch (_: InterruptedException) { + assertTrue(Thread.interrupted()) + } + } + thread.start() + Thread.sleep(500) + if (thread.isAlive) { + thread.interrupt() + thread.join() + } + } + @Test fun `addJob should add job to the scheduler`() { val scheduler = KtScheduler() @@ -234,7 +267,7 @@ class KtSchedulerTest { scheduler.addEventListener(eventListener) scheduler.start() - Thread.sleep(2100) + Thread.sleep(2200) // Job 1 should be completed twice assertEquals(2, eventListener.completedJobs.size)