Skip to content

Commit

Permalink
Merge pull request #235 from modelix/feature/make-modelclientv2-closable
Browse files Browse the repository at this point in the history
Feature/make modelclientv2 closable
  • Loading branch information
Oleksandr Dzhychko authored Sep 6, 2023
2 parents 08c61aa + 7ef24f9 commit b1ab8f3
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 13 deletions.
1 change: 1 addition & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ ktor-server-websockets = { group = "io.ktor", name = "ktor-server-websockets", v

ktor-client-core = { group = "io.ktor", name = "ktor-client-core", version.ref = "ktor" }
ktor-client-content-negotiation = { group = "io.ktor", name = "ktor-client-content-negotiation", version.ref = "ktor" }
ktor-client-mock = { group = "io.ktor", name = "ktor-client-mock", version.ref = "ktor" }
ktor-client-cio = { group = "io.ktor", name = "ktor-client-cio", version.ref = "ktor" }
ktor-client-websockets = { group = "io.ktor", name = "ktor-client-websockets", version.ref = "ktor" }
ktor-client-js = { group = "io.ktor", name = "ktor-client-js", version.ref = "ktor" }
Expand Down
15 changes: 3 additions & 12 deletions model-client/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,9 @@ kotlin {
}
val commonTest by getting {
dependencies {
kotlin("test-common")
kotlin("test-annotations-common")
implementation(libs.ktor.client.mock)
implementation(libs.kotlin.coroutines.test)
implementation(kotlin("test"))
}
}
val jvmMain by getting {
Expand All @@ -81,11 +82,6 @@ kotlin {
implementation(libs.ktor.serialization.json)
}
}
val jvmTest by getting {
dependencies {
implementation(kotlin("test"))
}
}
val jsMain by getting {
dependencies {
implementation(kotlin("stdlib-js"))
Expand All @@ -94,11 +90,6 @@ kotlin {
implementation(npm("js-base64", "^3.4.5"))
}
}
val jsTest by getting {
dependencies {
implementation(kotlin("test-js"))
}
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,14 @@
*/
package org.modelix.model.client2

import io.ktor.utils.io.core.Closeable
import org.modelix.model.IVersion
import org.modelix.model.api.IIdGenerator
import org.modelix.model.lazy.BranchReference
import org.modelix.model.lazy.RepositoryId
import org.modelix.model.server.api.ModelQuery

interface IModelClientV2 {
interface IModelClientV2 : Closeable {
fun getClientId(): Int
fun getIdGenerator(): IIdGenerator
fun getUserId(): String?
Expand All @@ -44,4 +45,6 @@ interface IModelClientV2 {

suspend fun poll(branch: BranchReference, lastKnownVersion: IVersion?): IVersion
suspend fun poll(branch: BranchReference, lastKnownVersion: IVersion?, filter: ModelQuery): IVersion

override fun close()
}
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,10 @@ class ModelClientV2(
TODO("Not yet implemented")
}

override fun close() {
httpClient.close()
}

private fun createVersion(baseVersion: CLVersion?, delta: VersionDelta): CLVersion {
return if (baseVersion == null) {
CLVersion(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import org.modelix.model.lazy.CLTree
import org.modelix.model.lazy.CLVersion
import org.modelix.model.operations.OTBranch
import org.modelix.model.server.api.ModelQuery
import kotlin.coroutines.cancellation.CancellationException

class ReplicatedModel(val client: IModelClientV2, val branchRef: BranchReference, val query: ModelQuery? = null) {
private val scope = CoroutineScope(Dispatchers.Default)
Expand Down Expand Up @@ -62,6 +63,9 @@ class ReplicatedModel(val client: IModelClientV2, val branchRef: BranchReference
} as CLVersion
remoteVersionReceived(newRemoteVersion)
nextDelayMs = 0
} catch (ex: CancellationException) {
LOG.debug { "Stop to poll branch $branchRef after disposing." }
throw ex
} catch (ex: Throwable) {
LOG.error(ex) { "Failed to poll branch $branchRef" }
nextDelayMs = (nextDelayMs * 3 / 2).coerceIn(1000, 30000)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package org.modelix.model.client2

import io.ktor.client.HttpClient
import io.ktor.client.engine.mock.MockEngine
import io.ktor.client.engine.mock.respondError
import io.ktor.http.HttpStatusCode
import kotlinx.coroutines.CancellationException
import kotlinx.coroutines.test.runTest
import kotlin.test.Test
import kotlin.test.assertFailsWith

class ModelClientV2Test {

@Test
fun disposeClient() = runTest {
val url = "http://localhost/v2"
val mockEngine = MockEngine {
respondError(HttpStatusCode.NotFound)
}
val httpClient = HttpClient(mockEngine)
val modelClient = ModelClientV2.builder()
.client(httpClient)
.url(url)
.build()
modelClient.close()
assertFailsWith<CancellationException>("Parent job is Completed") {
modelClient.init()
}
}
}

0 comments on commit b1ab8f3

Please # to comment.