Skip to content

Commit

Permalink
(feature) Async saving of attachments
Browse files Browse the repository at this point in the history
  • Loading branch information
craigatk committed Mar 3, 2020
1 parent 1ca391c commit 23aa423
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ package projektor.attachment
import io.ktor.util.KtorExperimentalAPI
import java.io.InputStream
import java.math.BigDecimal
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import kotlinx.coroutines.*
import org.slf4j.LoggerFactory
import projektor.objectstore.ObjectStoreClient
import projektor.objectstore.ObjectStoreConfig
Expand All @@ -21,6 +20,8 @@ class AttachmentService(

private val objectStoreClient = ObjectStoreClient(ObjectStoreConfig(config.url, config.accessKey, config.secretKey))

private val asyncAddAttachments = newFixedThreadPoolContext(8, "addAttachments")

fun conditionallyCreateBucketIfNotExists() {
if (config.autoCreateBucket) {
try {
Expand All @@ -42,11 +43,17 @@ class AttachmentService(
suspend fun addAttachment(publicId: PublicId, fileName: String, attachmentStream: InputStream, attachmentSize: Long?) {
val objectName = attachmentObjectName(publicId, fileName)

withContext(Dispatchers.IO) {
objectStoreClient.putObject(config.bucketName, objectName, attachmentStream)
}
GlobalScope.launch(asyncAddAttachments, CoroutineStart.DEFAULT) {
try {
withContext(Dispatchers.IO) {
objectStoreClient.putObject(config.bucketName, objectName, attachmentStream)
}

attachmentRepository.addAttachment(publicId, Attachment(fileName = fileName, objectName = objectName, fileSize = attachmentSize))
attachmentRepository.addAttachment(publicId, Attachment(fileName = fileName, objectName = objectName, fileSize = attachmentSize))
} catch (e: Exception) {
logger.error("Error saving attachment '$fileName' for test run ${publicId.id}", e)
}
}
}

suspend fun getAttachment(publicId: PublicId, attachmentFileName: String) = withContext(Dispatchers.IO) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,13 @@ import io.ktor.config.MapApplicationConfig
import io.ktor.util.KtorExperimentalAPI
import java.math.BigDecimal
import kotlin.test.AfterTest
import org.awaitility.kotlin.await
import org.awaitility.kotlin.until
import org.jooq.DSLContext
import org.koin.ktor.ext.get
import projektor.database.generated.tables.daos.*
import projektor.parser.ResultsXmlLoader
import projektor.server.api.PublicId

@KtorExperimentalAPI
open class ApplicationTestCase {
Expand All @@ -35,6 +38,7 @@ open class ApplicationTestCase {
lateinit var testSuiteDao: TestSuiteDao
lateinit var testCaseDao: TestCaseDao
lateinit var testFailureDao: TestFailureDao
lateinit var attachmentDao: TestRunAttachmentDao
lateinit var testRunDBGenerator: TestRunDBGenerator
lateinit var application: Application

Expand Down Expand Up @@ -76,11 +80,16 @@ open class ApplicationTestCase {
testSuiteDao = TestSuiteDao(dslContext.configuration())
testCaseDao = TestCaseDao(dslContext.configuration())
testFailureDao = TestFailureDao(dslContext.configuration())
attachmentDao = TestRunAttachmentDao(dslContext.configuration())
testRunDBGenerator = TestRunDBGenerator(testRunDao, testSuiteGroupDao, testSuiteDao, testCaseDao, testFailureDao)

this.application = application
}

fun waitUntilTestRunHasAttachments(publicId: PublicId, attachmentCount: Int) {
await until { attachmentDao.fetchByTestRunPublicId(publicId.id).size == attachmentCount }
}

@AfterTest
fun closeDataSource() {
dataSource.close()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ class AddAttachmentApplicationTest : ApplicationTestCase() {
expectThat(response.status()).isEqualTo(HttpStatusCode.OK)
}

waitUntilTestRunHasAttachments(publicId, 1)

handleRequest(HttpMethod.Get, "/run/$publicId/attachments/test-attachment.txt") {
}.apply {
expectThat(response.status()).isEqualTo(HttpStatusCode.OK)
Expand All @@ -68,6 +70,8 @@ class AddAttachmentApplicationTest : ApplicationTestCase() {
expectThat(response.status()).isEqualTo(HttpStatusCode.OK)
}

waitUntilTestRunHasAttachments(publicId, 1)

handleRequest(HttpMethod.Get, "/run/$publicId/attachments/test-attachment.txt") {
}.apply {
expectThat(response.status()).isEqualTo(HttpStatusCode.OK)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ class AddAttachmentTokenApplicationTest : ApplicationTestCase() {
expectThat(response.status()).isEqualTo(HttpStatusCode.OK)
}

waitUntilTestRunHasAttachments(publicId, 1)

handleRequest(HttpMethod.Get, "/run/$publicId/attachments/test-attachment.txt") {
}.apply {
expectThat(response.status()).isEqualTo(HttpStatusCode.OK)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ class ListAttachmentsApplicationTest : ApplicationTestCase() {
expectThat(response.status()).isEqualTo(HttpStatusCode.OK)
}

waitUntilTestRunHasAttachments(publicId, 2)

handleRequest(HttpMethod.Get, "/run/$publicId/attachments") {
}.apply {
expectThat(response.status()).isEqualTo(HttpStatusCode.OK)
Expand Down

0 comments on commit 23aa423

Please sign in to comment.