Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,41 @@ internal class ApkStore(
getUploadedApk(apkInfo)?.let {
return it
}
return putApk(
apkInfo = apkInfo,
bytes = bytes
)
}

/**
* Returns the APK for the given [name] and [sha256] if it already exists in the backend; otherwise obtains the
* APK contents via [bytes] and uploads it without re-computing SHA-256 or re-checking GCS existence.
*/
suspend fun getOrUploadApk(
name: String,
sha256: String,
bytes: suspend () -> ByteArray
): UploadedApk {
val apkInfo = ApkInfo(
filePath = name,
idHash = sha256
)
getUploadedApk(apkInfo)?.let {
return it
}
return putApk(
apkInfo = apkInfo,
bytes = bytes()
)
}

private suspend fun putApk(
apkInfo: ApkInfo,
bytes: ByteArray
): UploadedApk {
val relativePath = apkInfo.gcpRelativePath()
logger.info {
"uploading $name to $relativePath"
"uploading ${apkInfo.filePath} to $relativePath"
}
val gcsPath = googleCloudApi.upload(
relativePath = relativePath,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,18 +78,11 @@ internal class TestRunnerServiceImpl internal constructor(
name: String,
sha256: String,
bytes: suspend () -> ByteArray
): UploadedApk {
apkStore.getUploadedApk(
name = name,
sha256 = sha256
)?.let {
return it
}
return apkStore.uploadApk(
name = name,
bytes = bytes()
)
}
): UploadedApk = apkStore.getOrUploadApk(
name = name,
sha256 = sha256,
bytes = bytes
)

override suspend fun getOrUploadRemoteApk(
sourceGcsPath: GcsPath,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -262,8 +262,12 @@ internal sealed interface TestScheduler {
return
}
if (scope.entry.name == name) {
val alreadyUploaded = apkStore.getUploadedApk(name, sha256)
uploadedApk = alreadyUploaded ?: apkStore.uploadApk(name, scope.bytes)
uploadedApk = apkStore.getOrUploadApk(
name = name,
sha256 = sha256
) {
scope.bytes
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ internal class FakeGoogleCloudApi(
) : GoogleCloudApi {
var uploadCount = 0
private set
var existingFilePathQueryCount = 0
private set
val rootGcsPath = GcsPath("gs://$bucketName")
private val artifacts = mutableMapOf<GcsPath, ByteArray>()

Expand Down Expand Up @@ -85,6 +87,7 @@ internal class FakeGoogleCloudApi(
}

override suspend fun existingFilePath(relativePath: String): GcsPath? {
existingFilePathQueryCount++
val path = makeGcsPath(relativePath)
return if (artifacts.containsKey(path)) {
path
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,34 @@ internal class ApkStoreTest {
assertThat(gcpApi.uploadCount).isEqualTo(1)
assertThat(gcpApi.artifacts()[placeholderApk.gcsPath]).isNotNull()
}

@Test
fun getOrUploadApk_usesProvidedShaAndChecksExistenceOnce() = runBlocking<Unit> {
val apkBytes = byteArrayOf(10, 20, 30, 40)
val providedSha = "precomputed-sha256-from-config"

val uploaded = apkStore.getOrUploadApk(
name = "sub/dir/test.apk",
sha256 = providedSha
) {
apkBytes
}

assertThat(gcpApi.existingFilePathQueryCount).isEqualTo(1)
assertThat(gcpApi.uploadCount).isEqualTo(1)
assertThat(uploaded.apkInfo.idHash).isEqualTo(providedSha)
assertThat(uploaded.gcsPath.path).isEqualTo("gs://my-bucket/sub/dir/test/$providedSha.apk")
assertThat(gcpApi.artifacts()[uploaded.gcsPath]).isEqualTo(apkBytes)

val cached = apkStore.getOrUploadApk(
name = "sub/dir/test.apk",
sha256 = providedSha
) {
error("bytes provider should not be called when APK already exists in GCS")
}

assertThat(cached).isEqualTo(uploaded)
assertThat(gcpApi.existingFilePathQueryCount).isEqualTo(2)
assertThat(gcpApi.uploadCount).isEqualTo(1)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ class TestRunnerServiceImplTest {
assertThat(
fakeBackend.fakeGoogleCloudApi.uploadCount
).isEqualTo(1)
assertThat(
fakeBackend.fakeGoogleCloudApi.existingFilePathQueryCount
).isEqualTo(1)
// re-upload
val upload2 = subject.getOrUploadApk(
name = "foo.apk",
Expand All @@ -77,6 +80,9 @@ class TestRunnerServiceImplTest {
assertThat(
fakeBackend.fakeGoogleCloudApi.uploadCount
).isEqualTo(1)
assertThat(
fakeBackend.fakeGoogleCloudApi.existingFilePathQueryCount
).isEqualTo(2)
}

@Test
Expand Down
Loading