Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added test for non-serializable params #237

Merged
merged 2 commits into from
Nov 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/labels.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ jobs:
check-labels:
name: Check labels
runs-on: ubuntu-latest
if: ${{ github.event.pull_request.draft == false && !contains(github.event.pull_request.labels.*.name, 'release') && !contains(github.event.pull_request.labels.*.name, 'feature') && !contains(github.event.pull_request.labels.*.name, 'bug') && !contains(github.event.pull_request.labels.*.name, 'breaking') && !contains(github.event.pull_request.labels.*.name, 'infra') && !contains(github.event.pull_request.labels.*.name, 'docs') && !contains(github.event.pull_request.labels.*.name, 'deprecation') && !contains(github.event.pull_request.labels.*.name, 'dependencies') }}
if: ${{ github.event.pull_request.draft == false && !contains(github.event.pull_request.labels.*.name, 'release') && !contains(github.event.pull_request.labels.*.name, 'feature') && !contains(github.event.pull_request.labels.*.name, 'bug') && !contains(github.event.pull_request.labels.*.name, 'breaking') && !contains(github.event.pull_request.labels.*.name, 'infra') && !contains(github.event.pull_request.labels.*.name, 'docs') && !contains(github.event.pull_request.labels.*.name, 'deprecation') && !contains(github.event.pull_request.labels.*.name, 'dependencies') && !contains(github.event.pull_request.labels.*.name, 'housekeeping') }}
steps:
- name: Fail build after no labels present
run: |
echo "Pull request does not contain any required labels"
echo "Please use at least one of 'feature', 'bug', 'breaking', 'infra', 'docs', 'deprecation', 'release' or 'dependencies' labels"
echo "Please use at least one of 'feature', 'bug', 'breaking', 'infra', 'docs', 'deprecation', 'release', 'housekeeping' or 'dependencies' labels"
exit 1
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ import kotlinx.coroutines.flow.SharedFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.rpc.RemoteService
import kotlinx.rpc.annotations.Rpc
import kotlinx.serialization.Contextual
import kotlinx.serialization.Serializable
import java.time.LocalDate
import java.time.LocalDateTime

@Suppress("detekt.TooManyFunctions")
@Rpc
Expand All @@ -28,6 +32,11 @@ interface KrpcTestService : RemoteService {
suspend fun nullable(arg1: String?): TestClass?
suspend fun variance(arg2: TestList<in TestClass>, arg3: TestList2<*>): TestList<out TestClass>?

suspend fun nonSerializableClass(localDate: @Contextual LocalDate): LocalDate
suspend fun nonSerializableClassWithSerializer(
localDateTime: @Serializable(LocalDateTimeSerializer::class) LocalDateTime,
): @Serializable(LocalDateTimeSerializer::class) LocalDateTime

suspend fun incomingStreamSyncCollect(arg1: Flow<String>): Int
suspend fun incomingStreamAsyncCollect(arg1: Flow<String>): Int
suspend fun outgoingStream(): Flow<String>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ package kotlinx.rpc.krpc.test
import kotlinx.coroutines.*
import kotlinx.coroutines.flow.*
import kotlinx.serialization.Serializable
import java.time.LocalDate
import java.time.LocalDateTime
import java.time.format.DateTimeFormatter
import kotlin.coroutines.CoroutineContext
import kotlin.coroutines.resumeWithException
import kotlin.test.assertEquals
Expand Down Expand Up @@ -79,6 +82,14 @@ class KrpcTestServiceBackend(override val coroutineContext: CoroutineContext) :
return TestList(3)
}

override suspend fun nonSerializableClass(localDate: LocalDate): LocalDate {
return localDate.plusDays(1)
}

override suspend fun nonSerializableClassWithSerializer(localDateTime: LocalDateTime): String {
return localDateTime.plusDays(1).format(DateTimeFormatter.ISO_DATE_TIME)
}

