Skip to content

Commit

Permalink
Fix JS tests
Browse files Browse the repository at this point in the history
  • Loading branch information
davidepianca98 committed Aug 8, 2023
1 parent 716d6f6 commit 6f9eefd
Show file tree
Hide file tree
Showing 10 changed files with 76 additions and 26 deletions.
1 change: 1 addition & 0 deletions gradle.properties
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
kotlin.code.style=official
serializationVersion=1.5.1
coroutineVersion=1.7.3
kotlin.js.generate.executable.default=false
4 changes: 3 additions & 1 deletion kmqtt-broker/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ plugins {
}

val serializationVersion: String by project
val coroutineVersion: String by project

kotlin {
explicitApi()
Expand Down Expand Up @@ -63,13 +64,15 @@ kotlin {
implementation(project(":kmqtt-common"))
implementation("org.jetbrains.kotlinx:kotlinx-serialization-core:$serializationVersion")
implementation("org.jetbrains.kotlinx:kotlinx-serialization-protobuf:$serializationVersion")
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:$coroutineVersion")
}
}
val commonTest by getting {
dependencies {
implementation(kotlin("test-common"))
implementation(kotlin("test-annotations-common"))
implementation(project(":kmqtt-client"))
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-test:$coroutineVersion")
}
}
val jvmMain by getting {
Expand All @@ -86,7 +89,6 @@ kotlin {
val jsMain by getting {
dependencies {
implementation("org.jetbrains.kotlin-wrappers:kotlin-node:18.16.12-pre.599")
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core-js:1.7.2")
}
}
val jsTest by getting {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package integration

import IgnoreJs
import MQTTClient
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.test.runTest
import kotlinx.coroutines.withContext
import mqtt.MQTTException
import mqtt.broker.Broker
import mqtt.broker.interfaces.Authentication
Expand All @@ -11,10 +14,9 @@ import mqtt.packets.mqttv5.ReasonCode
import kotlin.test.Test
import kotlin.test.assertFailsWith

@IgnoreJs
class AuthenticationTest {

private fun testAuthentication(
private suspend fun testAuthentication(
client: MQTTClient,
broker: Broker
) {
Expand All @@ -23,6 +25,9 @@ class AuthenticationTest {
broker.step()
client.step()
i++
withContext(Dispatchers.Default) {
delay(10)
}
}
if (i >= 1000) {
throw Exception("Test timeout")
Expand All @@ -31,7 +36,7 @@ class AuthenticationTest {
}

@Test
fun testSimpleAuthentication() {
fun testSimpleAuthentication() = runTest {
val broker = Broker(authentication = object : Authentication {
override fun authenticate(clientId: String, username: String?, password: UByteArray?): Boolean {
return username == "user" && password?.toByteArray()?.decodeToString() == "pass"
Expand All @@ -45,7 +50,7 @@ class AuthenticationTest {
}

@Test
fun testSimpleAuthenticationFailure() {
fun testSimpleAuthenticationFailure() = runTest {
val broker = Broker(authentication = object : Authentication {
override fun authenticate(clientId: String, username: String?, password: UByteArray?): Boolean {
return username == "user" && password?.toByteArray()?.decodeToString() == "pass"
Expand All @@ -61,7 +66,7 @@ class AuthenticationTest {
}

@Test
fun testEnhancedAuthentication() {
fun testEnhancedAuthentication() = runTest {
val broker =
Broker(enhancedAuthenticationProviders = mapOf("TEST-EN-AUTH" to object :
EnhancedAuthenticationProvider {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package integration

import IgnoreJs
import MQTTClient
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.test.runTest
import kotlinx.coroutines.withContext
import mqtt.Subscription
import mqtt.broker.Broker
import mqtt.packets.Qos
Expand All @@ -10,10 +13,9 @@ import kotlin.test.Test
import kotlin.test.assertContentEquals
import kotlin.test.assertEquals

@IgnoreJs
class PublishSubscribeMultipleClientsTest {

private fun testPublish(qos: Qos, topic: String, payload: UByteArray) {
private suspend fun testPublish(qos: Qos, topic: String, payload: UByteArray) {
var received = false

val broker = Broker()
Expand All @@ -39,6 +41,9 @@ class PublishSubscribeMultipleClientsTest {
client1.step()
client2.step()
i++
withContext(Dispatchers.Default) {
delay(10)
}
}

broker.stop()
Expand All @@ -49,23 +54,23 @@ class PublishSubscribeMultipleClientsTest {
}

@Test
fun testPublishSubscribeTopicQos0() {
fun testPublishSubscribeTopicQos0() = runTest {
val sendPayload = "Test1Test1Test1Test1Test1Test1Test1Test1Test1Test1Test1Test1".encodeToByteArray()
val topic = "test/topic"

testPublish(Qos.AT_MOST_ONCE, topic, sendPayload.toUByteArray())
}

@Test
fun testPublishSubscribeTopicQos1() {
fun testPublishSubscribeTopicQos1() = runTest {
val sendPayload = "Test1Test1Test1Test1Test1Test1Test1Test1Test1Test1Test1Test1".encodeToByteArray()
val topic = "test/topic"

testPublish(Qos.AT_LEAST_ONCE, topic, sendPayload.toUByteArray())
}

@Test
fun testPublishSubscribeTopicQos2() {
fun testPublishSubscribeTopicQos2() = runTest {
val sendPayload = "Test1Test1Test1Test1Test1Test1Test1Test1Test1Test1Test1Test1".encodeToByteArray()
val topic = "test/topic"

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package integration

import IgnoreJs
import MQTTClient
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.test.runTest
import kotlinx.coroutines.withContext
import mqtt.Subscription
import mqtt.broker.Broker
import mqtt.packets.Qos
Expand All @@ -11,11 +14,10 @@ import kotlin.test.assertContentEquals
import kotlin.test.assertEquals


@IgnoreJs
class PublishSubscribeSingleClientTest {

@Test
fun testPublish() {
fun testPublish() = runTest {
val sendPayload = "Test"
val topic = "test/topic"

Expand All @@ -41,6 +43,9 @@ class PublishSubscribeSingleClientTest {
broker.step()
client.step()
i++
withContext(Dispatchers.Default) {
delay(10)
}
}

broker.stop()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package integration

import IgnoreJs
import MQTTClient
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.test.runTest
import kotlinx.coroutines.withContext
import mqtt.Subscription
import mqtt.broker.Broker
import mqtt.packets.Qos
Expand All @@ -10,11 +13,10 @@ import kotlin.test.Test
import kotlin.test.assertContentEquals
import kotlin.test.assertEquals

@IgnoreJs
class RetainedPublishTest {

@Test
fun testRetained() {
fun testRetained() = runTest {
val qos0Topic = "from/qos 0"
val qos1Topic = "from/qos 1"
val qos2Topic = "from/qos 2"
Expand Down Expand Up @@ -55,6 +57,9 @@ class RetainedPublishTest {
broker.step()
client.step()
i++
withContext(Dispatchers.Default) {
delay(10)
}
}

broker.stop()
Expand Down
11 changes: 8 additions & 3 deletions kmqtt-broker/src/commonTest/kotlin/integration/TLSTest.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package integration

import IgnoreJs
import MQTTClient
import TLSClientSettings
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.test.runTest
import kotlinx.coroutines.withContext
import mqtt.Subscription
import mqtt.broker.Broker
import mqtt.packets.Qos
Expand All @@ -12,11 +15,10 @@ import kotlin.test.Test
import kotlin.test.assertContentEquals
import kotlin.test.assertEquals

@IgnoreJs
class TLSTest {

@Test
fun testPublish() {
fun testPublish() = runTest {
val sendPayload = "Test"
val topic = "test/topic"

Expand All @@ -42,6 +44,9 @@ class TLSTest {
broker.step()
client.step()
i++
withContext(Dispatchers.Default) {
delay(10)
}
}

broker.stop()
Expand Down
6 changes: 5 additions & 1 deletion kmqtt-client/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@ kotlin {
binaries.executable()
}
}
mingwX64 {}
mingwX64 {
binaries {
executable { }
}
}
linuxX64 {}
linuxArm64 {}
iosX64 {}
Expand Down
18 changes: 16 additions & 2 deletions kmqtt-client/src/commonMain/kotlin/MQTTClient.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import mqtt.packets.mqtt.*
import mqtt.packets.mqttv4.*
import mqtt.packets.mqttv5.*
import socket.IOException
import socket.SocketClosedException
import socket.SocketInterface
import socket.streams.EOFException

Expand Down Expand Up @@ -279,7 +280,14 @@ public class MQTTClient(
}
}

private var lastException: Exception? = null

private fun check() {
if (socket == null) {
close()
// Needed because of JS callbacks, otherwise the exception gets swallowed and tests don't complete correctly
throw lastException ?: SocketClosedException("")
}
val data = socket!!.read()
lock.withLock {
if (data != null) {
Expand All @@ -290,20 +298,24 @@ public class MQTTClient(
handlePacket(it)
}
} catch (e: MQTTException) {
lastException = e
e.printStackTrace()
disconnect(e.reasonCode)
close()
throw e
} catch (e: EOFException) {
lastException = e
println("EOF")
close()
throw e
} catch (e: IOException) {
lastException = e
println("IOException ${e.message}")
disconnect(ReasonCode.UNSPECIFIED_ERROR)
close()
throw e
} catch (e: Exception) {
lastException = e
println("Exception ${e.message} ${e.cause?.message}")
disconnect(ReasonCode.IMPLEMENTATION_SPECIFIC_ERROR)
close()
Expand All @@ -314,14 +326,16 @@ public class MQTTClient(
val currentTime = currentTimeMillis()
if (!connackReceived && currentTime > lastActiveTimestamp + 30000) {
close()
throw Exception("CONNACK not received in 30 seconds")
lastException = Exception("CONNACK not received in 30 seconds")
throw lastException!!
}

if (keepAlive != 0 && connackReceived) {
if (currentTime > lastActiveTimestamp + (keepAlive * 1000)) {
// Timeout
close()
throw MQTTException(ReasonCode.KEEP_ALIVE_TIMEOUT)
lastException = MQTTException(ReasonCode.KEEP_ALIVE_TIMEOUT)
throw lastException!!
} else if (currentTime > lastActiveTimestamp + (keepAlive * 1000 * 0.9)) {
val pingreq = if (mqttVersion == 4) {
MQTT4Pingreq()
Expand Down
6 changes: 5 additions & 1 deletion kmqtt-common/src/jsMain/kotlin/utils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@
import node.buffer.Buffer

public fun UByteArray.toBuffer(): Buffer {
return Buffer(this)
val result = Buffer.alloc(this.size)
for (i in indices) {
result.writeUint8(this[i].toInt(), i)
}
return result
}

public fun Buffer.toUByteArray(): UByteArray {
Expand Down

0 comments on commit 6f9eefd

Please sign in to comment.