Skip to content

Commit

Permalink
chore: Add test that verifies that tasks are not inserted when the bu…
Browse files Browse the repository at this point in the history
…ffer is full
  • Loading branch information
davidkleiven committed Dec 10, 2023
1 parent ae1a6ab commit 50e38d1
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/main/ExceptionHandler.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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()}",
Expand Down
23 changes: 23 additions & 0 deletions src/test/TaskManagerTest.kt
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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<FullBufferException> { 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
}

0 comments on commit 50e38d1

Please sign in to comment.