From 951018f12499efef7357cec1bf449d4e4f672f4d Mon Sep 17 00:00:00 2001 From: Piasy Date: Sat, 11 Jan 2025 21:02:18 +0800 Subject: [PATCH] passing more Java version's tests --- README.md | 5 + build.gradle.kts | 1 + gradle/libs.versions.toml | 11 +- gradle/wrapper/gradle-wrapper.properties | 2 +- .../kmp/socketio/engineio/EngineSocket.kt | 6 +- .../piasy/kmp/socketio/engineio/Transport.kt | 23 +- .../engineio/transports/PollingXHR.kt | 20 +- .../socketio/engineio/transports/WebSocket.kt | 4 +- .../com/piasy/kmp/socketio/logging/Logger.kt | 14 +- .../piasy/kmp/socketio/socketio/Manager.kt | 5 +- .../com/piasy/kmp/socketio/socketio/Socket.kt | 3 +- .../java/io/socket/client/Connection.java | 2 +- .../client/ServerConnectionNamespaceTest.java | 12 - .../socket/client/ServerConnectionTest.java | 749 ++++++++------- .../java/io/socket/client/SocketTest.java | 876 ++++++++++-------- .../io/socket/engineio/client/Connection.java | 2 - .../engineio/client/ConnectionTest.java | 6 +- .../engineio/client/ServerConnectionTest.java | 2 +- .../kmp/socketio/engineio/EngineSocketTest.kt | 12 +- .../piasy/kmp/socketio/engineio/TestUtil.kt | 16 +- .../engineio/transports/PollingXHRTest.kt | 2 + .../com/piasy/kmp/socketio/engineio/utils.kt | 21 - 22 files changed, 952 insertions(+), 842 deletions(-) delete mode 100644 kmp-socketio/src/jvmTest/java/io/socket/client/ServerConnectionNamespaceTest.java diff --git a/README.md b/README.md index 56a438e..e997dd9 100644 --- a/README.md +++ b/README.md @@ -57,6 +57,11 @@ IO.socket("http://localhost:3000", IO.Options()) { socket -> } ``` +Most of the APIs are the same as socket.io-client-java, here are some differences: + +- Create socket is asyncronous, to make it's easier to guarantee thread safety. +- Binary messages can't be nested, because `emit` only accepts String/Boolean/Number/JsonElement/ByteArray, other types will be converted to String using `toString()`, so there is no way to put ByteArray in JsonElement. + ## Example ### Android diff --git a/build.gradle.kts b/build.gradle.kts index 99ef860..afcf81f 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -20,6 +20,7 @@ plugins { alias(libs.plugins.versionUpdate) } +// ./gradlew versionCatalogUpdate versionCatalogUpdate { sortByKey = false keep { diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 63398db..add2e9d 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -4,22 +4,21 @@ iosDeploymentTarget = "14.0" compileSdk = "35" minSdk = "21" targetSdk = "35" -agp = "8.7.2" +agp = "8.8.0" kotlin = "2.1.0" -mockk = "1.13.13" +mockk = "1.13.16" ktor = "3.0.3" coroutine = "1.10.1" compose = "1.7.5" [libraries] junit = "junit:junit:4.13.2" -hamcrest = "org.hamcrest:hamcrest-library:1.3" +hamcrest = "org.hamcrest:hamcrest-library:3.0" kotlin-test = { module = "org.jetbrains.kotlin:kotlin-test", version.ref = "kotlin" } mockk = { module = "io.mockk:mockk", version.ref = "mockk" } kotlinx-coroutines-test = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-test", version.ref = "coroutine" } -json = "org.json:json:20241224" - -socketioParser = "org.hildan.socketio:socketio-kotlin:1.6.0" +json = "org.json:json:20250107" +socketioParser = "org.hildan.socketio:socketio-kotlin:1.7.1" kotlinx-coroutines-core = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-core", version.ref = "coroutine" } ktor-client-core = { module = "io.ktor:ktor-client-core", version.ref = "ktor" } ktor-client-websockets = { module = "io.ktor:ktor-client-websockets", version.ref = "ktor" } diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index 4275805..993c6f9 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,6 +1,6 @@ #Wed Nov 16 22:27:43 CST 2022 distributionBase=GRADLE_USER_HOME -distributionUrl=https\://services.gradle.org/distributions/gradle-8.9-all.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-8.12-all.zip distributionPath=wrapper/dists zipStorePath=wrapper/dists zipStoreBase=GRADLE_USER_HOME diff --git a/kmp-socketio/src/commonMain/kotlin/com/piasy/kmp/socketio/engineio/EngineSocket.kt b/kmp-socketio/src/commonMain/kotlin/com/piasy/kmp/socketio/engineio/EngineSocket.kt index 61e77f9..a54ba02 100644 --- a/kmp-socketio/src/commonMain/kotlin/com/piasy/kmp/socketio/engineio/EngineSocket.kt +++ b/kmp-socketio/src/commonMain/kotlin/com/piasy/kmp/socketio/engineio/EngineSocket.kt @@ -42,7 +42,7 @@ class EngineSocket( internal var disablePingTimeout = false // to help unit test private var state = State.INIT - private var id = "" + internal var id = "" private var upgrades = emptyList() private var pingInterval = 0 private var pingTimeout = 0 @@ -317,7 +317,6 @@ class EngineSocket( val data = packet.payload if (data != null) { emit(EVENT_DATA, data) - emit(EVENT_MESSAGE, data) } } @@ -591,7 +590,7 @@ class EngineSocket( /** * Called when data is received from the server. */ - const val EVENT_MESSAGE = "message" + const val EVENT_DATA = "data" /** * Called when an error occurs. @@ -618,7 +617,6 @@ class EngineSocket( const val EVENT_PACKET = "packet" const val EVENT_PACKET_CREATE = "packetCreate" const val EVENT_HEARTBEAT = "heartbeat" - const val EVENT_DATA = "data" const val EVENT_PING = "ping" /** diff --git a/kmp-socketio/src/commonMain/kotlin/com/piasy/kmp/socketio/engineio/Transport.kt b/kmp-socketio/src/commonMain/kotlin/com/piasy/kmp/socketio/engineio/Transport.kt index 491b153..12d8a5e 100644 --- a/kmp-socketio/src/commonMain/kotlin/com/piasy/kmp/socketio/engineio/Transport.kt +++ b/kmp-socketio/src/commonMain/kotlin/com/piasy/kmp/socketio/engineio/Transport.kt @@ -7,6 +7,7 @@ import io.ktor.util.date.* import kotlinx.coroutines.CoroutineScope import org.hildan.socketio.EngineIO import org.hildan.socketio.EngineIOPacket +import org.hildan.socketio.InvalidSocketIOPacketException import kotlin.jvm.JvmField abstract class Transport( @@ -89,18 +90,26 @@ abstract class Transport( } @WorkThread - protected fun onWsData(data: String) { - logD("onData: `$data`") - if (stringMessagePayloadForTesting) { - onPacket(EngineIO.decodeWsFrame(data, deserializePayload = { it })) - } else { - onPacket(EngineIO.decodeSocketIO(data)) + protected fun onWsText(data: String) { + logD("onWsText: `$data`") + val packet = try { + if (stringMessagePayloadForTesting) { + EngineIO.decodeWsFrame(data, deserializePayload = { it }) + } else { + EngineIO.decodeSocketIO(data) + } + } catch (e: InvalidSocketIOPacketException) { + val log = "onWsText decode error: ${e.message}" + logE(log) + onError(log) + return } + onPacket(packet) } @WorkThread - protected fun onWsData(data: ByteArray) { // TODO: binary + protected fun onWsBinary(data: ByteArray) { } @WorkThread diff --git a/kmp-socketio/src/commonMain/kotlin/com/piasy/kmp/socketio/engineio/transports/PollingXHR.kt b/kmp-socketio/src/commonMain/kotlin/com/piasy/kmp/socketio/engineio/transports/PollingXHR.kt index 05824eb..78368e3 100644 --- a/kmp-socketio/src/commonMain/kotlin/com/piasy/kmp/socketio/engineio/transports/PollingXHR.kt +++ b/kmp-socketio/src/commonMain/kotlin/com/piasy/kmp/socketio/engineio/transports/PollingXHR.kt @@ -11,10 +11,7 @@ import io.ktor.util.* import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch -import org.hildan.socketio.EngineIO -import org.hildan.socketio.EngineIOPacket -import org.hildan.socketio.SocketIO -import org.hildan.socketio.SocketIOPacket +import org.hildan.socketio.* open class PollingXHR( opt: Options, @@ -138,10 +135,17 @@ open class PollingXHR( @WorkThread private fun onPollComplete(data: String) { logD("onPollComplete: state $state, `$data`") - val packets = if (stringMessagePayloadForTesting) { - EngineIO.decodeHttpBatch(data, deserializeTextPayload = { it }) - } else { - EngineIO.decodeHttpBatch(data, SocketIO::decode) + val packets = try { + if (stringMessagePayloadForTesting) { + EngineIO.decodeHttpBatch(data, deserializeTextPayload = { it }) + } else { + EngineIO.decodeHttpBatch(data, SocketIO::decode) + } + } catch (e: InvalidSocketIOPacketException) { + val log = "onPollComplete decode error: ${e.message}" + logE(log) + onError(log) + return } for (pkt in packets) { if ((state == State.OPENING || state == State.CLOSING) && pkt is EngineIOPacket.Open) { diff --git a/kmp-socketio/src/commonMain/kotlin/com/piasy/kmp/socketio/engineio/transports/WebSocket.kt b/kmp-socketio/src/commonMain/kotlin/com/piasy/kmp/socketio/engineio/transports/WebSocket.kt index 184cd5d..add33a4 100644 --- a/kmp-socketio/src/commonMain/kotlin/com/piasy/kmp/socketio/engineio/transports/WebSocket.kt +++ b/kmp-socketio/src/commonMain/kotlin/com/piasy/kmp/socketio/engineio/transports/WebSocket.kt @@ -70,11 +70,11 @@ open class WebSocket( logD("Receive frame: $frame") when (frame) { is Frame.Text -> { - scope.launch { onWsData(frame.readText()) } + scope.launch { onWsText(frame.readText()) } } is Frame.Binary -> { - scope.launch { onWsData(frame.readBytes()) } + scope.launch { onWsBinary(frame.readBytes()) } } is Frame.Close -> { diff --git a/kmp-socketio/src/commonMain/kotlin/com/piasy/kmp/socketio/logging/Logger.kt b/kmp-socketio/src/commonMain/kotlin/com/piasy/kmp/socketio/logging/Logger.kt index fbdd25e..6893533 100644 --- a/kmp-socketio/src/commonMain/kotlin/com/piasy/kmp/socketio/logging/Logger.kt +++ b/kmp-socketio/src/commonMain/kotlin/com/piasy/kmp/socketio/logging/Logger.kt @@ -1,5 +1,7 @@ package com.piasy.kmp.socketio.logging +import io.ktor.util.date.* + interface LoggerInterface { fun debug(tag: String, log: String) fun info(tag: String, log: String) @@ -7,7 +9,11 @@ interface LoggerInterface { } internal object Logger : LoggerInterface { - internal var logger: LoggerInterface = DefaultLogger + private var logger: LoggerInterface = DefaultLogger + + fun setLogger(logger: LoggerInterface) { + this.logger = logger + } override fun debug(tag: String, log: String) { logger.debug(tag, log) @@ -24,14 +30,14 @@ internal object Logger : LoggerInterface { private object DefaultLogger : LoggerInterface { override fun debug(tag: String, log: String) { - println("D $tag $log") + println("${GMTDate().timestamp} D $tag $log") } override fun info(tag: String, log: String) { - println("I $tag $log") + println("${GMTDate().timestamp} I $tag $log") } override fun error(tag: String, log: String) { - println("E $tag $log") + println("${GMTDate().timestamp} E $tag $log") } } diff --git a/kmp-socketio/src/commonMain/kotlin/com/piasy/kmp/socketio/socketio/Manager.kt b/kmp-socketio/src/commonMain/kotlin/com/piasy/kmp/socketio/socketio/Manager.kt index 6fe585c..fb5a5cb 100644 --- a/kmp-socketio/src/commonMain/kotlin/com/piasy/kmp/socketio/socketio/Manager.kt +++ b/kmp-socketio/src/commonMain/kotlin/com/piasy/kmp/socketio/socketio/Manager.kt @@ -47,6 +47,7 @@ class Manager( } field = value } + @JvmField var auth: Map = emptyMap() /** @@ -168,8 +169,8 @@ class Manager( val socket = engine ?: return subs.add(On.on(socket, EngineSocket.EVENT_DATA, object : Listener { override fun call(vararg args: Any) { - if (args.isNotEmpty() && args[0] is SocketIOPacket) { - Logger.debug(TAG, "on SocketIOPacket ${args[0]}") + if (args.isNotEmpty()) { + Logger.debug(TAG, "on EngineSocket data ${args[0]::class}") emit(EVENT_PACKET, args[0]) } } diff --git a/kmp-socketio/src/commonMain/kotlin/com/piasy/kmp/socketio/socketio/Socket.kt b/kmp-socketio/src/commonMain/kotlin/com/piasy/kmp/socketio/socketio/Socket.kt index d7fb0ca..0e567f9 100644 --- a/kmp-socketio/src/commonMain/kotlin/com/piasy/kmp/socketio/socketio/Socket.kt +++ b/kmp-socketio/src/commonMain/kotlin/com/piasy/kmp/socketio/socketio/Socket.kt @@ -197,7 +197,8 @@ class Socket( is SocketIOPacket.Disconnect -> onDisconnect() is SocketIOPacket.ConnectError -> { destroy() - super.emit(EVENT_CONNECT_ERROR, packet.errorData?.toString() ?: "Connect error") + val data = packet.errorData ?: JsonObject(emptyMap()) + super.emit(EVENT_CONNECT_ERROR, data) } is SocketIOPacket.Event -> onEvent(packet) diff --git a/kmp-socketio/src/jvmTest/java/io/socket/client/Connection.java b/kmp-socketio/src/jvmTest/java/io/socket/client/Connection.java index afcc642..1866599 100644 --- a/kmp-socketio/src/jvmTest/java/io/socket/client/Connection.java +++ b/kmp-socketio/src/jvmTest/java/io/socket/client/Connection.java @@ -30,7 +30,7 @@ public abstract class Connection { @Before public void startServer() throws IOException, InterruptedException { - TestUtil.setupLogger(); + TestUtil.stringMessagePayloadForTesting(false); logger.fine("Starting server ..."); final CountDownLatch latch = new CountDownLatch(1); diff --git a/kmp-socketio/src/jvmTest/java/io/socket/client/ServerConnectionNamespaceTest.java b/kmp-socketio/src/jvmTest/java/io/socket/client/ServerConnectionNamespaceTest.java deleted file mode 100644 index 60cf48c..0000000 --- a/kmp-socketio/src/jvmTest/java/io/socket/client/ServerConnectionNamespaceTest.java +++ /dev/null @@ -1,12 +0,0 @@ -//package io.socket.client; -// -//import org.junit.runner.RunWith; -//import org.junit.runners.JUnit4; -// -//@RunWith(JUnit4.class) -//public class ServerConnectionNamespaceTest extends ServerConnectionTest { -// -// protected String nsp() { -// return "/foo"; -// } -//} diff --git a/kmp-socketio/src/jvmTest/java/io/socket/client/ServerConnectionTest.java b/kmp-socketio/src/jvmTest/java/io/socket/client/ServerConnectionTest.java index 366d4c3..5c09000 100644 --- a/kmp-socketio/src/jvmTest/java/io/socket/client/ServerConnectionTest.java +++ b/kmp-socketio/src/jvmTest/java/io/socket/client/ServerConnectionTest.java @@ -1,356 +1,411 @@ -//package io.socket.client; -// -//import io.socket.emitter.Emitter; -//import io.socket.engineio.client.Transport; -//import io.socket.engineio.client.transports.Polling; -//import io.socket.engineio.client.transports.WebSocket; -//import org.json.JSONObject; -//import org.junit.Test; -//import org.junit.runner.RunWith; -//import org.junit.runners.JUnit4; -// -//import java.util.Arrays; -//import java.util.List; -//import java.util.Map; -//import java.util.concurrent.BlockingQueue; -//import java.util.concurrent.LinkedBlockingQueue; -// -//import static org.hamcrest.CoreMatchers.*; -//import static org.junit.Assert.assertThat; -// -//@RunWith(JUnit4.class) -//public class ServerConnectionTest extends Connection { -// -// private Socket socket; -// private Socket socket2; -// -// @Test(timeout = TIMEOUT) -// public void openAndClose() throws InterruptedException { -// final BlockingQueue values = new LinkedBlockingQueue<>(); -// -// socket = client(); -// socket.on(Socket.EVENT_CONNECT, new Emitter.Listener() { -// @Override -// public void call(Object... args) { -// values.offer(args); -// socket.disconnect(); -// } -// }).on(Socket.EVENT_DISCONNECT, new Emitter.Listener() { -// @Override -// public void call(Object... args) { -// values.offer(args); -// } -// }); -// socket.connect(); -// -// assertThat(((Object[])values.take()).length, is(0)); -// Object[] args = (Object[] )values.take(); -// assertThat(args.length, is(1)); -// assertThat(args[0], is(instanceOf(String.class))); -// } -// -// @Test(timeout = TIMEOUT) -// public void message() throws InterruptedException { -// final BlockingQueue values = new LinkedBlockingQueue<>(); -// -// socket = client(); -// socket.on(Socket.EVENT_CONNECT, new Emitter.Listener() { -// @Override -// public void call(Object... objects) { -// socket.send("foo", "bar"); -// } -// }).on(Socket.EVENT_MESSAGE, new Emitter.Listener() { -// @Override -// public void call(Object... args) { -// values.offer(args); -// } -// }); -// socket.connect(); -// -// assertThat((Object[])values.take(), is(new Object[] {"hello client"})); -// assertThat((Object[])values.take(), is(new Object[] {"foo", "bar"})); -// socket.disconnect(); -// } -// -// @Test(timeout = TIMEOUT) -// public void event() throws Exception { -// final BlockingQueue values = new LinkedBlockingQueue<>(); -// -// final JSONObject obj = new JSONObject(); -// obj.put("foo", 1); -// -// socket = client(); -// socket.on(Socket.EVENT_CONNECT, new Emitter.Listener() { -// @Override -// public void call(Object... objects) { -// socket.emit("echo", obj, null, "bar"); -// } -// }).on("echoBack", new Emitter.Listener() { -// @Override -// public void call(Object... args) { -// values.offer(args); -// } -// }); -// socket.connect(); -// -// Object[] args = (Object[])values.take(); -// assertThat(args.length, is(3)); -// assertThat(args[0].toString(), is(obj.toString())); -// assertThat(args[1], is(nullValue())); -// assertThat((String)args[2], is("bar")); -// socket.disconnect(); -// } -// -// @Test(timeout = TIMEOUT) -// public void ack() throws Exception { -// final BlockingQueue values = new LinkedBlockingQueue<>(); -// -// final JSONObject obj = new JSONObject(); -// obj.put("foo", 1); -// -// socket = client(); -// socket.on(Socket.EVENT_CONNECT, new Emitter.Listener() { -// @Override -// public void call(Object... objects) { -// socket.emit("ack", new Object[] {obj, "bar"}, new Ack() { -// @Override -// public void call(Object... args) { -// values.offer(args); -// } -// }); -// } -// }); -// socket.connect(); -// -// Object[] args = (Object[])values.take(); -// assertThat(args.length, is(2)); -// assertThat(args[0].toString(), is(obj.toString())); -// assertThat((String)args[1], is("bar")); -// socket.disconnect(); -// } -// +package io.socket.client; + +import com.piasy.kmp.socketio.emitter.Emitter; +import com.piasy.kmp.socketio.engineio.EngineSocket; +import com.piasy.kmp.socketio.engineio.TestUtil; +import com.piasy.kmp.socketio.engineio.Transport; +import com.piasy.kmp.socketio.engineio.transports.PollingXHR; +import com.piasy.kmp.socketio.engineio.transports.WebSocket; +import com.piasy.kmp.socketio.socketio.Ack; +import com.piasy.kmp.socketio.socketio.IO; +import com.piasy.kmp.socketio.socketio.Manager; +import com.piasy.kmp.socketio.socketio.Socket; +import kotlin.Unit; +import kotlinx.serialization.json.JsonElementBuildersKt; +import kotlinx.serialization.json.JsonElementKt; +import kotlinx.serialization.json.JsonObject; +import org.json.JSONObject; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +import java.util.Arrays; +import java.util.List; +import java.util.Map; +import java.util.concurrent.BlockingQueue; +import java.util.concurrent.LinkedBlockingQueue; + +import static org.hamcrest.CoreMatchers.*; +import static org.junit.Assert.assertThat; + +@RunWith(JUnit4.class) +public class ServerConnectionTest extends Connection { + + private Socket socket; + private Socket socket2; + + @Test(timeout = TIMEOUT) + public void openAndClose() throws InterruptedException { + final BlockingQueue values = new LinkedBlockingQueue<>(); + + client("/", socket -> { + this.socket = socket; + socket.on(Socket.EVENT_CONNECT, new Emitter.Listener() { + @Override + public void call(Object... args) { + values.offer(args); + socket.close(); + } + }).on(Socket.EVENT_DISCONNECT, new Emitter.Listener() { + @Override + public void call(Object... args) { + values.offer(args); + } + }); + socket.open(); + return Unit.INSTANCE; + }); + + assertThat(((Object[])values.take()).length, is(0)); + Object[] args = (Object[] )values.take(); + assertThat(args.length, is(1)); + assertThat(args[0], is(instanceOf(String.class))); + } + + @Test(timeout = TIMEOUT) + public void message() throws InterruptedException { + final BlockingQueue values = new LinkedBlockingQueue<>(); + + client("/", socket -> { + this.socket = socket; + socket.on(Socket.EVENT_CONNECT, new Emitter.Listener() { + @Override + public void call(Object... objects) { + socket.send("foo", "bar"); + } + }).on(Socket.EVENT_MESSAGE, new Emitter.Listener() { + @Override + public void call(Object... args) { + values.offer(args); + } + }); + socket.open(); + return Unit.INSTANCE; + }); + + assertThat((Object[])values.take(), is(new Object[] {"hello client"})); + assertThat((Object[])values.take(), is(new Object[] {"foo", "bar"})); + socket.close(); + } + + @Test(timeout = TIMEOUT) + public void event() throws Exception { + final BlockingQueue values = new LinkedBlockingQueue<>(); + + JsonObject obj = JsonElementBuildersKt.buildJsonObject(builder -> { + builder.put("foo", JsonElementKt.JsonPrimitive(1)); + return Unit.INSTANCE; + }); + + client("/", socket -> { + this.socket = socket; + socket.on(Socket.EVENT_CONNECT, new Emitter.Listener() { + @Override + public void call(Object... objects) { + socket.emit("echo", obj, "null", "bar"); + } + }).on("echoBack", new Emitter.Listener() { + @Override + public void call(Object... args) { + values.offer(args); + } + }); + socket.open(); + return Unit.INSTANCE; + }); + + Object[] args = (Object[])values.take(); + assertThat(args.length, is(3)); + assertThat(args[0].toString(), is(obj.toString())); + assertThat(args[1], is("null")); + assertThat((String)args[2], is("bar")); + socket.close(); + } + + @Test(timeout = TIMEOUT) + public void ack() throws Exception { + final BlockingQueue values = new LinkedBlockingQueue<>(); + + JsonObject obj = JsonElementBuildersKt.buildJsonObject(builder -> { + builder.put("foo", JsonElementKt.JsonPrimitive(1)); + return Unit.INSTANCE; + }); + + client("/", socket -> { + this.socket = socket; + socket.on(Socket.EVENT_CONNECT, new Emitter.Listener() { + @Override + public void call(Object... objects) { + socket.emit("ack", obj, "bar", new Ack() { + @Override + public void call(Object... args) { + values.offer(args); + } + }); + } + }); + socket.open(); + return Unit.INSTANCE; + }); + + Object[] args = (Object[])values.take(); + assertThat(args.length, is(2)); + assertThat(args[0].toString(), is(obj.toString())); + assertThat((String)args[1], is("bar")); + socket.close(); + } + + // in KMP definition, args could not be null, so we shouldn't have this case. // @Test(timeout = TIMEOUT) // public void ackWithoutArgs() throws InterruptedException { // final BlockingQueue values = new LinkedBlockingQueue<>(); // -// socket = client(); -// socket.on(Socket.EVENT_CONNECT, new Emitter.Listener() { -// @Override -// public void call(Object... objects) { -// socket.emit("ack", null, new Ack() { -// @Override -// public void call(Object... args) { -// values.offer(args.length); -// } -// }); -// } +// client("/", socket -> { +// this.socket = socket; +// socket.on(Socket.EVENT_CONNECT, new Emitter.Listener() { +// @Override +// public void call(Object... objects) { +// socket.emit("ack", null, new Ack() { +// @Override +// public void call(Object... args) { +// values.offer(args.length); +// } +// }); +// } +// }); +// socket.open(); +// return Unit.INSTANCE; // }); -// socket.connect(); // // assertThat((Integer)values.take(), is(0)); -// socket.disconnect(); -// } -// -// @Test(timeout = TIMEOUT) -// public void ackWithoutArgsFromClient() throws InterruptedException { -// final BlockingQueue values = new LinkedBlockingQueue<>(); -// -// socket = client(); -// socket.on(Socket.EVENT_CONNECT, new Emitter.Listener() { -// @Override -// public void call(Object... objects) { -// socket.on("ack", new Emitter.Listener() { -// @Override -// public void call(Object... args) { -// values.offer(args); -// Ack ack = (Ack)args[0]; -// ack.call(); -// } -// }).on("ackBack", new Emitter.Listener() { -// @Override -// public void call(Object... args) { -// values.offer(args); -// socket.disconnect(); -// } -// }); -// socket.emit("callAck"); -// } -// }); -// socket.connect(); -// -// Object[] args = (Object[])values.take(); -// assertThat(args.length, is(1)); -// assertThat(args[0], is(instanceOf(Ack.class))); -// args = (Object[])values.take(); -// assertThat(args.length, is(0)); -// socket.disconnect(); -// } -// -// @Test(timeout = TIMEOUT) -// public void closeEngineConnection() throws InterruptedException { -// final BlockingQueue values = new LinkedBlockingQueue<>(); -// -// socket = client(); -// socket.on(Socket.EVENT_CONNECT, new Emitter.Listener() { -// @Override -// public void call(Object... args) { -// socket.io().engine.on(io.socket.engineio.client.Socket.EVENT_CLOSE, new Emitter.Listener() { -// @Override -// public void call(Object... objects) { -// values.offer("done"); -// } -// }); -// socket.disconnect(); -// } -// }); -// socket.connect(); -// values.take(); -// } -// -// @Test(timeout = TIMEOUT) -// public void broadcast() throws InterruptedException { -// final BlockingQueue values = new LinkedBlockingQueue<>(); -// -// socket = client(); -// socket.on(Socket.EVENT_CONNECT, new Emitter.Listener() { -// @Override -// public void call(Object... objects) { -// socket2 = client(); -// -// socket2.on(Socket.EVENT_CONNECT, new Emitter.Listener() { -// @Override -// public void call(Object... objects) { -// socket2.emit("broadcast", "hi"); -// } -// }); -// socket2.connect(); -// } -// }).on("broadcastBack", new Emitter.Listener() { -// @Override -// public void call(Object... args) { -// values.offer(args); -// } -// }); -// socket.connect(); -// -// Object[] args = (Object[])values.take(); -// assertThat(args.length, is(1)); -// assertThat((String)args[0], is("hi")); -// socket.disconnect(); -// socket2.disconnect(); -// } -// -// @Test(timeout = TIMEOUT) -// public void room() throws InterruptedException { -// final BlockingQueue values = new LinkedBlockingQueue<>(); -// -// socket = client(); -// socket.on(Socket.EVENT_CONNECT, new Emitter.Listener() { -// @Override -// public void call(Object... objects) { -// socket.emit("room", "hi"); -// } -// }).on("roomBack", new Emitter.Listener() { -// @Override -// public void call(Object... args) { -// values.offer(args); -// } -// }); -// socket.connect(); -// -// Object[] args = (Object[])values.take(); -// assertThat(args.length, is(1)); -// assertThat((String)args[0], is("hi")); -// socket.disconnect(); -// } -// -// @Test(timeout = TIMEOUT) -// public void pollingHeaders() throws InterruptedException { -// final BlockingQueue values = new LinkedBlockingQueue<>(); -// -// IO.Options opts = createOptions(); -// opts.transports = new String[] {Polling.NAME}; -// socket = client(opts); -// socket.io().on(Manager.EVENT_TRANSPORT, new Emitter.Listener() { -// @Override -// public void call(Object... args) { -// Transport transport = (Transport)args[0]; -// transport.on(Transport.EVENT_REQUEST_HEADERS, new Emitter.Listener() { -// @Override -// public void call(Object... args) { -// @SuppressWarnings("unchecked") -// Map> headers = (Map>)args[0]; -// headers.put("X-SocketIO", Arrays.asList("hi")); -// } -// }).on(Transport.EVENT_RESPONSE_HEADERS, new Emitter.Listener() { -// @Override -// public void call(Object... args) { -// @SuppressWarnings("unchecked") -// Map> headers = (Map>)args[0]; -// List value = headers.get("X-SocketIO"); -// values.offer(value != null ? value.get(0) : ""); -// } -// }); -// } -// }); -// socket.open(); -// -// assertThat((String)values.take(), is("hi")); // socket.close(); // } -// -// @Test(timeout = TIMEOUT) -// public void websocketHandshakeHeaders() throws InterruptedException { -// final BlockingQueue values = new LinkedBlockingQueue<>(); -// -// IO.Options opts = createOptions(); -// opts.transports = new String[] {WebSocket.NAME}; -// socket = client(opts); -// socket.io().on(Manager.EVENT_TRANSPORT, new Emitter.Listener() { -// @Override -// public void call(Object... args) { -// Transport transport = (Transport)args[0]; -// transport.on(Transport.EVENT_REQUEST_HEADERS, new Emitter.Listener() { -// @Override -// public void call(Object... args) { -// @SuppressWarnings("unchecked") -// Map> headers = (Map>)args[0]; -// headers.put("X-SocketIO", Arrays.asList("hi")); -// } -// }).on(Transport.EVENT_RESPONSE_HEADERS, new Emitter.Listener() { -// @Override -// public void call(Object... args) { -// @SuppressWarnings("unchecked") -// Map> headers = (Map>)args[0]; -// List value = headers.get("X-SocketIO"); -// values.offer(value != null ? value.get(0) : ""); -// } -// }); -// } -// }); -// socket.open(); -// -// assertThat((String)values.take(), is("hi")); -// socket.close(); -// } -// -// @Test(timeout = TIMEOUT) -// public void disconnectFromServer() throws InterruptedException { -// final BlockingQueue values = new LinkedBlockingQueue<>(); -// -// socket = client(); -// socket.on(Socket.EVENT_CONNECT, new Emitter.Listener() { -// @Override -// public void call(Object... args) { -// socket.emit("requestDisconnect"); -// } -// }).on(Socket.EVENT_DISCONNECT, new Emitter.Listener() { -// @Override -// public void call(Object... args) { -// values.offer("disconnected"); -// } -// }); -// socket.connect(); -// assertThat((String)values.take(), is("disconnected")); -// } -//} + + @Test(timeout = TIMEOUT) + public void ackWithoutArgsFromClient() throws InterruptedException { + final BlockingQueue values = new LinkedBlockingQueue<>(); + + client("/", socket -> { + this.socket = socket; + socket.on(Socket.EVENT_CONNECT, new Emitter.Listener() { + @Override + public void call(Object... objects) { + socket.on("ack", new Emitter.Listener() { + @Override + public void call(Object... args) { + values.offer(args); + Ack ack = (Ack) args[0]; + ack.call(); + } + }).on("ackBack", new Emitter.Listener() { + @Override + public void call(Object... args) { + values.offer(args); + socket.close(); + } + }); + socket.emit("callAck"); + } + }); + socket.open(); + return Unit.INSTANCE; + }); + + Object[] args = (Object[])values.take(); + assertThat(args.length, is(1)); + assertThat(args[0], is(instanceOf(Ack.class))); + args = (Object[])values.take(); + assertThat(args.length, is(0)); + socket.close(); + } + + @Test(timeout = TIMEOUT) + public void closeEngineConnection() throws InterruptedException { + final BlockingQueue values = new LinkedBlockingQueue<>(); + + client("/", socket -> { + this.socket = socket; + socket.on(Socket.EVENT_CONNECT, new Emitter.Listener() { + @Override + public void call(Object... args) { + TestUtil.engineSocket(socket).on(EngineSocket.EVENT_CLOSE, new Emitter.Listener() { + @Override + public void call(Object... objects) { + values.offer("done"); + } + }); + socket.close(); + } + }); + socket.open(); + return Unit.INSTANCE; + }); + values.take(); + } + + @Test(timeout = TIMEOUT) + public void broadcast() throws InterruptedException { + final BlockingQueue values = new LinkedBlockingQueue<>(); + + ServerConnectionTest self = this; + client("/", socket -> { + this.socket = socket; + socket.on(Socket.EVENT_CONNECT, new Emitter.Listener() { + @Override + public void call(Object... objects) { + client("/", socket2 -> { + self.socket2 = socket; + + socket2.on(Socket.EVENT_CONNECT, new Emitter.Listener() { + @Override + public void call(Object... objects) { + socket2.emit("broadcast", "hi"); + } + }); + socket2.open(); + return Unit.INSTANCE; + }); + } + }).on("broadcastBack", new Emitter.Listener() { + @Override + public void call(Object... args) { + values.offer(args); + } + }); + socket.open(); + return Unit.INSTANCE; + }); + + Object[] args = (Object[])values.take(); + assertThat(args.length, is(1)); + assertThat((String)args[0], is("hi")); + socket.close(); + socket2.close(); + } + + @Test(timeout = TIMEOUT) + public void room() throws InterruptedException { + final BlockingQueue values = new LinkedBlockingQueue<>(); + + client("/", socket -> { + this.socket = socket; + socket.on(Socket.EVENT_CONNECT, new Emitter.Listener() { + @Override + public void call(Object... objects) { + socket.emit("room", "hi"); + } + }).on("roomBack", new Emitter.Listener() { + @Override + public void call(Object... args) { + values.offer(args); + } + }); + socket.open(); + return Unit.INSTANCE; + }); + + Object[] args = (Object[])values.take(); + assertThat(args.length, is(1)); + assertThat((String)args[0], is("hi")); + socket.close(); + } + + @Test(timeout = TIMEOUT) + public void pollingHeaders() throws InterruptedException { + final BlockingQueue values = new LinkedBlockingQueue<>(); + + IO.Options opts = createOptions(); + opts.transports = Arrays.asList(PollingXHR.NAME); + client("/", opts, socket -> { + this.socket = socket; + socket.getIo().on(Manager.EVENT_TRANSPORT, new Emitter.Listener() { + @Override + public void call(Object... args) { + Transport transport = (Transport) args[0]; + transport.on(Transport.EVENT_REQUEST_HEADERS, new Emitter.Listener() { + @Override + public void call(Object... args) { + @SuppressWarnings("unchecked") + Map> headers = (Map>) args[0]; + headers.put("X-SocketIO", Arrays.asList("hi")); + } + }).on(Transport.EVENT_RESPONSE_HEADERS, new Emitter.Listener() { + @Override + public void call(Object... args) { + @SuppressWarnings("unchecked") + Map> headers = (Map>) args[0]; + List value = headers.get("X-SocketIO"); + values.offer(value != null ? value.get(0) : ""); + } + }); + } + }); + socket.open(); + return Unit.INSTANCE; + }); + + assertThat((String)values.take(), is("hi")); + socket.close(); + } + + @Test(timeout = TIMEOUT) + public void websocketHandshakeHeaders() throws InterruptedException { + final BlockingQueue values = new LinkedBlockingQueue<>(); + + IO.Options opts = createOptions(); + opts.transports = Arrays.asList(WebSocket.NAME); + client("/", opts, socket -> { + this.socket = socket; + socket.getIo().on(Manager.EVENT_TRANSPORT, new Emitter.Listener() { + @Override + public void call(Object... args) { + Transport transport = (Transport) args[0]; + transport.on(Transport.EVENT_REQUEST_HEADERS, new Emitter.Listener() { + @Override + public void call(Object... args) { + @SuppressWarnings("unchecked") + Map> headers = (Map>) args[0]; + headers.put("X-SocketIO", Arrays.asList("hi")); + } + }).on(Transport.EVENT_RESPONSE_HEADERS, new Emitter.Listener() { + @Override + public void call(Object... args) { + @SuppressWarnings("unchecked") + Map> headers = (Map>) args[0]; + List value = headers.get("X-SocketIO"); + values.offer(value != null ? value.get(0) : ""); + } + }); + } + }); + socket.open(); + return Unit.INSTANCE; + }); + + assertThat((String)values.take(), is("hi")); + socket.close(); + } + + @Test(timeout = TIMEOUT) + public void disconnectFromServer() throws InterruptedException { + final BlockingQueue values = new LinkedBlockingQueue<>(); + + client("/", socket -> { + this.socket = socket; + socket.on(Socket.EVENT_CONNECT, new Emitter.Listener() { + @Override + public void call(Object... args) { + socket.emit("requestDisconnect"); + } + }).on(Socket.EVENT_DISCONNECT, new Emitter.Listener() { + @Override + public void call(Object... args) { + values.offer("disconnected"); + } + }); + socket.open(); + return Unit.INSTANCE; + }); + assertThat((String)values.take(), is("disconnected")); + } +} diff --git a/kmp-socketio/src/jvmTest/java/io/socket/client/SocketTest.java b/kmp-socketio/src/jvmTest/java/io/socket/client/SocketTest.java index 756c2dd..7bf418a 100644 --- a/kmp-socketio/src/jvmTest/java/io/socket/client/SocketTest.java +++ b/kmp-socketio/src/jvmTest/java/io/socket/client/SocketTest.java @@ -1,395 +1,445 @@ -//package io.socket.client; -// -//import io.socket.emitter.Emitter; -//import io.socket.util.Optional; -//import org.json.JSONException; -//import org.json.JSONObject; -//import org.junit.Assert; -//import org.junit.Test; -//import org.junit.runner.RunWith; -//import org.junit.runners.JUnit4; -// -//import java.util.Timer; -//import java.util.TimerTask; -//import java.util.concurrent.*; -// -//import static java.util.Collections.singletonMap; -//import static org.hamcrest.CoreMatchers.*; -//import static org.junit.Assert.assertThat; -//import static org.junit.Assert.fail; -// -//@RunWith(JUnit4.class) -//public class SocketTest extends Connection { -// -// private Socket socket; -// -// @Test(timeout = TIMEOUT) -// public void shouldHaveAnAccessibleSocketIdEqualToServerSideSocketId() throws InterruptedException { -// final BlockingQueue values = new LinkedBlockingQueue<>(); -// socket = client(); -// socket.on(Socket.EVENT_CONNECT, new Emitter.Listener() { -// @Override -// public void call(Object... objects) { -// values.offer(Optional.ofNullable(socket.id())); -// } -// }); -// socket.connect(); -// -// @SuppressWarnings("unchecked") -// Optional id = values.take(); -// assertThat(id.isPresent(), is(true)); -// assertThat(id.get(), not(socket.io().engine.id())); // distinct ID since Socket.IO v3 -// socket.disconnect(); -// } -// -// @Test(timeout = TIMEOUT) -// public void shouldHaveAnAccessibleSocketIdEqualToServerSideSocketIdOnCustomNamespace() throws InterruptedException { -// final BlockingQueue values = new LinkedBlockingQueue<>(); -// socket = client("/foo"); -// socket.on(Socket.EVENT_CONNECT, new Emitter.Listener() { -// @Override -// public void call(Object... objects) { -// values.offer(Optional.ofNullable(socket.id())); -// } -// }); -// socket.connect(); -// -// @SuppressWarnings("unchecked") -// Optional id = values.take(); -// assertThat(id.isPresent(), is(true)); -// assertThat(id.get(), is(not(socket.io().engine.id()))); // distinct ID since Socket.IO v3 -// socket.disconnect(); -// } -// -// @Test(timeout = TIMEOUT) -// public void clearsSocketIdUponDisconnection() throws InterruptedException { -// final BlockingQueue values = new LinkedBlockingQueue<>(); -// socket = client(); -// socket.on(Socket.EVENT_CONNECT, new Emitter.Listener() { -// @Override -// public void call(Object... objects) { -// socket.on(Socket.EVENT_DISCONNECT, new Emitter.Listener() { -// @Override -// public void call(Object... args) { -// values.offer(Optional.ofNullable(socket.id())); -// } -// }); -// -// socket.disconnect(); -// } -// }); -// socket.connect(); -// @SuppressWarnings("unchecked") -// Optional id = values.take(); -// assertThat(id.isPresent(), is(false)); -// } -// -// @Test(timeout = TIMEOUT) -// public void doesNotFireConnectErrorIfWeForceDisconnectInOpeningState() throws InterruptedException { -// final BlockingQueue values = new LinkedBlockingQueue<>(); -// IO.Options opts = new IO.Options(); -// opts.timeout = 100; -// socket = client(opts); -// socket.on(Socket.EVENT_CONNECT_ERROR, new Emitter.Listener() { -// @Override -// public void call(Object... args) { -// values.offer(Optional.of(new Error("Unexpected"))); -// } -// }); -// socket.connect(); -// socket.disconnect(); -// -// new Timer().schedule(new TimerTask() { -// @Override -// public void run() { -// values.offer(Optional.empty()); -// } -// }, 300); -// -// @SuppressWarnings("unchecked") -// Optional err = values.take(); -// if (err.isPresent()) throw err.get(); -// } -// -// @Test(timeout = TIMEOUT) -// public void shouldChangeSocketIdUponReconnection() throws InterruptedException { -// final BlockingQueue values = new LinkedBlockingQueue<>(); -// socket = client(); -// socket.once(Socket.EVENT_CONNECT, new Emitter.Listener() { -// @Override -// public void call(Object... objects) { -// values.offer(Optional.ofNullable(socket.id())); -// -// socket.io().on(Manager.EVENT_RECONNECT_ATTEMPT, new Emitter.Listener() { -// @Override -// public void call(Object... objects) { -// values.offer(Optional.ofNullable(socket.id())); -// } -// }); -// -// socket.once(Socket.EVENT_CONNECT, new Emitter.Listener() { -// @Override -// public void call(Object... objects) { -// values.offer(Optional.ofNullable(socket.id())); -// } -// }); -// -// socket.io().engine.close(); -// } -// }); -// socket.connect(); -// @SuppressWarnings("unchecked") -// Optional id1 = values.take(); -// -// @SuppressWarnings("unchecked") -// Optional id2 = values.take(); -// assertThat(id2.isPresent(), is(false)); -// -// @SuppressWarnings("unchecked") -// Optional id3 = values.take(); -// assertThat(id3.get(), is(not(id1.get()))); -// -// socket.disconnect(); -// } -// -// @Test(timeout = TIMEOUT) -// public void shouldAcceptAQueryStringOnDefaultNamespace() throws InterruptedException, JSONException { -// final BlockingQueue values = new LinkedBlockingQueue<>(); -// -// socket = client("/?c=d"); -// socket.emit("getHandshake", new Ack() { -// @Override -// public void call(Object... args) { -// JSONObject handshake = (JSONObject)args[0]; -// values.offer(Optional.ofNullable(handshake)); -// } -// }); -// socket.connect(); -// -// @SuppressWarnings("unchecked") -// Optional handshake = values.take(); -// assertThat(handshake.get().getJSONObject("query").getString("c"), is("d")); -// -// socket.disconnect(); -// } -// -// @Test(timeout = TIMEOUT) -// public void shouldAcceptAQueryString() throws InterruptedException, JSONException { -// final BlockingQueue values = new LinkedBlockingQueue<>(); -// -// socket = client("/abc?b=c&d=e"); -// socket.on("handshake", new Emitter.Listener() { -// @Override -// public void call(Object... args) { -// JSONObject handshake = (JSONObject)args[0]; -// values.offer(Optional.ofNullable(handshake)); -// } -// }); -// socket.connect(); -// -// @SuppressWarnings("unchecked") -// Optional handshake = values.take(); -// JSONObject query = handshake.get().getJSONObject("query"); -// assertThat(query.getString("b"), is("c")); -// assertThat(query.getString("d"), is("e")); -// -// socket.disconnect(); -// } -// -// @Test(timeout = TIMEOUT) -// public void shouldAcceptAnAuthOption() throws InterruptedException, JSONException { -// final BlockingQueue values = new LinkedBlockingQueue<>(); -// -// IO.Options opts = new IO.Options(); -// opts.auth = singletonMap("token", "abcd"); -// socket = client("/abc", opts); -// socket.on("handshake", new Emitter.Listener() { -// @Override -// public void call(Object... args) { -// JSONObject handshake = (JSONObject)args[0]; -// values.offer(Optional.ofNullable(handshake)); -// } -// }); -// socket.connect(); -// -// @SuppressWarnings("unchecked") -// Optional handshake = values.take(); -// JSONObject query = handshake.get().getJSONObject("auth"); -// assertThat(query.getString("token"), is("abcd")); -// -// socket.disconnect(); -// } -// -// @Test(timeout = TIMEOUT) -// public void shouldFireAnErrorEventOnMiddlewareFailure() throws InterruptedException, JSONException { -// final BlockingQueue values = new LinkedBlockingQueue<>(); -// -// socket = client("/no"); -// socket.on(Socket.EVENT_CONNECT_ERROR, new Emitter.Listener() { -// @Override -// public void call(Object... args) { -// values.offer(Optional.ofNullable(args[0])); -// } -// }); -// socket.connect(); -// -// @SuppressWarnings("unchecked") -// JSONObject error = ((Optional) values.take()).get(); -// assertThat(error.getString("message"), is("auth failed")); -// assertThat(error.getJSONObject("data").getString("a"), is("b")); -// assertThat(error.getJSONObject("data").getInt("c"), is(3)); -// -// socket.disconnect(); -// } -// -// @Test(timeout = TIMEOUT) -// public void shouldThrowOnReservedEvent() { -// final BlockingQueue values = new LinkedBlockingQueue<>(); -// -// socket = client("/no"); -// try { -// socket.emit("disconnecting", "goodbye"); -// fail(); -// } catch (RuntimeException e) { -// assertThat(e.getMessage(), is("'disconnecting' is a reserved event name")); -// } -// -// socket.disconnect(); -// } -// -// @Test(timeout = TIMEOUT) -// public void shouldEmitEventsInOrder() throws InterruptedException { -// final BlockingQueue values = new LinkedBlockingQueue<>(); -// -// socket = client(); -// -// socket.on(Socket.EVENT_CONNECT, new Emitter.Listener() { -// @Override -// public void call(Object... objects) { -// socket.emit("ack", "second", new Ack() { -// @Override -// public void call(Object... args) { -// values.offer((String) args[0]); -// } -// }); -// } -// }); -// -// socket.emit("ack", "first", new Ack() { -// @Override -// public void call(Object... args) { -// values.offer((String) args[0]); -// } -// }); -// -// socket.connect(); -// assertThat(values.take(), is("first")); -// assertThat(values.take(), is("second")); -// } -// -// @Test(timeout = TIMEOUT) -// public void shouldTimeoutAfterTheGivenDelayWhenSocketIsNotConnected() throws InterruptedException { -// final BlockingQueue values = new LinkedBlockingQueue<>(); -// -// socket = client(); -// -// socket.emit("event", new AckWithTimeout(50) { -// @Override -// public void onSuccess(Object... args) { -// fail(); -// } -// -// @Override -// public void onTimeout() { -// values.offer(true); -// } -// }); -// -// assertThat(values.take(), is(true)); -// } -// -// @Test(timeout = TIMEOUT) -// public void shouldTimeoutWhenTheServerDoesNotAcknowledgeTheEvent() throws InterruptedException { -// final BlockingQueue values = new LinkedBlockingQueue<>(); -// -// socket = client(); -// -// socket.on(Socket.EVENT_CONNECT, new Emitter.Listener() { -// @Override -// public void call(Object... args) { -// socket.emit("unknown", new AckWithTimeout(50) { -// @Override -// public void onTimeout() { -// values.offer(true); -// } -// -// @Override -// public void onSuccess(Object... args) { -// fail(); -// } -// }); -// } -// }); -// -// socket.connect(); -// -// assertThat(values.take(), is(true)); -// } -// -// @Test(timeout = TIMEOUT) -// public void shouldTimeoutWhenTheServerDoesNotAcknowledgeTheEventInTime() throws InterruptedException { -// final BlockingQueue values = new LinkedBlockingQueue<>(); -// -// socket = client(); -// -// socket.on(Socket.EVENT_CONNECT, new Emitter.Listener() { -// @Override -// public void call(Object... args) { -// socket.emit("ack", new AckWithTimeout(0) { -// @Override -// public void onTimeout() { -// values.offer(true); -// } -// -// @Override -// public void onSuccess(Object... args) { -// fail(); -// } -// }); -// } -// }); -// -// socket.connect(); -// -// assertThat(values.take(), is(true)); -// } -// +package io.socket.client; + +import io.socket.util.Optional; +import kotlin.Unit; +import kotlinx.serialization.json.JsonObject; + +import org.jetbrains.annotations.NotNull; +import org.json.JSONException; +import org.json.JSONObject; +import org.junit.Assert; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +import java.util.Timer; +import java.util.TimerTask; +import java.util.concurrent.*; + +import static java.util.Collections.singletonMap; +import static org.hamcrest.CoreMatchers.*; +import static org.junit.Assert.assertThat; +import static org.junit.Assert.fail; + +import com.piasy.kmp.socketio.emitter.Emitter; +import com.piasy.kmp.socketio.engineio.TestUtil; +import com.piasy.kmp.socketio.socketio.Ack; +import com.piasy.kmp.socketio.socketio.AckWithTimeout; +import com.piasy.kmp.socketio.socketio.IO; +import com.piasy.kmp.socketio.socketio.Manager; +import com.piasy.kmp.socketio.socketio.Socket; + +@RunWith(JUnit4.class) +public class SocketTest extends Connection { + + private Socket socket; + + @Test(timeout = TIMEOUT) + public void shouldHaveAnAccessibleSocketIdEqualToServerSideSocketId() throws InterruptedException { + final BlockingQueue values = new LinkedBlockingQueue<>(); + client("/", socket -> { + this.socket = socket; + socket.on(Socket.EVENT_CONNECT, new Emitter.Listener() { + @Override + public void call(Object... objects) { + values.offer(Optional.ofNullable(socket.getId())); + } + }); + socket.open(); + return Unit.INSTANCE; + }); + + @SuppressWarnings("unchecked") + Optional id = values.take(); + assertThat(id.isPresent(), is(true)); + assertThat(id.get(), not(TestUtil.engineId(socket))); // distinct ID since Socket.IO v3 + socket.close(); + } + + @Test(timeout = TIMEOUT) + public void shouldHaveAnAccessibleSocketIdEqualToServerSideSocketIdOnCustomNamespace() throws InterruptedException { + final BlockingQueue values = new LinkedBlockingQueue<>(); + client("/foo", socket -> { + this.socket = socket; + socket.on(Socket.EVENT_CONNECT, new Emitter.Listener() { + @Override + public void call(Object... objects) { + values.offer(Optional.ofNullable(socket.getId())); + } + }); + socket.open(); + return Unit.INSTANCE; + }); + + @SuppressWarnings("unchecked") + Optional id = values.take(); + assertThat(id.isPresent(), is(true)); + assertThat(id.get(), is(not(TestUtil.engineId(socket)))); // distinct ID since Socket.IO v3 + socket.close(); + } + + @Test(timeout = TIMEOUT) + public void clearsSocketIdUponDisconnection() throws InterruptedException { + final BlockingQueue values = new LinkedBlockingQueue<>(); + client("/", socket -> { + this.socket = socket; + socket.on(Socket.EVENT_CONNECT, new Emitter.Listener() { + @Override + public void call(Object... objects) { + socket.on(Socket.EVENT_DISCONNECT, new Emitter.Listener() { + @Override + public void call(Object... args) { + values.offer(Optional.ofNullable(socket.getId())); + } + }); + + socket.close(); + } + }); + socket.open(); + return Unit.INSTANCE; + }); + @SuppressWarnings("unchecked") + Optional id = values.take(); + assertThat(id.get(), is("")); + } + + @Test(timeout = TIMEOUT) + public void doesNotFireConnectErrorIfWeForceDisconnectInOpeningState() throws InterruptedException { + final BlockingQueue values = new LinkedBlockingQueue<>(); + IO.Options opts = new IO.Options(); + opts.timeout = 100; + client("/", opts, socket -> { + this.socket = socket; + socket.on(Socket.EVENT_CONNECT_ERROR, new Emitter.Listener() { + @Override + public void call(Object... args) { + values.offer(Optional.of(new Error("Unexpected"))); + } + }); + socket.open(); + socket.close(); + return Unit.INSTANCE; + }); + + new Timer().schedule(new TimerTask() { + @Override + public void run() { + values.offer(Optional.empty()); + } + }, 300); + + @SuppressWarnings("unchecked") + Optional err = values.take(); + if (err.isPresent()) throw err.get(); + } + + @Test(timeout = TIMEOUT) + public void shouldChangeSocketIdUponReconnection() throws InterruptedException { + final BlockingQueue values = new LinkedBlockingQueue<>(); + client("/", socket -> { + this.socket = socket; + socket.once(Socket.EVENT_CONNECT, new Emitter.Listener() { + @Override + public void call(Object... objects) { + values.offer(Optional.ofNullable(socket.getId())); + + socket.getIo().on(Manager.EVENT_RECONNECT_ATTEMPT, new Emitter.Listener() { + @Override + public void call(Object... objects) { + values.offer(Optional.ofNullable(socket.getId())); + } + }); + + socket.once(Socket.EVENT_CONNECT, new Emitter.Listener() { + @Override + public void call(Object... objects) { + values.offer(Optional.ofNullable(socket.getId())); + } + }); + + TestUtil.closeEngineSocket(socket); + } + }); + socket.open(); + return Unit.INSTANCE; + }); + @SuppressWarnings("unchecked") + Optional id1 = values.take(); + + @SuppressWarnings("unchecked") + Optional id2 = values.take(); + assertThat(id2.get(), is("")); + + @SuppressWarnings("unchecked") + Optional id3 = values.take(); + assertThat(id3.get(), is(not(id1.get()))); + + socket.close(); + } + + @Test(timeout = TIMEOUT) + public void shouldAcceptAQueryStringOnDefaultNamespace() throws InterruptedException, JSONException { + final BlockingQueue values = new LinkedBlockingQueue<>(); + client("/?c=d", socket -> { + this.socket = socket; + socket.emit("getHandshake", new Ack() { + @Override + public void call(Object... args) { + values.offer(Optional.ofNullable(TestUtil.toJSON(args[0]))); + } + }); + socket.open(); + return Unit.INSTANCE; + }); + + @SuppressWarnings("unchecked") + Optional handshake = values.take(); + assertThat(handshake.get().getJSONObject("query").getString("c"), is("d")); + + socket.close(); + } + + @Test(timeout = TIMEOUT) + public void shouldAcceptAQueryString() throws InterruptedException, JSONException { + final BlockingQueue values = new LinkedBlockingQueue<>(); + client("/abc?b=c&d=e", socket -> { + this.socket = socket; + socket.on("handshake", new Emitter.Listener() { + @Override + public void call(Object... args) { + values.offer(Optional.ofNullable(TestUtil.toJSON(args[0]))); + } + }); + socket.open(); + return Unit.INSTANCE; + }); + + @SuppressWarnings("unchecked") + Optional handshake = values.take(); + JSONObject query = handshake.get().getJSONObject("query"); + assertThat(query.getString("b"), is("c")); + assertThat(query.getString("d"), is("e")); + + socket.close(); + } + + @Test(timeout = TIMEOUT) + public void shouldAcceptAnAuthOption() throws InterruptedException, JSONException { + final BlockingQueue values = new LinkedBlockingQueue<>(); + + IO.Options opts = new IO.Options(); + opts.auth = singletonMap("token", "abcd"); + client("/abc", opts, socket -> { + this.socket = socket; + socket.on("handshake", new Emitter.Listener() { + @Override + public void call(Object... args) { + values.offer(Optional.ofNullable(TestUtil.toJSON(args[0]))); + } + }); + socket.open(); + return Unit.INSTANCE; + }); + + @SuppressWarnings("unchecked") + Optional handshake = values.take(); + JSONObject query = handshake.get().getJSONObject("auth"); + assertThat(query.getString("token"), is("abcd")); + + socket.close(); + } + + @Test(timeout = TIMEOUT) + public void shouldFireAnErrorEventOnMiddlewareFailure() throws InterruptedException, JSONException { + final BlockingQueue values = new LinkedBlockingQueue<>(); + + client("/no", socket -> { + this.socket = socket; + socket.on(Socket.EVENT_CONNECT_ERROR, new Emitter.Listener() { + @Override + public void call(Object... args) { + values.offer(Optional.ofNullable(TestUtil.toJSON(args[0]))); + } + }); + socket.open(); + return Unit.INSTANCE; + }); + + @SuppressWarnings("unchecked") + JSONObject error = ((Optional) values.take()).get(); + assertThat(error.getString("message"), is("auth failed")); + assertThat(error.getJSONObject("data").getString("a"), is("b")); + assertThat(error.getJSONObject("data").getInt("c"), is(3)); + + socket.close(); + } + + @Test(timeout = TIMEOUT) + public void shouldThrowOnReservedEvent() throws InterruptedException { + final BlockingQueue values = new LinkedBlockingQueue<>(); + + client("/no", socket -> { + this.socket = socket; + try { + socket.emit("disconnecting", "goodbye"); + fail(); + } catch (RuntimeException e) { + values.offer(Optional.ofNullable(e.getMessage())); + } + return Unit.INSTANCE; + }); + assertThat((String) values.take().get(), is("emit reserved event: disconnecting")); + } + + @Test(timeout = TIMEOUT) + public void shouldEmitEventsInOrder() throws InterruptedException { + final BlockingQueue values = new LinkedBlockingQueue<>(); + + client("/", socket -> { + this.socket = socket; + + socket.on(Socket.EVENT_CONNECT, new Emitter.Listener() { + @Override + public void call(Object... objects) { + socket.emit("ack", "second", new Ack() { + @Override + public void call(Object... args) { + values.offer((String) args[0]); + } + }); + } + }); + + socket.emit("ack", "first", new Ack() { + @Override + public void call(Object... args) { + values.offer((String) args[0]); + } + }); + + socket.open(); + return Unit.INSTANCE; + }); + assertThat(values.take(), is("first")); + assertThat(values.take(), is("second")); + } + + @Test(timeout = TIMEOUT) + public void shouldTimeoutAfterTheGivenDelayWhenSocketIsNotConnected() throws InterruptedException { + final BlockingQueue values = new LinkedBlockingQueue<>(); + + client("/", socket -> { + this.socket = socket; + + socket.emit("event", new AckWithTimeout(50) { + @Override + public void onSuccess(@NotNull Object @NotNull ... args) { + fail(); + } + + @Override + public void onTimeout() { + values.offer(true); + } + }); + return Unit.INSTANCE; + }); + + assertThat(values.take(), is(true)); + } + + @Test(timeout = TIMEOUT) + public void shouldTimeoutWhenTheServerDoesNotAcknowledgeTheEvent() throws InterruptedException { + final BlockingQueue values = new LinkedBlockingQueue<>(); + + client("/", socket -> { + this.socket = socket; + + socket.on(Socket.EVENT_CONNECT, new Emitter.Listener() { + @Override + public void call(Object... args) { + socket.emit("unknown", new AckWithTimeout(50) { + @Override + public void onTimeout() { + values.offer(true); + } + + @Override + public void onSuccess(Object... args) { + fail(); + } + }); + } + }); + + socket.open(); + return Unit.INSTANCE; + }); + + assertThat(values.take(), is(true)); + } + + @Test(timeout = TIMEOUT) + public void shouldTimeoutWhenTheServerDoesNotAcknowledgeTheEventInTime() throws InterruptedException { + final BlockingQueue values = new LinkedBlockingQueue<>(); + + client("/", socket -> { + this.socket = socket; + + socket.on(Socket.EVENT_CONNECT, new Emitter.Listener() { + @Override + public void call(Object... args) { + socket.emit("ack", new AckWithTimeout(0) { + @Override + public void onTimeout() { + values.offer(true); + } + + @Override + public void onSuccess(Object... args) { + fail(); + } + }); + } + }); + + socket.open(); + return Unit.INSTANCE; + }); + + assertThat(values.take(), is(true)); + } + // @Test(timeout = TIMEOUT) // public void shouldNotTimeoutWhenTheServerDoesAcknowledgeTheEvent() throws InterruptedException { // final BlockingQueue values = new LinkedBlockingQueue<>(); // -// socket = client(); +// client("/", socket -> { +// this.socket = socket; // -// socket.on(Socket.EVENT_CONNECT, new Emitter.Listener() { -// @Override -// public void call(Object... args) { -// socket.emit("ack", 1, "2", new byte[] { 3 }, new AckWithTimeout(200) { -// @Override -// public void onTimeout() { -// fail(); -// } +// socket.on(Socket.EVENT_CONNECT, new Emitter.Listener() { +// @Override +// public void call(Object... args) { +// socket.emit("ack", 1, "2", new byte[]{3}, new AckWithTimeout(200) { +// @Override +// public void onTimeout() { +// fail(); +// } // -// @Override -// public void onSuccess(Object... args) { -// for (Object arg : args) { -// values.offer(arg); +// @Override +// public void onSuccess(Object... args) { +// for (Object arg : args) { +// values.offer(arg); +// } // } -// } -// }); -// } -// }); +// }); +// } +// }); // -// socket.connect(); +// socket.open(); +// return Unit.INSTANCE; +// }); // // assertThat((Integer) values.take(), is(1)); // assertThat((String) values.take(), is("2")); @@ -400,25 +450,28 @@ // public void shouldCallCatchAllListenerForIncomingPackets() throws InterruptedException { // final BlockingQueue values = new LinkedBlockingQueue<>(); // -// socket = client(); +// client("/", socket -> { +// this.socket = socket; // -// socket.on("message", new Emitter.Listener() { -// @Override -// public void call(Object... args) { -// socket.emit("echo", 1, "2", new byte[] { 3 }); +// socket.on("message", new Emitter.Listener() { +// @Override +// public void call(Object... args) { +// socket.emit("echo", 1, "2", new byte[]{3}); // -// socket.onAnyIncoming(new Emitter.Listener() { -// @Override -// public void call(Object... args) { -// for (Object arg : args) { -// values.offer(arg); +// socket.onAnyIncoming(new Emitter.Listener() { +// @Override +// public void call(Object... args) { +// for (Object arg : args) { +// values.offer(arg); +// } // } -// } -// }); -// } -// }); +// }); +// } +// }); // -// socket.connect(); +// socket.open(); +// return Unit.INSTANCE; +// }); // // assertThat((String) values.take(), is("echoBack")); // assertThat((Integer) values.take(), is(1)); @@ -430,24 +483,27 @@ // public void shouldCallCatchAllListenerForOutgoingPackets() throws InterruptedException { // final BlockingQueue values = new LinkedBlockingQueue<>(); // -// socket = client(); +// client("/", socket -> { +// this.socket = socket; // -// socket.emit("echo", 1, "2", new byte[] { 3 }); +// socket.emit("echo", 1, "2", new byte[]{3}); // -// socket.onAnyOutgoing(new Emitter.Listener() { -// @Override -// public void call(Object... args) { -// for (Object arg : args) { -// values.offer(arg); +// socket.onAnyOutgoing(new Emitter.Listener() { +// @Override +// public void call(Object... args) { +// for (Object arg : args) { +// values.offer(arg); +// } // } -// } -// }); +// }); // -// socket.connect(); +// socket.open(); +// return Unit.INSTANCE; +// }); // // assertThat((String) values.take(), is("echo")); // assertThat((Integer) values.take(), is(1)); // assertThat((String) values.take(), is("2")); // assertThat((byte[]) values.take(), is(new byte[] { 3 })); // } -//} +} diff --git a/kmp-socketio/src/jvmTest/java/io/socket/engineio/client/Connection.java b/kmp-socketio/src/jvmTest/java/io/socket/engineio/client/Connection.java index 5e148cf..957ce89 100644 --- a/kmp-socketio/src/jvmTest/java/io/socket/engineio/client/Connection.java +++ b/kmp-socketio/src/jvmTest/java/io/socket/engineio/client/Connection.java @@ -27,7 +27,6 @@ public abstract class Connection { @Before public void startServer() throws IOException, InterruptedException { - TestUtil.setupLogger(); TestUtil.stringMessagePayloadForTesting(true); logger.fine("Starting server ..."); @@ -72,7 +71,6 @@ public void run() { @After public void stopServer() throws InterruptedException { - TestUtil.stringMessagePayloadForTesting(false); logger.fine("Stopping server ..."); serverProcess.destroy(); serverOutout.cancel(false); diff --git a/kmp-socketio/src/jvmTest/java/io/socket/engineio/client/ConnectionTest.java b/kmp-socketio/src/jvmTest/java/io/socket/engineio/client/ConnectionTest.java index 0abbfc6..f106817 100644 --- a/kmp-socketio/src/jvmTest/java/io/socket/engineio/client/ConnectionTest.java +++ b/kmp-socketio/src/jvmTest/java/io/socket/engineio/client/ConnectionTest.java @@ -29,7 +29,7 @@ public void connectToLocalhost() throws InterruptedException { socket.on(EngineSocket.EVENT_OPEN, new Emitter.Listener() { @Override public void call(Object... args) { - socket.on(EngineSocket.EVENT_MESSAGE, new Emitter.Listener() { + socket.on(EngineSocket.EVENT_DATA, new Emitter.Listener() { @Override public void call(Object... args) { values.offer(args[0]); @@ -52,7 +52,7 @@ public void receiveMultibyteUTF8StringsWithPolling() throws InterruptedException @Override public void call(Object... args) { socket.send(new EngineIOPacket.Message("cash money €€€")); - socket.on(EngineSocket.EVENT_MESSAGE, new Emitter.Listener() { + socket.on(EngineSocket.EVENT_DATA, new Emitter.Listener() { @Override public void call(Object... args) { if ("hi".equals(args[0])) return; @@ -76,7 +76,7 @@ public void receiveEmoji() throws InterruptedException { @Override public void call(Object... args) { socket.send(new EngineIOPacket.Message("\uD800\uDC00-\uDB7F\uDFFF\uDB80\uDC00-\uDBFF\uDFFF\uE000-\uF8FF")); - socket.on(EngineSocket.EVENT_MESSAGE, new Emitter.Listener() { + socket.on(EngineSocket.EVENT_DATA, new Emitter.Listener() { @Override public void call(Object... args) { if ("hi".equals(args[0])) return; diff --git a/kmp-socketio/src/jvmTest/java/io/socket/engineio/client/ServerConnectionTest.java b/kmp-socketio/src/jvmTest/java/io/socket/engineio/client/ServerConnectionTest.java index 5a273db..19fd368 100644 --- a/kmp-socketio/src/jvmTest/java/io/socket/engineio/client/ServerConnectionTest.java +++ b/kmp-socketio/src/jvmTest/java/io/socket/engineio/client/ServerConnectionTest.java @@ -62,7 +62,7 @@ public void messages() throws URISyntaxException, InterruptedException { public void call(Object... args) { socket.send(new EngineIOPacket.Message("hello")); } - }).on(EngineSocket.EVENT_MESSAGE, new Emitter.Listener() { + }).on(EngineSocket.EVENT_DATA, new Emitter.Listener() { @Override public void call(Object... args) { events.offer((String) args[0]); diff --git a/kmp-socketio/src/jvmTest/kotlin/com/piasy/kmp/socketio/engineio/EngineSocketTest.kt b/kmp-socketio/src/jvmTest/kotlin/com/piasy/kmp/socketio/engineio/EngineSocketTest.kt index a4b5572..a808894 100644 --- a/kmp-socketio/src/jvmTest/kotlin/com/piasy/kmp/socketio/engineio/EngineSocketTest.kt +++ b/kmp-socketio/src/jvmTest/kotlin/com/piasy/kmp/socketio/engineio/EngineSocketTest.kt @@ -56,7 +56,6 @@ class EngineSocketTest : BaseTest() { val data = HashMap>() on(socket, EngineSocket.EVENT_OPEN, events, data) on(socket, EngineSocket.EVENT_CLOSE, events, data) - on(socket, EngineSocket.EVENT_MESSAGE, events, data) on(socket, EngineSocket.EVENT_ERROR, events, data) on(socket, EngineSocket.EVENT_UPGRADE_ERROR, events, data) on(socket, EngineSocket.EVENT_FLUSH, events, data) @@ -374,12 +373,11 @@ class EngineSocketTest : BaseTest() { EngineSocket.EVENT_PACKET, EngineSocket.EVENT_HEARTBEAT, EngineSocket.EVENT_DATA, - EngineSocket.EVENT_MESSAGE, ), sock.events, ) - assertEquals(listOf(pkt.payload), sock.data[EngineSocket.EVENT_MESSAGE]) + assertEquals(listOf(pkt.payload), sock.data[EngineSocket.EVENT_DATA]) } class TestTransport( @@ -412,19 +410,19 @@ class EngineSocketTest : BaseTest() { pingInterval: Int = 25000, pingTimeout: Int = 20000 ) { - onWsData(mockOpen(upgrades, pingInterval, pingTimeout)) + onWsText(mockOpen(upgrades, pingInterval, pingTimeout)) } fun mockOnPing() { - onWsData("2") + onWsText("2") } fun mockOnPong(data: String? = null) { - onWsData("3${data ?: ""}") + onWsText("3${data ?: ""}") } fun mockOnMessage(msg: String) { - onWsData(msg) + onWsText(msg) } } diff --git a/kmp-socketio/src/jvmTest/kotlin/com/piasy/kmp/socketio/engineio/TestUtil.kt b/kmp-socketio/src/jvmTest/kotlin/com/piasy/kmp/socketio/engineio/TestUtil.kt index 09dc11c..ab06f7e 100644 --- a/kmp-socketio/src/jvmTest/kotlin/com/piasy/kmp/socketio/engineio/TestUtil.kt +++ b/kmp-socketio/src/jvmTest/kotlin/com/piasy/kmp/socketio/engineio/TestUtil.kt @@ -6,7 +6,9 @@ import com.piasy.kmp.socketio.socketio.Manager import com.piasy.kmp.socketio.socketio.Socket import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers +import kotlinx.serialization.encodeToString import kotlinx.serialization.json.* +import org.json.JSONObject object TestUtil { @JvmStatic @@ -26,9 +28,6 @@ object TestUtil { Transport.stringMessagePayloadForTesting = enable } - @JvmStatic - fun setupLogger() = setupTestLogger() - @JvmStatic fun triggerTransportError(socket: EngineSocket, error: String) { socket.transport?.onError(error) @@ -57,8 +56,19 @@ object TestUtil { socket.io.engine?.close() } + @JvmStatic + fun engineId(socket: Socket) = socket.io.engine?.id + + @JvmStatic + fun engineSocket(socket: Socket) = socket.io.engine + @JvmStatic fun jsonBool(json: JsonObject, key: String): Boolean? { return json[key]?.jsonPrimitive?.boolean } + + @JvmStatic + fun toJSON(obj: Any): JSONObject { + return JSONObject(Json.encodeToString(obj as JsonObject)) + } } diff --git a/kmp-socketio/src/jvmTest/kotlin/com/piasy/kmp/socketio/engineio/transports/PollingXHRTest.kt b/kmp-socketio/src/jvmTest/kotlin/com/piasy/kmp/socketio/engineio/transports/PollingXHRTest.kt index 80e120c..8f53cc3 100644 --- a/kmp-socketio/src/jvmTest/kotlin/com/piasy/kmp/socketio/engineio/transports/PollingXHRTest.kt +++ b/kmp-socketio/src/jvmTest/kotlin/com/piasy/kmp/socketio/engineio/transports/PollingXHRTest.kt @@ -4,6 +4,7 @@ import com.piasy.kmp.socketio.engineio.BaseTest import com.piasy.kmp.socketio.engineio.Transport import com.piasy.kmp.socketio.engineio.mockOpen import com.piasy.kmp.socketio.engineio.on +import com.piasy.kmp.socketio.logging.Logger import io.ktor.client.request.* import io.ktor.client.statement.* import io.ktor.http.* @@ -253,6 +254,7 @@ class PollingXHRTest : BaseTest() { waitExec(this, 1500) coVerify(exactly = 3) { polling.factory.httpRequest(any(), any()) } + Logger.info("XXPXX", "closeOpening verify events") assertEquals( listOf( // poll & open diff --git a/kmp-socketio/src/jvmTest/kotlin/com/piasy/kmp/socketio/engineio/utils.kt b/kmp-socketio/src/jvmTest/kotlin/com/piasy/kmp/socketio/engineio/utils.kt index 51ca713..6682412 100644 --- a/kmp-socketio/src/jvmTest/kotlin/com/piasy/kmp/socketio/engineio/utils.kt +++ b/kmp-socketio/src/jvmTest/kotlin/com/piasy/kmp/socketio/engineio/utils.kt @@ -1,16 +1,12 @@ package com.piasy.kmp.socketio.engineio import com.piasy.kmp.socketio.emitter.Emitter -import com.piasy.kmp.socketio.logging.Logger -import com.piasy.kmp.socketio.logging.LoggerInterface -import io.ktor.util.date.* import io.mockk.verify import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.delay import kotlinx.coroutines.test.TestScope import kotlinx.coroutines.test.advanceUntilIdle import kotlinx.coroutines.withContext -import kotlinx.serialization.encodeToString import kotlinx.serialization.json.Json import kotlin.test.BeforeTest @@ -49,26 +45,9 @@ fun mockOpen( return "0$jsonHandshake" } -fun setupTestLogger() { - Logger.logger = object : LoggerInterface { - override fun debug(tag: String, log: String) { - println("${GMTDate().timestamp} D $tag $log") - } - - override fun info(tag: String, log: String) { - println("${GMTDate().timestamp} I $tag $log") - } - - override fun error(tag: String, log: String) { - println("${GMTDate().timestamp} E $tag $log") - } - } -} - open class BaseTest { @BeforeTest fun setUp() { - setupTestLogger() } protected suspend fun waitExec(scope: TestScope, millis: Long = 300) {