-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update engine tasks to self-sort and more unit tests
- Loading branch information
Showing
6 changed files
with
83 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package rs.dusk.engine | ||
|
||
import org.junit.jupiter.api.Assertions.assertEquals | ||
import org.junit.jupiter.api.Test | ||
import org.koin.core.get | ||
import org.koin.dsl.module | ||
import rs.dusk.engine.script.KoinMock | ||
|
||
/** | ||
* @author Greg Hibberd <greg@greghibberd.com> | ||
* @since May 03, 2020 | ||
*/ | ||
internal class EngineTaskTest : KoinMock() { | ||
|
||
private class TestTask(tasks: EngineTasks, priority: Int) : EngineTask(tasks, priority) { | ||
override fun run() { | ||
} | ||
} | ||
|
||
override val modules = listOf(engineModule, module { | ||
single { TestTask(get(), 1) } | ||
}) | ||
|
||
@Test | ||
fun `Adds self to task list`() { | ||
// Given | ||
val tasks: EngineTasks = get() | ||
val updateTask: TestTask = get() | ||
// Then | ||
assert(tasks.contains(updateTask)) | ||
} | ||
|
||
@Test | ||
fun `Multiple tasks are ordered by priority`() { | ||
// Given | ||
val tasks: EngineTasks = get() | ||
val third = TestTask(tasks, 1) | ||
val first = TestTask(tasks, 3) | ||
val second = TestTask(tasks, 2) | ||
// Then | ||
val expected = listOf(first, second, third) | ||
tasks.data.forEachIndexed { index, task -> | ||
assertEquals(expected[index], task) | ||
} | ||
} | ||
|
||
} |
This file was deleted.
Oops, something went wrong.
34 changes: 34 additions & 0 deletions
34
engine/src/test/kotlin/rs/dusk/engine/ParallelEngineTaskTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package rs.dusk.engine | ||
|
||
import kotlinx.coroutines.GlobalScope | ||
import kotlinx.coroutines.async | ||
import kotlinx.coroutines.delay | ||
import org.junit.jupiter.api.Test | ||
import kotlin.system.measureTimeMillis | ||
|
||
/** | ||
* @author Greg Hibberd <greg@greghibberd.com> | ||
* @since May 03, 2020 | ||
*/ | ||
internal class ParallelEngineTaskTest { | ||
|
||
private class TestTask(tasks: EngineTasks) : ParallelEngineTask(tasks) | ||
|
||
@Test | ||
fun `Tasks run in parallel`() { | ||
// Given | ||
val tasks = EngineTasks() | ||
val first = TestTask(tasks) | ||
first.defers.add(GlobalScope.async { | ||
delay(100) | ||
}) | ||
first.defers.add(GlobalScope.async { | ||
delay(100) | ||
}) | ||
// When | ||
val took = measureTimeMillis { | ||
first.run() | ||
} | ||
assert(took < 200)// This could be temperamental | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters