1 /* 2 * Copyright 2017-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. 3 */ 4 5 package kotlinx.serialization.json 6 7 import kotlinx.serialization.* 8 import kotlin.native.concurrent.* 9 import kotlin.test.* 10 11 class MultiWorkerJsonTest { 12 @Serializable 13 data class PlainOne(val one: Int) 14 15 @Serializable 16 data class PlainTwo(val two: Int) 17 doTestnull18 private fun doTest(json: () -> Json) { 19 val worker = Worker.start() 20 val operation = { 21 for (i in 0..999) { 22 assertEquals(PlainOne(42), json().decodeFromString("""{"one":42,"two":239}""")) 23 } 24 } 25 worker.executeAfter(1000, operation.freeze()) 26 for (i in 0..999) { 27 assertEquals(PlainTwo(239), json().decodeFromString("""{"one":42,"two":239}""")) 28 } 29 worker.requestTermination() 30 } 31 32 33 @Test testJsonIsFreezeSafenull34 fun testJsonIsFreezeSafe() { 35 val json = Json { 36 isLenient = true 37 ignoreUnknownKeys = true 38 useAlternativeNames = true 39 } 40 // reuse instance 41 doTest { json } 42 } 43 44 @Test testJsonInstantiationnull45 fun testJsonInstantiation() { 46 // create new instanceEveryTime 47 doTest { 48 Json { 49 isLenient = true 50 ignoreUnknownKeys = true 51 useAlternativeNames = true 52 } 53 } 54 } 55 } 56