Skip to content

Commit

Permalink
test: Extend scheduler tests and add coverage reports (#5)
Browse files Browse the repository at this point in the history
---------
Signed-off-by: starry-shivam <[email protected]>
  • Loading branch information
starry-shivam authored Jun 18, 2024
1 parent 6ddeb57 commit abaa94e
Show file tree
Hide file tree
Showing 7 changed files with 174 additions and 106 deletions.
22 changes: 11 additions & 11 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ jobs:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- name: set up JDK 21
uses: actions/setup-java@v3
with:
java-version: '21'
distribution: 'temurin'
cache: gradle
- uses: actions/checkout@v3
- name: set up JDK 21
uses: actions/setup-java@v3
with:
java-version: '21'
distribution: 'temurin'
cache: gradle

- name: Grant execute permission for gradlew
run: chmod +x gradlew
- name: Run tests with gradle
run: ./gradlew test --stacktrace
- name: Grant execute permission for gradlew
run: chmod +x gradlew
- name: Run tests with gradle
run: ./gradlew test --stacktrace
25 changes: 17 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
<a href="https://www.repostatus.org/#active"><img src="https://www.repostatus.org/badges/latest/active.svg" alt="Project Status: Active – The project has reached a stable, usable state and is being actively developed." /></a>
</p>

**KtScheduler** is a lightweight task/job scheduling library for Kotlin, powered by Kotlin coroutines! The design of this library is inspired by the [APScheduler](https://github.com/agronholm/apscheduler) library for Python, while keeping things simple and easy to use.
**KtScheduler** is a lightweight task/job scheduling library for Kotlin, powered by Kotlin coroutines! The design of
this library is inspired by the [APScheduler](https://github.com/agronholm/apscheduler) library for Python, while
keeping things simple and easy to use.

------

Expand Down Expand Up @@ -66,7 +68,8 @@ scheduler.idle()

#### Triggers

Triggers determine when and at what frequency a particular job should be executed. KtScheduler provides four types of triggers:
Triggers determine when and at what frequency a particular job should be executed. KtScheduler provides four types of
triggers:

1. `CronTrigger` - A trigger that determines the next run time based on the specified days of the week and time.

Expand Down Expand Up @@ -131,7 +134,8 @@ class WeekendTrigger(private val time: LocalTime) : Trigger {

#### Listening for Job Events

You can listen for job events such as completion or failure due to errors by attaching a `JobEventListener` to the `KtScheduler`. Here's an example:
You can listen for job events such as completion or failure due to errors by attaching a `JobEventListener` to
the `KtScheduler`. Here's an example:

```kotlin
import dev.starry.ktscheduler.event.JobEvent
Expand All @@ -150,21 +154,26 @@ class MyEventListener : JobEventListener {
val eventListener = MyEventListener()
scheduler.addEventListener(eventListener)
```

------

#### Contributing 🫶
### Contributing 🫶

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change, or feel free to tackle any of the open issues present at the moment. If you're doing the latter, please leave a comment on the issue you want to contribute to before starting to work on it.
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change, or
feel free to tackle any of the open issues present at the moment. If you're doing the latter, please leave a comment on
the issue you want to contribute to before starting to work on it.

------

#### Supporting ❤️
### 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 joining the list of stargazers 🌟
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 joining the list of stargazers 🌟

------

#### License ©️
### License ©️

```
Copyright [2024 - Present] starry-shivam
Expand Down
10 changes: 8 additions & 2 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
plugins {
kotlin("jvm") version "2.0.0"
id("org.jetbrains.kotlinx.kover") version "0.8.1"
`maven-publish`
}

Expand All @@ -21,13 +22,18 @@ dependencies {
testImplementation("org.jetbrains.kotlin:kotlin-test-junit:2.0.0")
}

kover {
reports {
verify { rule { minBound(70) } }
}
}

publishing {
publications {
register("mavenJava", MavenPublication::class) {
groupId = "dev.starry.ktscheduler"
groupId = group.toString()
artifactId = "ktscheduler"
version = "1.0.0"
version = version.toString()
from(components["java"])
}
}
Expand Down
77 changes: 0 additions & 77 deletions src/main/kotlin/Main.kt

This file was deleted.

19 changes: 15 additions & 4 deletions src/main/kotlin/scheduler/KtScheduler.kt
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,6 @@ class KtScheduler(
private val logger = Logger.getLogger(TAG)
}

// The coroutine scope with a SupervisorJob to prevent cancellation of all jobs
// if one of them fails.
private val coroutineScope = CoroutineScope(Dispatchers.Default + SupervisorJob())

// The list of event listeners attached to the scheduler.
private val eventListeners = mutableListOf<JobEventListener>()

Expand All @@ -77,14 +73,26 @@ class KtScheduler(
// The tick interval in milliseconds.
private val tickInterval = 100L

// The coroutine scope with a SupervisorJob to prevent cancellation of all jobs
// if one of them fails.
private lateinit var coroutineScope: CoroutineScope

/**
* Starts the scheduler.
*
* The scheduler will run in a coroutine and continuously process due jobs
* at the specified tick interval unless it is paused or shut down.
*
* @throws IllegalStateException If the scheduler is already running.
*/
override fun start() {
// Check if the scheduler is already running.
if (::coroutineScope.isInitialized && coroutineScope.isActive) {
throw IllegalStateException("Scheduler is already running")
}
// Start the scheduler.
logger.info("Starting scheduler")
coroutineScope = createCoroutineScope()
coroutineScope.launch {
while (isActive) {
if (!isPaused) {
Expand Down Expand Up @@ -231,6 +239,9 @@ class KtScheduler(
eventListeners.add(listener)
}

// Creates a new coroutine scope.
private fun createCoroutineScope() = CoroutineScope(Dispatchers.Default + SupervisorJob())

// Processes due jobs and executes them.
private suspend fun processDueJobs() {
val now = ZonedDateTime.now(timeZone)
Expand Down
1 change: 0 additions & 1 deletion src/test/kotlin/CoroutineExecutorTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import dev.starry.ktscheduler.triggers.OneTimeTrigger
import junit.framework.TestCase.assertTrue
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.delay
import kotlinx.coroutines.test.UnconfinedTestDispatcher
import kotlinx.coroutines.test.resetMain
import kotlinx.coroutines.test.runTest
Expand Down
Loading

0 comments on commit abaa94e

Please sign in to comment.