summaryrefslogtreecommitdiff
path: root/formats/json/nativeTest/src/kotlinx/serialization/json/MultiWorkerJsonTest.kt
blob: 2ea063db6ff2c3f19b0f9754d51bfc4a239f80fa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/*
 * Copyright 2017-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
 */

package kotlinx.serialization.json

import kotlinx.serialization.*
import kotlin.native.concurrent.*
import kotlin.test.*

class MultiWorkerJsonTest {
    @Serializable
    data class PlainOne(val one: Int)

    @Serializable
    data class PlainTwo(val two: Int)

    private fun doTest(json: () -> Json) {
        val worker = Worker.start()
        val operation = {
            for (i in 0..999) {
                assertEquals(PlainOne(42), json().decodeFromString("""{"one":42,"two":239}"""))
            }
        }
        worker.executeAfter(1000, operation.freeze())
        for (i in 0..999) {
            assertEquals(PlainTwo(239), json().decodeFromString("""{"one":42,"two":239}"""))
        }
        worker.requestTermination()
    }


    @Test
    fun testJsonIsFreezeSafe() {
        val json = Json {
            isLenient = true
            ignoreUnknownKeys = true
            useAlternativeNames = true
        }
        // reuse instance
        doTest { json }
    }

    @Test
    fun testJsonInstantiation() {
        // create new instanceEveryTime
        doTest {
            Json {
                isLenient = true
                ignoreUnknownKeys = true
                useAlternativeNames = true
            }
        }
    }
}