Skip to content

Fix builds paging when models param is used #163

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

Merged
merged 5 commits into from
Mar 28, 2024
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
4 changes: 4 additions & 0 deletions library/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,10 @@ tasks.named("check") {
dependsOn("integrationTest")
}

tasks.named<Test>("integrationTest") {
jvmArgs("-Xmx512m")
}

java {
consistentResolution {
useRuntimeClasspathVersions()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,86 @@ package com.gabrielfeo.gradle.enterprise.api.extension

import com.gabrielfeo.gradle.enterprise.api.*
import com.gabrielfeo.gradle.enterprise.api.internal.*
import com.gabrielfeo.gradle.enterprise.api.model.BuildModelName
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.flow.take
import kotlinx.coroutines.test.runTest
import okhttp3.Request
import org.junit.jupiter.api.*
import org.junit.jupiter.api.Assertions.*
import kotlin.test.AfterTest
import kotlin.time.Duration.Companion.minutes

class BuildsApiExtensionsIntegrationTest {

@Test
fun getBuildsFlowUsesQueryInAllRequests() = runTest {
init {
env = RealEnv
keychain = RealKeychain(RealSystemProperties)
val recorder = RequestRecorder()
val api = buildApi(recorder)
api.buildsApi.getBuildsFlow(since = 0, query = "user:*").take(2000).collect()
recorder.requests.forEachIndexed { i, request ->
assertEquals("user:*", request.url.queryParameter("query"), "[$i]")
}
}

private val recorder = RequestRecorder()
private val api = buildApi(recorder)

@AfterTest
fun setup() {
api.shutdown()
}

@Test
fun getBuildsFlowPreservesParamsAcrossRequests() = runTest(timeout = 3.minutes) {
api.buildsApi.getBuildsFlow(
since = 0,
query = "user:*",
models = listOf(BuildModelName.gradleAttributes),
reverse = true,
).take(2000).collect()
recorder.requests.forEach {
assertUrlParam(it, "query", "user:*")
assertUrlParam(it, "models", "gradle-attributes")
assertUrlParam(it, "reverse", "true")
}
}

@Test
fun getBuildsFlowReplacesSinceForFromBuildAfterFirstRequest() = runTest {
api.buildsApi.getBuildsFlow(since = 1).take(2000).collect()
assertReplacedForFromBuildAfterFirstRequest(param = "since" to "1")
}

@Test
fun getBuildsFlowReplacesFromInstantForFromBuildAfterFirstRequest() = runTest {
api.buildsApi.getBuildsFlow(fromInstant = 1).take(2000).collect()
assertReplacedForFromBuildAfterFirstRequest(param = "fromInstant" to "1")
}

private fun assertReplacedForFromBuildAfterFirstRequest(param: Pair<String, String>) {
with(recorder.requests) {
val (key, value) = param
first().let {
assertUrlParam(it, key, value)
assertUrlParam(it, "fromBuild", null)
}
(this - first()).forEach {
assertUrlParam(it, key, null)
assertUrlParamNotNull(it, "fromBuild")
}
}
}

private fun buildApi(recorder: RequestRecorder) =
GradleEnterpriseApi.newInstance(
config = Config(
clientBuilder = recorder.clientBuilder(),
cacheConfig = Config.CacheConfig(cacheEnabled = false),
)
)

private fun assertUrlParam(request: Request, key: String, expected: String?) {
val actual = request.url.queryParameter(key)
assertEquals(expected, actual, "Expected '$key='$expected', but was '$key=$actual' (${request.url})")
}

private fun assertUrlParamNotNull(request: Request, key: String) {
assertNotNull(request.url.queryParameter(key), "Expected param $key, but was null (${request.url})")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ fun BuildsApi.getBuildsFlow(
reverse = reverse,
maxWaitSecs = maxWaitSecs,
maxBuilds = buildsPerPage,
models = models,
)
emitAll(builds.asFlow())
}
Expand Down