diff --git a/driver-multiplatform-wire/src/commonMain/kotlin/MongoSocket.kt b/driver-multiplatform-wire/src/commonMain/kotlin/MongoSocket.kt new file mode 100644 index 00000000..5c1d5c01 --- /dev/null +++ b/driver-multiplatform-wire/src/commonMain/kotlin/MongoSocket.kt @@ -0,0 +1,60 @@ +/* + * Copyright (c) 2026, OpenSavvy and contributors. + * + * 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 opensavvy.ktmongo.multiplatform.wire + +import io.ktor.network.selector.* +import io.ktor.network.sockets.* +import io.ktor.utils.io.* +import kotlinx.coroutines.isActive + +/** + * Custom abstraction to allow creating fake sockets for testing. + */ +internal interface MongoSocket : AutoCloseable { + + val isActive: Boolean + + fun openReadChannel(): ByteReadChannel + + fun openWriteChannel(): ByteWriteChannel +} + +private class KtorMongoSocket( + private val socket: Socket, + private val selectorManager: SelectorManager, +) : MongoSocket { + + override val isActive: Boolean + get() = socket.isActive + + override fun openReadChannel(): ByteReadChannel = + socket.openReadChannel() + + override fun openWriteChannel(): ByteWriteChannel = + socket.openWriteChannel() + + override fun close() { + socket.close() + selectorManager.close() + } + + override fun toString(): String = + "Ktor ${socket.remoteAddress}" +} + +internal fun MongoSocket(socket: Socket, selectorManager: SelectorManager): MongoSocket = + KtorMongoSocket(socket, selectorManager) diff --git a/driver-multiplatform-wire/src/commonMain/kotlin/MongoWireClient.kt b/driver-multiplatform-wire/src/commonMain/kotlin/MongoWireClient.kt index 9a8a0cb9..c1a6e445 100644 --- a/driver-multiplatform-wire/src/commonMain/kotlin/MongoWireClient.kt +++ b/driver-multiplatform-wire/src/commonMain/kotlin/MongoWireClient.kt @@ -64,8 +64,7 @@ interface MongoWireClient : AutoCloseable { */ @LowLevelApi private class SocketWireClient( - private val socket: Socket, - private val selectorManager: SelectorManager, + private val socket: MongoSocket, private val factory: BsonFactory, coroutineScope: CoroutineScope, // Should contain a Job dedicated to this client ) : MongoWireClient { @@ -113,7 +112,7 @@ private class SocketWireClient( } init { - log("Creating client for socket ${socket.remoteAddress}") + log("Creating client for socket $socket") // Ensure that no resources can leak actorsJob.invokeOnCompletion { close() } @@ -288,10 +287,9 @@ private class SocketWireClient( override fun close() { actorsJob.cancel("${this::class}.close() has been called") socket.close() - selectorManager.close() } - override fun toString() = "MongoWireClient(${socket.remoteAddress})" + override fun toString() = "MongoWireClient($socket)" } @LowLevelApi @@ -309,8 +307,7 @@ suspend fun MongoWireClient( } return SocketWireClient( - socket = socket, - selectorManager = selectorManager, + socket = MongoSocket(socket, selectorManager), factory = factory, coroutineScope = CoroutineScope(coroutineContext + innerJob + CoroutineName("ktmongo-client")) )