|
3 | 3 | */ |
4 | 4 |
|
5 | 5 | import io.ktor.client.* |
| 6 | +import io.ktor.client.engine.* |
6 | 7 | import io.ktor.client.plugins.api.* |
| 8 | +import io.ktor.client.request.* |
| 9 | +import io.ktor.http.* |
| 10 | +import io.ktor.util.date.* |
| 11 | +import io.ktor.utils.io.* |
| 12 | +import kotlinx.coroutines.Job |
| 13 | +import kotlinx.coroutines.test.runTest |
| 14 | +import kotlin.coroutines.EmptyCoroutineContext |
7 | 15 | import kotlin.test.Test |
8 | 16 | import kotlin.test.assertEquals |
| 17 | +import kotlin.test.assertFalse |
| 18 | +import kotlin.test.assertSame |
| 19 | +import kotlin.test.assertTrue |
9 | 20 |
|
10 | 21 | class HttpClientConfigTest { |
11 | 22 |
|
@@ -41,4 +52,88 @@ class HttpClientConfigTest { |
41 | 52 | assertEquals(1, first) |
42 | 53 | assertEquals(1, second) |
43 | 54 | } |
| 55 | + |
| 56 | + @Test |
| 57 | + fun closingChildDoesNotCloseEngine() = runTest { |
| 58 | + var engineClosed = false |
| 59 | + val client = HttpClient(createTestEngineFactory { engineClosed = true }) |
| 60 | + val child = client.config {} |
| 61 | + |
| 62 | + assertSame(client.engine, child.engine) |
| 63 | + |
| 64 | + child.close() |
| 65 | + |
| 66 | + assertFalse(engineClosed) |
| 67 | + client.close() |
| 68 | + assertTrue(engineClosed) |
| 69 | + } |
| 70 | + |
| 71 | + @Test |
| 72 | + fun closingParentDoesNotCloseEngineWhileChildIsOpen() = runTest { |
| 73 | + var engineClosed = false |
| 74 | + val client = HttpClient(createTestEngineFactory { engineClosed = true }) |
| 75 | + val child = client.config {} |
| 76 | + |
| 77 | + assertSame(client.engine, child.engine) |
| 78 | + |
| 79 | + client.close() |
| 80 | + |
| 81 | + assertFalse(engineClosed) |
| 82 | + child.close() |
| 83 | + assertTrue(engineClosed) |
| 84 | + } |
| 85 | + |
| 86 | + @Test |
| 87 | + fun cancellingChildDoesNotCancelEngine() = runTest { |
| 88 | + val client = HttpClient(createTestEngineFactory {}) |
| 89 | + val child = client.config {} |
| 90 | + |
| 91 | + assertSame(client.engine, child.engine) |
| 92 | + |
| 93 | + child.coroutineContext[Job]!!.cancel() |
| 94 | + |
| 95 | + assertFalse(client.engine.coroutineContext[Job]!!.isCancelled) |
| 96 | + client.coroutineContext[Job]!!.cancel() |
| 97 | + assertTrue(client.engine.coroutineContext[Job]!!.isCancelled) |
| 98 | + } |
| 99 | + |
| 100 | + @Test |
| 101 | + fun cancellingParentDoesNotCancelEngineWhileChildIsOpen() = runTest { |
| 102 | + val client = HttpClient(createTestEngineFactory {}) |
| 103 | + val child = client.config {} |
| 104 | + |
| 105 | + assertSame(client.engine, child.engine) |
| 106 | + |
| 107 | + client.coroutineContext[Job]!!.cancel() |
| 108 | + |
| 109 | + assertFalse(child.engine.coroutineContext[Job]!!.isCancelled) |
| 110 | + child.coroutineContext[Job]!!.cancel() |
| 111 | + assertTrue(child.engine.coroutineContext[Job]!!.isCancelled) |
| 112 | + } |
| 113 | + |
| 114 | + private fun createTestEngineFactory(onClose: () -> Unit): HttpClientEngineFactory<HttpClientEngineConfig> { |
| 115 | + val engine = object : HttpClientEngineBase("test-engine") { |
| 116 | + override val config: HttpClientEngineConfig = HttpClientEngineConfig() |
| 117 | + |
| 118 | + @InternalAPI |
| 119 | + override suspend fun execute(data: HttpRequestData): HttpResponseData { |
| 120 | + return HttpResponseData( |
| 121 | + HttpStatusCode.OK, |
| 122 | + GMTDate.START, |
| 123 | + Headers.Empty, |
| 124 | + HttpProtocolVersion.HTTP_1_1, |
| 125 | + Unit, |
| 126 | + EmptyCoroutineContext |
| 127 | + ) |
| 128 | + } |
| 129 | + |
| 130 | + override fun close() { |
| 131 | + super.close() |
| 132 | + onClose() |
| 133 | + } |
| 134 | + } |
| 135 | + return object : HttpClientEngineFactory<HttpClientEngineConfig> { |
| 136 | + override fun create(block: HttpClientEngineConfig.() -> Unit) = engine |
| 137 | + } |
| 138 | + } |
44 | 139 | } |
0 commit comments