override suspend fun incomingStreamSyncCollect(arg1: Flow<String>): Int {
return arg1.count()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,60 @@ import kotlinx.rpc.krpc.server.KrpcServer
import kotlinx.rpc.krpc.streamScoped
import kotlinx.rpc.registerService
import kotlinx.rpc.withService
import kotlinx.serialization.KSerializer
import kotlinx.serialization.descriptors.PrimitiveKind
import kotlinx.serialization.descriptors.PrimitiveSerialDescriptor
import kotlinx.serialization.descriptors.SerialDescriptor
import kotlinx.serialization.encoding.Decoder
import kotlinx.serialization.encoding.Encoder
import kotlinx.serialization.modules.SerializersModule
import org.junit.Assert.assertEquals
import org.junit.Rule
import org.junit.rules.Timeout
import java.time.LocalDate
import java.time.LocalDateTime
import java.time.format.DateTimeFormatter
import java.util.concurrent.Semaphore
import java.util.concurrent.TimeUnit
import java.util.concurrent.atomic.AtomicBoolean
import kotlin.coroutines.cancellation.CancellationException
import kotlin.test.*

internal object LocalDateSerializer : KSerializer<LocalDate> {
override val descriptor: SerialDescriptor = PrimitiveSerialDescriptor("LocalDate", PrimitiveKind.STRING)

override fun serialize(
encoder: Encoder,
value: LocalDate,
) {
encoder.encodeString(value.format(DateTimeFormatter.ISO_DATE))
}

override fun deserialize(decoder: Decoder): LocalDate {
return LocalDate.parse(decoder.decodeString(), DateTimeFormatter.ISO_DATE)
}
}

internal object LocalDateTimeSerializer : KSerializer<LocalDateTime> {
override val descriptor: SerialDescriptor = PrimitiveSerialDescriptor("LocalDateTime", PrimitiveKind.STRING)

override fun serialize(
encoder: Encoder,
value: LocalDateTime,
) {
encoder.encodeString(value.format(DateTimeFormatter.ISO_DATE_TIME))
}

override fun deserialize(decoder: Decoder): LocalDateTime {
return LocalDateTime.parse(decoder.decodeString(), DateTimeFormatter.ISO_DATE_TIME)
}
}

abstract class KrpcTransportTestBase {
protected val serializersModule = SerializersModule {
contextual(LocalDate::class) { LocalDateSerializer }
}

protected abstract val serializationConfig: KrpcSerialFormatConfiguration.() -> Unit

private val serverConfig by lazy {
Expand Down Expand Up @@ -112,6 +156,20 @@ abstract class KrpcTransportTestBase {
}
}

@Test
@Ignore // todo will fix in next PR
fun nonSerializableParameter() {
runBlocking {
val localDate = LocalDate.of(2001, 8, 23)
val resultDate = client.nonSerializableClass(localDate)
assertEquals(localDate.plusDays(1), resultDate)

val localDateTime = LocalDateTime.of(2001, 8, 23, 0, 0)
val resultDateTime = client.nonSerializableClassWithSerializer(localDateTime)
assertEquals(localDateTime.plusDays(1).format(DateTimeFormatter.ISO_DATE_TIME), resultDateTime)
}
}

@Test
fun doubleGenericReturnType() {
val result = runBlocking { client.doubleGenericReturnType() }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,28 @@ abstract class LocalTransportTest : KrpcTransportTestBase() {

class JsonLocalTransportTest : LocalTransportTest() {
override val serializationConfig: KrpcSerialFormatConfiguration.() -> Unit = {
json()
json {
serializersModule = [email protected]
}
}
}

class CborLocalTransportTest : LocalTransportTest() {
@OptIn(ExperimentalSerializationApi::class)
override val serializationConfig: KrpcSerialFormatConfiguration.() -> Unit = {
cbor()
cbor {
serializersModule = [email protected]
}
}
}

@Suppress("detekt.EmptyFunctionBlock")
class ProtoBufLocalTransportTest : LocalTransportTest() {
@OptIn(ExperimentalSerializationApi::class)
override val serializationConfig: KrpcSerialFormatConfiguration.() -> Unit = {
protobuf()
protobuf {
serializersModule = [email protected]
}
}

// 'null' is not supported in ProtoBuf
Expand Down