|
| 1 | +package com.piasy.kmp.socketio.socketio |
| 2 | + |
| 3 | +import com.piasy.kmp.socketio.engineio.transports.WebSocket |
| 4 | +import com.piasy.kmp.xlog.Logging |
| 5 | +import java.io.BufferedReader |
| 6 | +import java.io.InputStreamReader |
| 7 | +import java.util.concurrent.CountDownLatch |
| 8 | +import java.util.concurrent.TimeUnit |
| 9 | +import kotlin.test.assertFalse |
| 10 | +import kotlin.test.assertTrue |
| 11 | +import kotlin.concurrent.thread |
| 12 | +import kotlin.test.Test |
| 13 | +import kotlin.test.AfterTest |
| 14 | +import kotlin.test.BeforeTest |
| 15 | +import kotlinx.coroutines.CompletableDeferred |
| 16 | +import kotlinx.coroutines.Dispatchers |
| 17 | +import kotlinx.coroutines.delay |
| 18 | +import kotlinx.coroutines.test.runTest |
| 19 | +import kotlinx.coroutines.withContext |
| 20 | +import kotlin.time.Duration.Companion.seconds |
| 21 | + |
| 22 | +class ConnectionTestJvm { |
| 23 | + private var server: Process? = null |
| 24 | + private var serverOutputThread: Thread? = null |
| 25 | + private val serverReady = CountDownLatch(1) |
| 26 | + |
| 27 | + @BeforeTest |
| 28 | + fun startServer() { |
| 29 | + Logging.info(TAG, "startServer") |
| 30 | + val process = ProcessBuilder("node", "src/jvmTest/resources/socket-server.js", "/") |
| 31 | + .redirectErrorStream(true) |
| 32 | + .start() |
| 33 | + server = process |
| 34 | + serverOutputThread = thread(start = true, isDaemon = true, name = "socket-server-jvm-test-stdout") { |
| 35 | + BufferedReader(InputStreamReader(process.inputStream)).use { reader -> |
| 36 | + while (true) { |
| 37 | + val line = reader.readLine() ?: break |
| 38 | + Logging.info(TAG, "SERVER OUT: $line") |
| 39 | + if (line.contains("Socket.IO server listening on port 3000")) { |
| 40 | + serverReady.countDown() |
| 41 | + } |
| 42 | + } |
| 43 | + } |
| 44 | + } |
| 45 | + check(serverReady.await(3, TimeUnit.SECONDS)) { "Socket.IO test server did not start in 3s" } |
| 46 | + Logging.info(TAG, "startServer finish") |
| 47 | + } |
| 48 | + |
| 49 | + @AfterTest |
| 50 | + fun stopServer() { |
| 51 | + Logging.info(TAG, "stopServer") |
| 52 | + serverReady.countDown() |
| 53 | + server?.let { process -> |
| 54 | + process.destroy() |
| 55 | + if (!process.waitFor(3, TimeUnit.SECONDS)) { |
| 56 | + process.destroyForcibly() |
| 57 | + process.waitFor(3, TimeUnit.SECONDS) |
| 58 | + } |
| 59 | + } |
| 60 | + server = null |
| 61 | + |
| 62 | + serverOutputThread?.join(1000) |
| 63 | + serverOutputThread = null |
| 64 | + Logging.info(TAG, "stopServer finish") |
| 65 | + } |
| 66 | + |
| 67 | + @Test |
| 68 | + fun shouldExposeConnectionState() = runTest(timeout = 10.seconds) { |
| 69 | + withContext(Dispatchers.Default) { |
| 70 | + delay(1000) |
| 71 | + } |
| 72 | + |
| 73 | + val isConnectedBeforeOpen = CompletableDeferred<Boolean>() |
| 74 | + val isConnectedWhenConnectedEvent = CompletableDeferred<Boolean>() |
| 75 | + val isConnectedWhenDisconnectedEvent = CompletableDeferred<Boolean>() |
| 76 | + |
| 77 | + val opt = IO.Options() |
| 78 | + opt.transports = listOf(WebSocket.NAME) |
| 79 | + IO.socket("http://localhost:3000/", opt) { socket -> |
| 80 | + isConnectedBeforeOpen.complete(socket.connected) |
| 81 | + socket.on(Socket.EVENT_CONNECT) { |
| 82 | + isConnectedWhenConnectedEvent.complete(socket.connected) |
| 83 | + socket.close() |
| 84 | + }.on(Socket.EVENT_DISCONNECT) { |
| 85 | + isConnectedWhenDisconnectedEvent.complete(socket.connected) |
| 86 | + } |
| 87 | + |
| 88 | + socket.open() |
| 89 | + } |
| 90 | + |
| 91 | + assertFalse(isConnectedBeforeOpen.await()) |
| 92 | + assertTrue(isConnectedWhenConnectedEvent.await()) |
| 93 | + assertFalse(isConnectedWhenDisconnectedEvent.await()) |
| 94 | + } |
| 95 | + |
| 96 | + companion object { |
| 97 | + private const val TAG = "ConnectionTestJvm" |
| 98 | + } |
| 99 | +} |
0 commit comments