-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
42b7cab
commit 2ff23d7
Showing
4 changed files
with
50 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
src/jvmTest/kotlin/io/konform/validation/ConcurrencyTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package io.konform.validation | ||
|
||
import kotlin.concurrent.thread | ||
import kotlin.test.Test | ||
import kotlin.test.assertContentEquals | ||
import kotlin.test.assertEquals | ||
|
||
class ConcurrencyTest { | ||
data class User(val name: String) | ||
|
||
val user1 = User("a") | ||
val user2 = User("invalid") | ||
|
||
private val sleepTimeMs = 300L | ||
|
||
val mustNotBeNamedInvalidValidation = | ||
Validation<User> { (user) -> | ||
addConstraint("Cannot be 'invalid'") { | ||
// Simulate a long expensive blocking operation | ||
Thread.sleep(sleepTimeMs) | ||
user.name != "invalid" | ||
} | ||
} | ||
|
||
@Test | ||
fun validationsMustBeThreadSafe() { | ||
var validation1Holder: ValidationResult<User>? = null | ||
// Run the first validation concurrently | ||
thread { | ||
validation1Holder = mustNotBeNamedInvalidValidation.validate(user1) | ||
} | ||
val validation2 = mustNotBeNamedInvalidValidation.validate(user2) | ||
// Give the thread time to finish | ||
Thread.sleep(sleepTimeMs + 100) | ||
val validation1 = | ||
requireNotNull(validation1Holder) { | ||
"Validation 1 should have completed by now" | ||
} | ||
assertContentEquals(validation1.errors, listOf()) | ||
assertEquals(1, validation2.errors.size) | ||
} | ||
} |