Skip to content

Commit

Permalink
Add a first test
Browse files Browse the repository at this point in the history
  • Loading branch information
wasdennnoch committed Dec 3, 2023
1 parent 9ae0756 commit fa61e76
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 90 deletions.
9 changes: 9 additions & 0 deletions latte/build.gradle.kts
Expand Up @@ -26,6 +26,11 @@ dependencies {
implementation("org.jetbrains:annotations:24.1.0")
compileOnly("org.apache.logging.log4j:log4j-api:2.22.0")

// JUnit testing framework
val junitVersion = "5.10.1"
testImplementation("org.junit.jupiter:junit-jupiter-api:$junitVersion")
testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:$junitVersion")

}

repositories {
Expand All @@ -36,4 +41,8 @@ kotlin {
jvmToolchain(17)
}

tasks.test {
useJUnitPlatform()
}

defaultTasks("build")
90 changes: 0 additions & 90 deletions latte/src/main/java/gg/beemo/latte/TestBrokerClient.kt

This file was deleted.

2 changes: 2 additions & 0 deletions latte/src/main/java/gg/beemo/latte/broker/BrokerSubclients.kt
Expand Up @@ -3,6 +3,7 @@ package gg.beemo.latte.broker
import com.squareup.moshi.JsonAdapter
import com.squareup.moshi.Moshi
import gg.beemo.latte.util.MoshiInstantAdapter
import gg.beemo.latte.util.MoshiUnitAdapter
import gg.beemo.latte.util.SuspendingCountDownLatch
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.coroutineScope
Expand Down Expand Up @@ -30,6 +31,7 @@ sealed class BaseSubclient(
// https://github.com/square/moshi#custom-type-adapters
@JvmStatic
protected val moshi: Moshi = Moshi.Builder()
.add(MoshiUnitAdapter())
.add(MoshiInstantAdapter())
.build()
}
Expand Down
19 changes: 19 additions & 0 deletions latte/src/main/java/gg/beemo/latte/util/MoshiAdapters.kt
@@ -1,18 +1,37 @@
package gg.beemo.latte.util

import com.squareup.moshi.FromJson
import com.squareup.moshi.JsonAdapter
import com.squareup.moshi.JsonReader
import com.squareup.moshi.JsonWriter
import com.squareup.moshi.ToJson
import java.time.Instant

class MoshiInstantAdapter : JsonAdapter<Instant>() {

@FromJson
override fun fromJson(reader: JsonReader): Instant? {
return Instant.ofEpochMilli(reader.nextLong())
}

@ToJson
override fun toJson(writer: JsonWriter, value: Instant?) {
writer.value(value?.toEpochMilli())
}

}

class MoshiUnitAdapter : JsonAdapter<Unit>() {

@FromJson
override fun fromJson(reader: JsonReader): Unit? {
reader.skipValue()
return Unit
}

@ToJson
override fun toJson(writer: JsonWriter, value: Unit?) {
writer.nullValue()
}

}
28 changes: 28 additions & 0 deletions latte/src/test/kotlin/gg/beemo/latte/broker/BrokerClientTest.kt
@@ -0,0 +1,28 @@
package gg.beemo.latte.broker

import kotlinx.coroutines.runBlocking
import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.Test

class BrokerClientTest {

private val connection = LocalConnection()
private val client = TestBrokerClient(connection)

@Test
fun `test greeting RPC`() {
runBlocking {
val response = client.greetingRpc.call(GreetingRequest("Beemo"))
Assertions.assertEquals("Hello, Beemo", response.value.greeting)
}
}

@Test
fun `test null RPC`() {
runBlocking {
val response = client.nullRpc.call(null)
Assertions.assertNull(response.value)
}
}

}
34 changes: 34 additions & 0 deletions latte/src/test/kotlin/gg/beemo/latte/broker/TestBrokerClient.kt
@@ -0,0 +1,34 @@
package gg.beemo.latte.broker

import com.squareup.moshi.JsonClass
import kotlinx.coroutines.delay
import kotlin.time.Duration.Companion.seconds


@JsonClass(generateAdapter = true)
data class GreetingRequest(val name: String)

@JsonClass(generateAdapter = true)
data class GreetingResponse(val greeting: String)

class TestBrokerClient(connection: BrokerConnection) : BrokerClient(connection) {

val greetingRpc = rpc<GreetingRequest, GreetingResponse>(
topic = "rpc.greetings",
key = "greeting.requests",
) {
delay(2.seconds)
return@rpc GreetingResponse("Hello, ${it.value.name}")
}

val nullRpc = rpc<Unit?, Unit?>(
topic = "null",
key = "null",
) {
if (it.value != null) {
throw IllegalStateException("nullRpc received non-null")
}
return@rpc null
}

}

0 comments on commit fa61e76

Please sign in to comment.