Skip to content

Commit

Permalink
KTOR-1067 Use shared references in test
Browse files Browse the repository at this point in the history
  • Loading branch information
e5l committed Sep 22, 2020
1 parent ff789b5 commit b8f5a96
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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() }
Expand Down Expand Up @@ -53,15 +56,14 @@ class CallValidatorTest {

@Test
fun testExceptionFromEngine() = testWithEngine(MockEngine) {
var handleTriggered = 0
config {
engine {
addHandler { throw CallValidatorTestException() }
}
HttpResponseValidator {
handleResponseException {
assertTrue(it is CallValidatorTestException)
handleTriggered++
firstHandler++
}
}
}
Expand All @@ -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() }
Expand All @@ -102,9 +103,6 @@ class CallValidatorTest {

@Test
fun testMergeMultipleConfigs() = testWithEngine(MockEngine) {
var firstHandler = 0
var secondHandler = 0

config {
engine {
addHandler { respondOk() }
Expand All @@ -127,22 +125,20 @@ class CallValidatorTest {
test { client ->
client.responsePipeline.intercept(HttpResponsePipeline.Transform) { throw CallValidatorTestException() }

var thirdHandler = false
try {
client.get<String>()
} 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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -38,7 +40,6 @@ class CacheTest : ClientLoader() {

@Test
fun testNoCache() = clientTests {
var storage: HttpCache.Config? = null
config {
install(HttpCache) {
storage = this
Expand All @@ -62,7 +63,6 @@ class CacheTest : ClientLoader() {

@Test
fun testETagCache() = clientTests(listOf("Js")) {
var storage: HttpCache.Config? = null
config {
install(HttpCache) {
storage = this
Expand All @@ -84,7 +84,6 @@ class CacheTest : ClientLoader() {

@Test
fun testLastModified() = clientTests(listOf("Js")) {
var storage: HttpCache.Config? = null
config {
install(HttpCache) {
storage = this
Expand All @@ -106,7 +105,6 @@ class CacheTest : ClientLoader() {

@Test
fun testVary() = clientTests(listOf("Js")) {
var storage: HttpCache.Config? = null
config {
install(HttpCache) {
storage = this
Expand Down Expand Up @@ -152,7 +150,6 @@ class CacheTest : ClientLoader() {

@Test
fun testMaxAge() = clientTests {
var storage: HttpCache.Config? = null
config {
install(HttpCache) {
storage = this
Expand All @@ -178,7 +175,6 @@ class CacheTest : ClientLoader() {

@Test
fun testPublicAndPrivateCache() = clientTests(listOf("native")) {
var storage: HttpCache.Config? = null
config {
install(HttpCache) {
storage = this
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>()
private val log = ConcurrentList<String>()

override fun log(message: String) {
log += message
Expand Down

0 comments on commit b8f5a96

Please sign in to comment.