Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9ae0756
commit fa61e76
Showing
6 changed files
with
92 additions
and
90 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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
28
latte/src/test/kotlin/gg/beemo/latte/broker/BrokerClientTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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
34
latte/src/test/kotlin/gg/beemo/latte/broker/TestBrokerClient.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} | ||
|
||
} |