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

Added test that reproduces wrapRow cache bug #1

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
@@ -1,16 +1,22 @@
package org.jetbrains.exposed.sql.tests.shared.entities

import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.async
import kotlinx.coroutines.awaitAll
import org.jetbrains.exposed.dao.IntEntity
import org.jetbrains.exposed.dao.IntEntityClass
import org.jetbrains.exposed.dao.entityCache
import org.jetbrains.exposed.dao.id.EntityID
import org.jetbrains.exposed.dao.id.IntIdTable
import org.jetbrains.exposed.sql.SchemaUtils
import org.jetbrains.exposed.sql.select
import org.jetbrains.exposed.sql.selectAll
import org.jetbrains.exposed.sql.tests.DatabaseTestsBase
import org.jetbrains.exposed.sql.tests.TestDB
import org.jetbrains.exposed.sql.tests.shared.assertEqualCollections
import org.jetbrains.exposed.sql.tests.shared.assertEquals
import org.jetbrains.exposed.sql.transactions.experimental.suspendedTransactionAsync
import org.jetbrains.exposed.sql.transactions.transaction
import org.junit.Assume
import org.junit.Test
Expand Down Expand Up @@ -160,7 +166,7 @@ class EntityCacheTests : DatabaseTestsBase() {
}

@Test
fun `EntityCache should not be cleaned on explicit commit` () {
fun `EntityCache should not be cleaned on explicit commit`() {
withTables(TestTable) {
val entity = TestEntity.new {
value = Random.nextInt()
Expand All @@ -170,4 +176,41 @@ class EntityCacheTests : DatabaseTestsBase() {
assertEquals(entity, TestEntity.testCache(entity.id))
}
}

@Test
fun `wrapRow in select For Update should not return stale value`() {
withTables(TestTable) {
val entityId = TestEntity.new {
value = 0
}.id
commit()

val job = GlobalScope.async {
// 5 concurrent threads to increment entity's value
(1..5).map {
suspendedTransactionAsync(Dispatchers.IO, db = db) {
val currentEntity = TestEntity.findById(entityId)!!
// Simple business logic:
// Increment value if it's not over 100 yet
if (currentEntity.value < 100) {
// Select for update to lock the row
val lockedEntity = TestTable.select { TestTable.id eq currentEntity.id }.forUpdate().map { row ->
TestEntity.wrapRow(row).also { entity ->
// Fetched row should have same value as entity!
assertEquals(entity.value, row[TestTable.value])
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Btw, this line fails with

Expected :0
Actual   :1

}
}.first()
lockedEntity.value += 1 // Increments value
}
// Update will be flushed when transaction finishes
}
}.awaitAll()
}
while (!job.isCompleted) Thread.sleep(100)
job.getCompletionExceptionOrNull()?.let { throw it }
val updatedEntity = TestTable.select { TestTable.id eq entityId }.map { TestEntity.wrapRow(it) }.first()
// Summing 5 times should give 5 in total
assertEquals(5, updatedEntity.value)
}
}
}