Skip to content

Commit

Permalink
Make S3OutputStream lazier
Browse files Browse the repository at this point in the history
  • Loading branch information
cyberdelia committed Nov 11, 2020
1 parent bfad092 commit 797caff
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 7 deletions.
16 changes: 9 additions & 7 deletions src/main/kotlin/com/lapanthere/signals/S3OutputStream.kt
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,15 @@ public class S3OutputStream(
private val buffer = ByteArrayOutputStream(MIN_PART_SIZE.toInt())
private val digest = DigestOutputStream(buffer, MessageDigest.getInstance("MD5"))
private val partSize = SizeIterator()
private val uploadID = s3.createMultipartUpload(
CreateMultipartUploadRequest.builder()
.applyMutation(mutator)
.bucket(bucket)
.key(key)
.build()
).get().uploadId()
private val uploadID by lazy {
s3.createMultipartUpload(
CreateMultipartUploadRequest.builder()
.applyMutation(mutator)
.bucket(bucket)
.key(key)
.build()
).get().uploadId()
}

override fun write(b: Int) {
digest.write(b)
Expand Down
4 changes: 4 additions & 0 deletions src/test/kotlin/com/lapanthere/signals/S3InputStreamTest.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.lapanthere.signals

import io.mockk.confirmVerified
import io.mockk.every
import io.mockk.mockk
import io.mockk.verify
Expand Down Expand Up @@ -105,6 +106,8 @@ internal class S3InputStreamTest {
any<ByteArrayAsyncResponseTransformer<GetObjectResponse>>()
)
}

confirmVerified(s3)
}

@Test
Expand Down Expand Up @@ -145,6 +148,7 @@ internal class S3InputStreamTest {
verify(exactly = 0) {
s3.getObject(any<GetObjectRequest>(), any<ByteArrayAsyncResponseTransformer<GetObjectResponse>>())
}
confirmVerified(s3)
}

@Test
Expand Down
10 changes: 10 additions & 0 deletions src/test/kotlin/com/lapanthere/signals/S3OutputStreamTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -209,4 +209,14 @@ internal class S3OutputStreamTest {

confirmVerified(s3)
}

@Test
fun `not uploading before being written to`() {
S3OutputStream(bucket = bucket, key = key, s3 = s3)
verify(exactly = 0) {
s3.createMultipartUpload(any<CreateMultipartUploadRequest>())
s3.uploadPart(any<UploadPartRequest>(), any<AsyncRequestBody>())
}
confirmVerified(s3)
}
}

0 comments on commit 797caff

Please sign in to comment.