Skip to content
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

chore: Make tests faster #2569

Merged
merged 2 commits into from
Oct 12, 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
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
Loading