diff --git a/src/main/ExceptionHandler.kt b/src/main/ExceptionHandler.kt index f29bf14..0ce3ec5 100644 --- a/src/main/ExceptionHandler.kt +++ b/src/main/ExceptionHandler.kt @@ -28,6 +28,13 @@ class ExceptionHandler { ) } + is FullBufferException -> { + call.respondText( + "Service is currently unavailable because too many tasks are running.", + status = HttpStatusCode.ServiceUnavailable + ) + } + else -> { call.respondText( "500: $cause. Stack trace: ${cause.stackTraceToString()}", diff --git a/src/test/TaskManagerTest.kt b/src/test/TaskManagerTest.kt index e347b0a..f374460 100644 --- a/src/test/TaskManagerTest.kt +++ b/src/test/TaskManagerTest.kt @@ -1,8 +1,10 @@ +import com.github.statnett.loadflowservice.FullBufferException import com.github.statnett.loadflowservice.Task import com.github.statnett.loadflowservice.TaskManager import com.github.statnett.loadflowservice.TaskStatus import junit.framework.TestCase.* import kotlin.test.Test +import kotlin.test.assertFailsWith class TaskManagerTest { @Test @@ -31,4 +33,25 @@ class TaskManagerTest { assertNull(tm.queue.get(task1.id)) assertEquals(2, tm.queue.size()) } + + @Test + fun `test tasks not inserted when max number of tasks are running`() { + val maxRunning = 5 + val tm = TaskManager(maxRunning) + repeat (maxRunning) { tm.register(runningTask()) } + assertEquals(maxRunning, tm.numRunning()) + assertEquals(maxRunning, tm.size()) + + // When inserting one more, it should raise FullBufferError + assertFailsWith { tm.register(runningTask()) } + + // Verify that the task was not inserted + assertEquals(maxRunning, tm.size()) + } +} + +fun runningTask(): Task { + val task = Task() + task.status = TaskStatus.RUNNING + return task } \ No newline at end of file