Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Add test that verifies that tasks are not inserted when the bu… #51

Merged
merged 1 commit into from
Dec 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}
Loading