1
+ package io.github.optimumcode.json.schema.cuncurrency
2
+
3
+ import io.github.optimumcode.json.schema.JsonSchema
4
+ import io.github.optimumcode.json.schema.OutputCollector
5
+ import io.github.optimumcode.json.schema.SchemaType
6
+ import io.kotest.assertions.throwables.shouldNotThrowAny
7
+ import io.kotest.core.spec.style.FunSpec
8
+ import io.kotest.matchers.booleans.shouldBeTrue
9
+ import kotlinx.coroutines.Dispatchers
10
+ import kotlinx.coroutines.coroutineScope
11
+ import kotlinx.coroutines.delay
12
+ import kotlinx.coroutines.launch
13
+ import kotlinx.coroutines.withContext
14
+ import kotlinx.serialization.json.JsonPrimitive
15
+ import kotlinx.serialization.json.buildJsonObject
16
+ import kotlin.time.Duration.Companion.milliseconds
17
+
18
+ class ConcurrentExecutionTest : FunSpec () {
19
+ init {
20
+ val schema =
21
+ JsonSchema .Companion .fromDefinition(
22
+ """
23
+ {
24
+ "properties": {
25
+ "inner": {
26
+ "type": "object",
27
+ "properties": {
28
+ "value": {
29
+ "type": "string"
30
+ }
31
+ }
32
+ }
33
+ }
34
+ }
35
+ """ .trimIndent(),
36
+ defaultType = SchemaType .DRAFT_2020_12 ,
37
+ )
38
+
39
+ test(" BUG #224: JsonSchema can be used concurrently" ).config(coroutineTestScope = false ) {
40
+ val target =
41
+ buildJsonObject {
42
+ put(
43
+ " inner" ,
44
+ buildJsonObject {
45
+ put(" value" , JsonPrimitive (" test" ))
46
+ },
47
+ )
48
+ }
49
+ shouldNotThrowAny {
50
+ coroutineScope {
51
+ withContext(Dispatchers .Default ) {
52
+ repeat(1000 ) {
53
+ launch {
54
+ // delay is added to force suspension and increase changes of catching a concurrent issue within JsonSchema
55
+ delay(1 .milliseconds)
56
+ val result = schema.validate(target, OutputCollector .Companion .flag())
57
+ result.valid.shouldBeTrue()
58
+ }
59
+ }
60
+ }
61
+ }
62
+ }
63
+ }
64
+ }
65
+ }
0 commit comments