@@ -26,6 +26,7 @@ import dev.starry.ktscheduler.triggers.OneTimeTrigger
26
26
import junit.framework.TestCase.assertFalse
27
27
import junit.framework.TestCase.assertTrue
28
28
import kotlinx.coroutines.ExperimentalCoroutinesApi
29
+ import kotlinx.coroutines.delay
29
30
import kotlinx.coroutines.test.runTest
30
31
import org.junit.Test
31
32
import java.time.ZonedDateTime
@@ -289,6 +290,33 @@ class KtSchedulerTest {
289
290
scheduler.shutdown()
290
291
}
291
292
293
+ @Test
294
+ fun `scheduler should not execute job multiple times if it is still running` (): Unit = runTest {
295
+ val scheduler = KtScheduler ()
296
+
297
+ // Create a job that takes 2 seconds to execute
298
+ val job = Job (
299
+ jobId = " longRunningJob" ,
300
+ trigger = IntervalTrigger (intervalSeconds = 1 ),
301
+ nextRunTime = ZonedDateTime .now().plusSeconds(1 ),
302
+ callback = { delay(2000 ) }
303
+ )
304
+ val eventListener = TestJobEventListener ()
305
+
306
+ scheduler.addJob(job)
307
+ scheduler.addEventListener(eventListener)
308
+ scheduler.start()
309
+ Thread .sleep(200 )
310
+ // Job should not be completed yet
311
+ assertEquals(0 , eventListener.completedJobs.size)
312
+ // Wait for enough time to ensure job has run
313
+ Thread .sleep(3000 )
314
+ scheduler.shutdown()
315
+ // Assert that the job was only executed once
316
+ assertEquals(1 , eventListener.completedJobs.size)
317
+ assertEquals(" longRunningJob" , eventListener.completedJobs[0 ])
318
+ }
319
+
292
320
private fun createTestJob (
293
321
jobId : String ,
294
322
runAt : ZonedDateTime = ZonedDateTime .now().plusSeconds(1),
0 commit comments