|
| 1 | +package com.piasy.kmp.socketio.socketio |
| 2 | + |
| 3 | +import com.piasy.kmp.xlog.Logging |
| 4 | +import java.io.BufferedReader |
| 5 | +import java.io.InputStreamReader |
| 6 | +import java.util.concurrent.TimeUnit |
| 7 | +import kotlin.concurrent.thread |
| 8 | +import kotlin.test.AfterTest |
| 9 | +import kotlin.test.BeforeTest |
| 10 | + |
| 11 | +class ConnectionTestJvm : ConnectionTest() { |
| 12 | + private var server: Process? = null |
| 13 | + private var serverOutputThread: Thread? = null |
| 14 | + |
| 15 | + @BeforeTest |
| 16 | + override fun startServer() { |
| 17 | + Logging.info(TAG, "startServer") |
| 18 | + val process = ProcessBuilder("node", "src/jvmTest/resources/socket-server.js", "/") |
| 19 | + .redirectErrorStream(true) |
| 20 | + .start() |
| 21 | + server = process |
| 22 | + serverOutputThread = thread(start = true, isDaemon = true, name = "socket-server-jvm-test-stdout") { |
| 23 | + BufferedReader(InputStreamReader(process.inputStream)).use { reader -> |
| 24 | + while (true) { |
| 25 | + val line = reader.readLine() ?: break |
| 26 | + Logging.info(TAG, "SERVER OUT: $line") |
| 27 | + } |
| 28 | + } |
| 29 | + } |
| 30 | + Logging.info(TAG, "startServer finish") |
| 31 | + } |
| 32 | + |
| 33 | + @AfterTest |
| 34 | + override fun stopServer() { |
| 35 | + Logging.info(TAG, "stopServer") |
| 36 | + server?.let { process -> |
| 37 | + process.destroy() |
| 38 | + if (!process.waitFor(3, TimeUnit.SECONDS)) { |
| 39 | + process.destroyForcibly() |
| 40 | + process.waitFor(3, TimeUnit.SECONDS) |
| 41 | + } |
| 42 | + } |
| 43 | + server = null |
| 44 | + |
| 45 | + serverOutputThread?.join(1000) |
| 46 | + serverOutputThread = null |
| 47 | + Logging.info(TAG, "stopServer finish") |
| 48 | + } |
| 49 | +} |
0 commit comments