Skip to content

Commit b1ab8f3

Browse files
author
Oleksandr Dzhychko
authored
Merge pull request #235 from modelix/feature/make-modelclientv2-closable
Feature/make modelclientv2 closable
2 parents 08c61aa + 7ef24f9 commit b1ab8f3

File tree

6 files changed

+61
-13
lines changed

6 files changed

+61
-13
lines changed

gradle/libs.versions.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ ktor-server-websockets = { group = "io.ktor", name = "ktor-server-websockets", v
5555

5656
ktor-client-core = { group = "io.ktor", name = "ktor-client-core", version.ref = "ktor" }
5757
ktor-client-content-negotiation = { group = "io.ktor", name = "ktor-client-content-negotiation", version.ref = "ktor" }
58+
ktor-client-mock = { group = "io.ktor", name = "ktor-client-mock", version.ref = "ktor" }
5859
ktor-client-cio = { group = "io.ktor", name = "ktor-client-cio", version.ref = "ktor" }
5960
ktor-client-websockets = { group = "io.ktor", name = "ktor-client-websockets", version.ref = "ktor" }
6061
ktor-client-js = { group = "io.ktor", name = "ktor-client-js", version.ref = "ktor" }

model-client/build.gradle.kts

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,9 @@ kotlin {
5555
}
5656
val commonTest by getting {
5757
dependencies {
58-
kotlin("test-common")
59-
kotlin("test-annotations-common")
58+
implementation(libs.ktor.client.mock)
59+
implementation(libs.kotlin.coroutines.test)
60+
implementation(kotlin("test"))
6061
}
6162
}
6263
val jvmMain by getting {
@@ -81,11 +82,6 @@ kotlin {
8182
implementation(libs.ktor.serialization.json)
8283
}
8384
}
84-
val jvmTest by getting {
85-
dependencies {
86-
implementation(kotlin("test"))
87-
}
88-
}
8985
val jsMain by getting {
9086
dependencies {
9187
implementation(kotlin("stdlib-js"))
@@ -94,11 +90,6 @@ kotlin {
9490
implementation(npm("js-base64", "^3.4.5"))
9591
}
9692
}
97-
val jsTest by getting {
98-
dependencies {
99-
implementation(kotlin("test-js"))
100-
}
101-
}
10293
}
10394
}
10495

model-client/src/commonMain/kotlin/org/modelix/model/client2/IModelClientV2.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,14 @@
1313
*/
1414
package org.modelix.model.client2
1515

16+
import io.ktor.utils.io.core.Closeable
1617
import org.modelix.model.IVersion
1718
import org.modelix.model.api.IIdGenerator
1819
import org.modelix.model.lazy.BranchReference
1920
import org.modelix.model.lazy.RepositoryId
2021
import org.modelix.model.server.api.ModelQuery
2122

22-
interface IModelClientV2 {
23+
interface IModelClientV2 : Closeable {
2324
fun getClientId(): Int
2425
fun getIdGenerator(): IIdGenerator
2526
fun getUserId(): String?
@@ -44,4 +45,6 @@ interface IModelClientV2 {
4445

4546
suspend fun poll(branch: BranchReference, lastKnownVersion: IVersion?): IVersion
4647
suspend fun poll(branch: BranchReference, lastKnownVersion: IVersion?, filter: ModelQuery): IVersion
48+
49+
override fun close()
4750
}

model-client/src/commonMain/kotlin/org/modelix/model/client2/ModelClientV2.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,10 @@ class ModelClientV2(
190190
TODO("Not yet implemented")
191191
}
192192

193+
override fun close() {
194+
httpClient.close()
195+
}
196+
193197
private fun createVersion(baseVersion: CLVersion?, delta: VersionDelta): CLVersion {
194198
return if (baseVersion == null) {
195199
CLVersion(

model-client/src/commonMain/kotlin/org/modelix/model/client2/ReplicatedModel.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import org.modelix.model.lazy.CLTree
1717
import org.modelix.model.lazy.CLVersion
1818
import org.modelix.model.operations.OTBranch
1919
import org.modelix.model.server.api.ModelQuery
20+
import kotlin.coroutines.cancellation.CancellationException
2021

2122
class ReplicatedModel(val client: IModelClientV2, val branchRef: BranchReference, val query: ModelQuery? = null) {
2223
private val scope = CoroutineScope(Dispatchers.Default)
@@ -62,6 +63,9 @@ class ReplicatedModel(val client: IModelClientV2, val branchRef: BranchReference
6263
} as CLVersion
6364
remoteVersionReceived(newRemoteVersion)
6465
nextDelayMs = 0
66+
} catch (ex: CancellationException) {
67+
LOG.debug { "Stop to poll branch $branchRef after disposing." }
68+
throw ex
6569
} catch (ex: Throwable) {
6670
LOG.error(ex) { "Failed to poll branch $branchRef" }
6771
nextDelayMs = (nextDelayMs * 3 / 2).coerceIn(1000, 30000)
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Licensed under the Apache License, Version 2.0 (the "License");
3+
* you may not use this file except in compliance with the License.
4+
* You may obtain a copy of the License at
5+
*
6+
* http://www.apache.org/licenses/LICENSE-2.0
7+
*
8+
* Unless required by applicable law or agreed to in writing,
9+
* software distributed under the License is distributed on an
10+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
11+
* KIND, either express or implied. See the License for the
12+
* specific language governing permissions and limitations
13+
* under the License.
14+
*/
15+
16+
package org.modelix.model.client2
17+
18+
import io.ktor.client.HttpClient
19+
import io.ktor.client.engine.mock.MockEngine
20+
import io.ktor.client.engine.mock.respondError
21+
import io.ktor.http.HttpStatusCode
22+
import kotlinx.coroutines.CancellationException
23+
import kotlinx.coroutines.test.runTest
24+
import kotlin.test.Test
25+
import kotlin.test.assertFailsWith
26+
27+
class ModelClientV2Test {
28+
29+
@Test
30+
fun disposeClient() = runTest {
31+
val url = "http://localhost/v2"
32+
val mockEngine = MockEngine {
33+
respondError(HttpStatusCode.NotFound)
34+
}
35+
val httpClient = HttpClient(mockEngine)
36+
val modelClient = ModelClientV2.builder()
37+
.client(httpClient)
38+
.url(url)
39+
.build()
40+
modelClient.close()
41+
assertFailsWith<CancellationException>("Parent job is Completed") {
42+
modelClient.init()
43+
}
44+
}
45+
}

0 commit comments

Comments
 (0)