Repository: ktorio/ktor
File: ktor-server/ktor-server-test-base/jsAndWasmShared/src/io/ktor/server/test/base/EngineTestBase.jsAndWasmShared.kt (Kotlin)

Code before:
import kotlinx.coroutines.*
import kotlin.coroutines.*
import kotlin.test.*
import kotlin.time.Duration.Companion.seconds

actual abstract class EngineTestBase<
    TEngine : ApplicationEngine,
    TConfiguration : ApplicationEngine.Configuration
    >
actual constructor(
    actual val applicationEngineFactory: ApplicationEngineFactory<TEngine, TConfiguration>,
) : BaseTest(), CoroutineScope {
    private val testJob = Job()
    actual override val coroutineContext: CoroutineContext = testJob + Dispatchers.Default

    @Target(AnnotationTarget.FUNCTION)
    @Retention
    protected actual annotation class Http2Only actual constructor()

    /**
     * It's not possible to find a free port during test setup,
     * as on JS (Node.js) all APIs are non-blocking (suspend).
     * That's why we assign port after the server is started in [startServer]
     * Note: this means, that [port] can be used only after calling [createAndStartServer] or [startServer].
     */
    private var _port: Int = 0
    protected actual var port: Int
        get() {
            check(_port != 0) { "Port is not initialized" }
            return _port
        }
        set(_) {
            error("Can't reassign port.")
        }

    protected actual var sslPort: Int = 0
    protected actual var server: EmbeddedServer<TEngine, TConfiguration>? = null

    protected actual var enableHttp2: Boolean = false
    protected actual var enableSsl: Boolean = false
    protected actual var enableCertVerify: Boolean = false

    @OptIn(DelicateCoroutinesApi::class)
    @AfterTest
    fun tearDownBase() {
        GlobalScope.launch { server?.stopSuspend(gracePeriodMillis = 0, timeoutMillis = 500) }
    }

    protected actual suspend fun createAndStartServer(
        log: Logger?,
        parent: CoroutineContext,

Review comment (refactor):
Please consider extracting a constant for non initialized value and use a different value (other than default port)

Code after:
import kotlinx.coroutines.*
import kotlin.coroutines.*
import kotlin.test.*
import kotlin.time.Duration.Companion.seconds

private const val UNINITIALIZED_PORT = -1
private const val DEFAULT_PORT = 0

actual abstract class EngineTestBase<
    TEngine : ApplicationEngine,
    TConfiguration : ApplicationEngine.Configuration
    >
actual constructor(
    actual val applicationEngineFactory: ApplicationEngineFactory<TEngine, TConfiguration>,
) : BaseTest(), CoroutineScope {
    private val testJob = Job()
    actual override val coroutineContext: CoroutineContext = testJob + Dispatchers.Default

    @Target(AnnotationTarget.FUNCTION)
    @Retention
    protected actual annotation class Http2Only actual constructor()

    /**
     * It's not possible to find a free port during test setup,
     * as on JS (Node.js) all APIs are non-blocking (suspend).
     * That's why we assign port after the server is started in [startServer]
     * Note: this means, that [port] can be used only after calling [createAndStartServer] or [startServer].
     */
    private var _port: Int = UNINITIALIZED_PORT
    protected actual var port: Int
        get() {
            check(_port != UNINITIALIZED_PORT) { "Port is not initialized" }
            return _port
        }
        set(_) {
            error("Can't reassign port.")
        }

    protected actual var sslPort: Int = 0
    protected actual var server: EmbeddedServer<TEngine, TConfiguration>? = null

    protected actual var enableHttp2: Boolean = false
    protected actual var enableSsl: Boolean = false
    protected actual var enableCertVerify: Boolean = false

    @OptIn(DelicateCoroutinesApi::class)
    @AfterTest
    fun tearDownBase() {
        GlobalScope.launch { server?.stopSuspend(gracePeriodMillis = 0, timeoutMillis = 500) }
    }