Skip to content

Commit

Permalink
chore: Make tests faster (#2569)
Browse files Browse the repository at this point in the history
  • Loading branch information
JanCizmar authored Oct 12, 2024
1 parent 5530d3a commit 07ffa68
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -433,9 +433,12 @@ class TestDataService(
private fun saveAllUsers(builder: TestDataBuilder) {
val userAccountBuilders = builder.data.userAccounts
userAccountService.saveAll(
userAccountBuilders.map {
it.self.password = passwordEncoder.encode(it.rawPassword)
it.self
userAccountBuilders.map { userBuilder ->
userBuilder.self.password =
passwordHashCache.computeIfAbsent(userBuilder.rawPassword) {
passwordEncoder.encode(userBuilder.rawPassword)
}
userBuilder.self
},
)
saveUserAvatars(userAccountBuilders)
Expand Down Expand Up @@ -500,4 +503,8 @@ class TestDataService(
private fun clearEntityManager() {
entityManager.clear()
}

companion object {
private val passwordHashCache = mutableMapOf<String, String>()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package io.tolgee

import io.tolgee.BatchJobTestUtil.pauseAndClearBatchJobs
import io.tolgee.BatchJobTestUtil.resumeBatchJobs
import org.springframework.test.context.TestContext
import org.springframework.test.context.TestExecutionListener

class BatchJobCleanerListener : TestExecutionListener {
override fun beforeTestMethod(testContext: TestContext) {
pauseAndClearBatchJobs(testContext)
resumeBatchJobs(testContext)
}
}
24 changes: 24 additions & 0 deletions backend/testing/src/main/kotlin/io/tolgee/BatchJobTestUtil.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package io.tolgee

import io.tolgee.batch.BatchJobChunkExecutionQueue
import io.tolgee.batch.BatchJobConcurrentLauncher
import org.springframework.test.context.TestContext

object BatchJobTestUtil {
fun resumeBatchJobs(testContext: TestContext) {
getBatchJobConcurrentLauncher(testContext).pause = false
}

fun pauseAndClearBatchJobs(testContext: TestContext) {
getBatchJobConcurrentLauncher(testContext).pause = true
getBatchJobExecutionQueue(testContext).clear()
}

private fun getBatchJobExecutionQueue(testContext: TestContext): BatchJobChunkExecutionQueue {
return testContext.applicationContext.getBean(BatchJobChunkExecutionQueue::class.java)
}

private fun getBatchJobConcurrentLauncher(testContext: TestContext): BatchJobConcurrentLauncher {
return testContext.applicationContext.getBean(BatchJobConcurrentLauncher::class.java)
}
}
22 changes: 21 additions & 1 deletion backend/testing/src/main/kotlin/io/tolgee/CleanDbTestListener.kt
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ class CleanDbTestListener : TestExecutionListener {
}
}
}

i++
}
}
Expand Down Expand Up @@ -90,19 +89,40 @@ class CleanDbTestListener : TestExecutionListener {
while (rs.next()) {
tables.add(rs.getString(1) to rs.getString(2))
}

val disableConstraintsSQL = generateDisableConstraintsSQL(tables)
disableConstraintsSQL.forEach { stmt.addBatch(it) }
stmt.executeBatch()

stmt.execute(
java.lang.String.format(
"TRUNCATE TABLE %s",
tables.joinToString(",") { it.first + "." + it.second },
),
)

val enableConstraintsSQL = generateEnableConstraintsSQL(tables)
enableConstraintsSQL.forEach { stmt.addBatch(it) }
stmt.executeBatch()
} catch (e: InterruptedException) {
stmt.cancel()
throw e
}
}
}

private fun generateDisableConstraintsSQL(tables: List<Pair<String, String>>): List<String> {
return tables.map { (schema, table) ->
"ALTER TABLE \"$schema\".\"$table\" DISABLE TRIGGER ALL"
}
}

private fun generateEnableConstraintsSQL(tables: List<Pair<String, String>>): List<String> {
return tables.map { (schema, table) ->
"ALTER TABLE \"$schema\".\"$table\" ENABLE TRIGGER ALL"
}
}

@Throws(Exception::class)
override fun afterTestMethod(testContext: TestContext) {
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.tolgee.testing

import io.tolgee.BatchJobCleanerListener
import io.tolgee.CleanDbTestListener
import jakarta.persistence.EntityManager
import org.springframework.beans.factory.annotation.Autowired
Expand All @@ -16,6 +17,7 @@ import org.springframework.test.context.transaction.TransactionalTestExecutionLi
DependencyInjectionTestExecutionListener::class,
CleanDbTestListener::class,
DirtiesContextTestExecutionListener::class,
BatchJobCleanerListener::class,
],
)
@ActiveProfiles(profiles = ["local"])
Expand Down

0 comments on commit 07ffa68

Please sign in to comment.