From b8f5a9673677ad1d3c0efbcc39ac746163b5a4cd Mon Sep 17 00:00:00 2001 From: Leonid Stashevsky Date: Tue, 22 Sep 2020 17:42:40 +0300 Subject: [PATCH] KTOR-1067 Use shared references in test --- .../io/ktor/client/tests/CallValidatorTest.kt | 24 ++++++++----------- .../ktor/client/tests/features/CacheTest.kt | 10 +++----- .../io/ktor/client/tests/utils/Logging.kt | 3 ++- 3 files changed, 15 insertions(+), 22 deletions(-) diff --git a/ktor-client/ktor-client-tests/common/test/io/ktor/client/tests/CallValidatorTest.kt b/ktor-client/ktor-client-tests/common/test/io/ktor/client/tests/CallValidatorTest.kt index cbfbde3cb66..4a303818472 100644 --- a/ktor-client/ktor-client-tests/common/test/io/ktor/client/tests/CallValidatorTest.kt +++ b/ktor-client/ktor-client-tests/common/test/io/ktor/client/tests/CallValidatorTest.kt @@ -10,14 +10,17 @@ import io.ktor.client.request.* import io.ktor.client.statement.* import io.ktor.client.tests.utils.* import io.ktor.http.* +import io.ktor.utils.io.concurrent.* import kotlin.test.* class CallValidatorTest { + private var firstHandler by shared(0) + private var secondHandler by shared(0) + private var handleTriggered by shared(false) + private var validator by shared(0) + @Test fun testAllExceptionHandlers() = testWithEngine(MockEngine) { - var firstHandler = 0 - var secondHandler = 0 - config { engine { addHandler { respondOk() } @@ -53,7 +56,6 @@ class CallValidatorTest { @Test fun testExceptionFromEngine() = testWithEngine(MockEngine) { - var handleTriggered = 0 config { engine { addHandler { throw CallValidatorTestException() } @@ -61,7 +63,7 @@ class CallValidatorTest { HttpResponseValidator { handleResponseException { assertTrue(it is CallValidatorTestException) - handleTriggered++ + firstHandler++ } } } @@ -71,13 +73,12 @@ class CallValidatorTest { } catch (_: CallValidatorTestException) { } - assertEquals(1, handleTriggered) + assertEquals(1, firstHandler) } } @Test fun testExceptionFromReceivePipeline() = testWithEngine(MockEngine) { - var handleTriggered = false config { engine { addHandler { respondOk() } @@ -102,9 +103,6 @@ class CallValidatorTest { @Test fun testMergeMultipleConfigs() = testWithEngine(MockEngine) { - var firstHandler = 0 - var secondHandler = 0 - config { engine { addHandler { respondOk() } @@ -127,22 +125,20 @@ class CallValidatorTest { test { client -> client.responsePipeline.intercept(HttpResponsePipeline.Transform) { throw CallValidatorTestException() } - var thirdHandler = false try { client.get() } catch (_: CallValidatorTestException) { - thirdHandler = true + handleTriggered = true } assertEquals(1, firstHandler) assertEquals(1, secondHandler) - assertTrue(thirdHandler) + assertTrue(handleTriggered) } } @Test fun testResponseValidation() = testWithEngine(MockEngine) { - var validator = 0 config { engine { addHandler { diff --git a/ktor-client/ktor-client-tests/common/test/io/ktor/client/tests/features/CacheTest.kt b/ktor-client/ktor-client-tests/common/test/io/ktor/client/tests/features/CacheTest.kt index 52ace8709ad..07653983bc3 100644 --- a/ktor-client/ktor-client-tests/common/test/io/ktor/client/tests/features/CacheTest.kt +++ b/ktor-client/ktor-client-tests/common/test/io/ktor/client/tests/features/CacheTest.kt @@ -8,13 +8,15 @@ import io.ktor.client.features.logging.* import io.ktor.client.request.* import io.ktor.client.tests.utils.* import io.ktor.http.* +import io.ktor.utils.io.concurrent.* import kotlinx.coroutines.* import kotlin.test.* class CacheTest : ClientLoader() { + var storage: HttpCache.Config? by shared(null) + @Test fun testNoStore() = clientTests { - var storage: HttpCache.Config? = null config { install(HttpCache) { storage = this @@ -38,7 +40,6 @@ class CacheTest : ClientLoader() { @Test fun testNoCache() = clientTests { - var storage: HttpCache.Config? = null config { install(HttpCache) { storage = this @@ -62,7 +63,6 @@ class CacheTest : ClientLoader() { @Test fun testETagCache() = clientTests(listOf("Js")) { - var storage: HttpCache.Config? = null config { install(HttpCache) { storage = this @@ -84,7 +84,6 @@ class CacheTest : ClientLoader() { @Test fun testLastModified() = clientTests(listOf("Js")) { - var storage: HttpCache.Config? = null config { install(HttpCache) { storage = this @@ -106,7 +105,6 @@ class CacheTest : ClientLoader() { @Test fun testVary() = clientTests(listOf("Js")) { - var storage: HttpCache.Config? = null config { install(HttpCache) { storage = this @@ -152,7 +150,6 @@ class CacheTest : ClientLoader() { @Test fun testMaxAge() = clientTests { - var storage: HttpCache.Config? = null config { install(HttpCache) { storage = this @@ -178,7 +175,6 @@ class CacheTest : ClientLoader() { @Test fun testPublicAndPrivateCache() = clientTests(listOf("native")) { - var storage: HttpCache.Config? = null config { install(HttpCache) { storage = this diff --git a/ktor-client/ktor-client-tests/common/test/io/ktor/client/tests/utils/Logging.kt b/ktor-client/ktor-client-tests/common/test/io/ktor/client/tests/utils/Logging.kt index 7216e300f7a..e085c498843 100644 --- a/ktor-client/ktor-client-tests/common/test/io/ktor/client/tests/utils/Logging.kt +++ b/ktor-client/ktor-client-tests/common/test/io/ktor/client/tests/utils/Logging.kt @@ -5,10 +5,11 @@ package io.ktor.client.tests.utils import io.ktor.client.features.logging.* +import io.ktor.util.collections.* import kotlin.test.* internal class TestLogger(private vararg val expectedLog: String) : Logger { - private val log = mutableListOf() + private val log = ConcurrentList() override fun log(message: String) { log += message