Skip to content

Commit 4b09f8f

Browse files
author
Oleksandr Dzhychko
authored
Merge pull request #239 from modelix/bugfix/remove-unneeded-dependency-to-closable
fix(model-client): change wrong dependency to Closable
2 parents a1d5c09 + 7be1dd2 commit 4b09f8f

File tree

6 files changed

+130
-5
lines changed

6 files changed

+130
-5
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/*
2+
* Copyright (c) 2023.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.modelix.model.client2
18+
19+
internal expect interface Closable {
20+
fun close()
21+
}

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

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

16-
import io.ktor.utils.io.core.Closeable
1716
import org.modelix.model.IVersion
1817
import org.modelix.model.api.IIdGenerator
1918
import org.modelix.model.lazy.BranchReference
2019
import org.modelix.model.lazy.RepositoryId
2120
import org.modelix.model.server.api.ModelQuery
2221

23-
interface IModelClientV2 : Closeable {
22+
/**
23+
* This interface is meant exclusively for model client usage.
24+
*
25+
* It is designed to ensure decoupling between model client usage operations and other aspects,
26+
* such as lifecycle management.
27+
* Users of this interface cannot incidentally depend on non-usage functionality.
28+
* See also: [Interface segregation principle](https://en.wikipedia.org/wiki/Interface_segregation_principle)
29+
*
30+
* Specifically, this interface should not be used for managing the client's lifecycle,
31+
* as the lifecycle management may vary depending on the specific implementation.
32+
* If you need to manage the client's lifecycle, use the methods in the class interface of the concrete implementations,
33+
* such as [ModelClientV2].
34+
*/
35+
interface IModelClientV2 {
2436
fun getClientId(): Int
2537
fun getIdGenerator(): IIdGenerator
2638
fun getUserId(): String?
@@ -45,6 +57,4 @@ interface IModelClientV2 : Closeable {
4557

4658
suspend fun poll(branch: BranchReference, lastKnownVersion: IVersion?): IVersion
4759
suspend fun poll(branch: BranchReference, lastKnownVersion: IVersion?, filter: ModelQuery): IVersion
48-
49-
override fun close()
5060
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ import kotlin.time.Duration.Companion.seconds
5050
class ModelClientV2(
5151
private val httpClient: HttpClient,
5252
val baseUrl: String,
53-
) : IModelClientV2 {
53+
) : IModelClientV2, Closable {
5454
private var clientId: Int = 0
5555
private var idGenerator: IIdGenerator = IdGeneratorDummy()
5656
private var userId: String? = null
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/*
2+
* Copyright (c) 2023.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.modelix.model.client2
18+
19+
internal actual interface Closable {
20+
actual fun close()
21+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/*
2+
* Copyright (c) 2023.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.modelix.model.client2
18+
19+
internal actual interface Closable : java.io.Closeable {
20+
actual override fun close()
21+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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 ModelClientV2JvmTest {
28+
29+
@Test
30+
fun `Java implementation implements closable functionality`() = 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+
// Implementing `close` allow to use `.use` method.
41+
modelClient.use {
42+
}
43+
assertFailsWith<CancellationException>("Parent job is Completed") {
44+
modelClient.init()
45+
}
46+
// `Closable` implies, that `.close` method is idempotent.
47+
modelClient.close()
48+
assertFailsWith<CancellationException>("Parent job is Completed") {
49+
modelClient.init()
50+
}
51+
}
52+
}

0 commit comments

Comments
 (0)