Skip to content

Commit

Permalink
Make it possible to disable pushing to test cache
Browse files Browse the repository at this point in the history
  • Loading branch information
Sergey Chelombitko committed Dec 17, 2024
1 parent a8e0080 commit 3bf38c3
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,21 @@ class CacheScenarios {
assertTrue(isFromCache)
}

@Test
fun `GIVEN cache is enabled and push is disabled WHEN running tests second time THEN test results are not from cache`() = runTest {
val test = createTest()
val cacheConfiguration = CacheConfiguration(remote = RemoteCacheConfiguration.Enabled(url = container.cacheUrl, push = false))

val build1OutputDir = tempDir.resolve("build-1")
runMarathonWithOneTest(cacheConfiguration, build1OutputDir, test)

val build2OutputDir = tempDir.resolve("build-2")
runMarathonWithOneTest(cacheConfiguration, build2OutputDir, test)

val isFromCache = isFromCache(build2OutputDir, test)
assertFalse(isFromCache)
}

private fun createTest(): MarathonTest =
MarathonTest(
pkg = "test",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@ internal class TrackerFactory(
val mappingTracker = MappingTracker(delegatingTrackerInternal)

track + mappingTracker
track + cacheTestResultsTracker
if (configuration.cache.isPushEnabled) {
track + cacheTestResultsTracker
}
configuration.analyticsTracker?.let { track + it }

return delegatingTrackerInternal
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ sealed class RemoteCacheConfiguration {

data class Enabled(
val url: URI,
val credentials: Credentials? = null
val credentials: Credentials? = null,
val push: Boolean = true
) : RemoteCacheConfiguration()

data object Disabled : RemoteCacheConfiguration()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ package com.malinskiy.marathon.execution
import com.malinskiy.marathon.cache.config.LocalCacheConfiguration
import com.malinskiy.marathon.cache.config.RemoteCacheConfiguration

// TODO: support optional cache push
data class CacheConfiguration(
val local: LocalCacheConfiguration = LocalCacheConfiguration.Disabled,
val remote: RemoteCacheConfiguration = RemoteCacheConfiguration.Disabled
) {

val isEnabled: Boolean
get() = local !is LocalCacheConfiguration.Disabled || remote !is RemoteCacheConfiguration.Disabled
get() = local is LocalCacheConfiguration.Enabled || remote is RemoteCacheConfiguration.Enabled

val isPushEnabled: Boolean
get() = remote is RemoteCacheConfiguration.Enabled && remote.push
}
48 changes: 32 additions & 16 deletions core/src/main/kotlin/com/malinskiy/marathon/execution/Scheduler.kt
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,8 @@ class Scheduler(
private val scope = CoroutineScope(context)

suspend fun initialize() {
logger.debug("Subscribing to devices")
subscribeOnDevices()

logger.debug("Initializing test cache")
subscribeToCacheController()
cacheLoader.initialize(scope)
cacheSaver.initialize(scope)
initializeCache()

try {
withTimeout(deviceProvider.deviceInitializationTimeoutMillis) {
Expand All @@ -83,7 +78,9 @@ class Scheduler(

@OptIn(DelicateCoroutinesApi::class)
suspend fun stopAndWaitForCompletion() {
cacheLoader.stop()
if (configuration.cache.isEnabled) {
cacheLoader.stop()
}

logger.debug("Requesting stop in pools")

Expand All @@ -97,27 +94,45 @@ class Scheduler(
child.join()
}

cacheSaver.terminate()
if (configuration.cache.isPushEnabled) {
cacheSaver.terminate()
}
}

suspend fun addTests(shard: TestShard) {
pools.keys.forEach { pool ->
cacheLoader.addTests(pool, shard)
if (configuration.cache.isEnabled) {
pools.keys.forEach { pool ->
cacheLoader.addTests(pool, shard)
}
} else {
pools.values.forEach {
it.send(FromScheduler.AddTests(shard))
}
}
}

private fun subscribeToCacheController() {
scope.launch {
for (cacheResult in cacheLoader.results) {
when (cacheResult) {
is CacheResult.Miss -> pools.getValue(cacheResult.pool).send(FromScheduler.AddTests(cacheResult.testShard))
is CacheResult.Hit -> cachedTestsReporter.onCachedTest(cacheResult.pool, cacheResult.testResult)
private fun initializeCache() {
logger.debug("Test cache is ${if (configuration.cache.isEnabled) "enabled" else "disabled"}")

if (configuration.cache.isEnabled) {
cacheLoader.initialize(scope)
scope.launch {
for (cacheResult in cacheLoader.results) {
when (cacheResult) {
is CacheResult.Miss -> pools.getValue(cacheResult.pool).send(FromScheduler.AddTests(cacheResult.testShard))
is CacheResult.Hit -> cachedTestsReporter.onCachedTest(cacheResult.pool, cacheResult.testResult)
}
}
}
}
if (configuration.cache.isPushEnabled) {
cacheSaver.initialize(scope)
}
}

private fun subscribeOnDevices() {
logger.debug("Subscribing to devices")

scope.launch {
logger.debug("Reading messages from device provider")

Expand All @@ -126,6 +141,7 @@ class Scheduler(
is DeviceProvider.DeviceEvent.DeviceConnected -> {
onDeviceConnected(msg, job, coroutineContext)
}

is DeviceProvider.DeviceEvent.DeviceDisconnected -> {
onDeviceDisconnected(msg)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ interface CachePluginConfiguration {
val remote: RemoteCacheExtension

fun local(action: Action<LocalCacheExtension>) {
local.initDefaults()
action.execute(local)
}

Expand All @@ -30,16 +29,22 @@ interface CachePluginConfiguration {

interface LocalCacheExtension {
val directory: DirectoryProperty
val enabled: Property<Boolean>
val removeUnusedEntriesAfterDays: Property<Int>
}

interface RemoteCacheExtension {
val url: Property<URI>
val credentials: Property<PasswordCredentials>
val enabled: Property<Boolean>
val push: Property<Boolean>
}

internal fun LocalCacheExtension.initDefaults() {
removeUnusedEntriesAfterDays.convention(7)
internal fun CachePluginConfiguration.initDefaults() {
local.enabled.convention(false)
local.removeUnusedEntriesAfterDays.convention(7)
remote.enabled.convention(false)
remote.push.convention(true)
}

internal fun CachePluginConfiguration.toCacheConfiguration(): CacheConfiguration =
Expand All @@ -49,15 +54,19 @@ internal fun CachePluginConfiguration.toCacheConfiguration(): CacheConfiguration
)

private fun LocalCacheExtension.toConfig(): LocalCacheConfiguration =
if (directory.isPresent) {
if (enabled.get()) {
LocalCacheConfiguration.Enabled(directory.get().asFile, removeUnusedEntriesAfterDays.get())
} else {
LocalCacheConfiguration.Disabled
}

private fun RemoteCacheExtension.toConfig(): RemoteCacheConfiguration =
if (url.isPresent) {
RemoteCacheConfiguration.Enabled(url.get(), credentials.orNull?.toCredentials())
if (enabled.get()) {
RemoteCacheConfiguration.Enabled(
url = url.get(),
credentials = credentials.orNull?.toCredentials(),
push = push.get()
)
} else {
RemoteCacheConfiguration.Disabled
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ interface MarathonExtension {
}

internal fun MarathonExtension.initDefaults() {
cache.initDefaults()
poolingStrategy.initDefaults()
strictRunConfiguration.initDefaults()

Expand Down

0 comments on commit 3bf38c3

Please sign in to comment.