forked from MarathonLabs/marathon
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Save test results to cache concurrently
- Loading branch information
Sergey Chelombitko
committed
Dec 17, 2024
1 parent
732b77e
commit fb30a86
Showing
2 changed files
with
19 additions
and
27 deletions.
There are no files selected for viewing
42 changes: 18 additions & 24 deletions
42
core/src/main/kotlin/com/malinskiy/marathon/cache/test/TestCacheSaver.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 |
---|---|---|
@@ -1,45 +1,39 @@ | ||
package com.malinskiy.marathon.cache.test | ||
|
||
import com.malinskiy.marathon.actor.unboundedChannel | ||
import com.malinskiy.marathon.cache.test.key.TestCacheKeyFactory | ||
import com.malinskiy.marathon.device.DevicePoolId | ||
import com.malinskiy.marathon.execution.TestResult | ||
import com.malinskiy.marathon.log.MarathonLogging | ||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.Deferred | ||
import kotlinx.coroutines.async | ||
import kotlinx.coroutines.channels.Channel | ||
import kotlinx.coroutines.runBlocking | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.SupervisorJob | ||
import kotlinx.coroutines.cancel | ||
import kotlinx.coroutines.launch | ||
|
||
class TestCacheSaver( | ||
private val cache: TestResultsCache, | ||
private val testCacheKeyProvider: TestCacheKeyFactory | ||
) { | ||
) : AutoCloseable { | ||
|
||
private val logger = MarathonLogging.getLogger(TestCacheSaver::class.java) | ||
|
||
private val tasks: Channel<SaveTask> = unboundedChannel() | ||
private lateinit var completableDeferred: Deferred<Unit> | ||
|
||
fun initialize(scope: CoroutineScope) { | ||
completableDeferred = scope.async { | ||
for (task in tasks) { | ||
val cacheKey = testCacheKeyProvider.getCacheKey(task.poolId, task.result.test) | ||
cache.store(cacheKey, task.result) | ||
} | ||
private val job = SupervisorJob() | ||
private val dispatcher = Dispatchers.IO.limitedParallelism(16, "Cache saver") | ||
private val scope = CoroutineScope(job + dispatcher) | ||
|
||
fun saveTestResult(poolId: DevicePoolId, result: TestResult) { | ||
scope.launch { | ||
val cacheKey = testCacheKeyProvider.getCacheKey(poolId, result.test) | ||
cache.store(cacheKey, result) | ||
} | ||
} | ||
|
||
fun saveTestResult(poolId: DevicePoolId, result: TestResult) = runBlocking { | ||
// channel is unbounded, so it will return immediately | ||
tasks.send(SaveTask(poolId, result)) | ||
} | ||
|
||
suspend fun terminate() { | ||
tasks.close() | ||
completableDeferred.await() | ||
job.complete() | ||
job.join() | ||
logger.debug("Cache saver is terminated") | ||
} | ||
|
||
private class SaveTask(val poolId: DevicePoolId, val result: TestResult) | ||
override fun close() { | ||
scope.cancel() | ||
} | ||
} |
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