Skip to content

Commit a653cc1

Browse files
authored
Added test for non-serializable params (#237)
1 parent a21a5cc commit a653cc1

File tree

5 files changed

+89
-5
lines changed

5 files changed

+89
-5
lines changed

.github/workflows/labels.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ jobs:
1515
check-labels:
1616
name: Check labels
1717
runs-on: ubuntu-latest
18-
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') }}
18+
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') }}
1919
steps:
2020
- name: Fail build after no labels present
2121
run: |
2222
echo "Pull request does not contain any required labels"
23-
echo "Please use at least one of 'feature', 'bug', 'breaking', 'infra', 'docs', 'deprecation', 'release' or 'dependencies' labels"
23+
echo "Please use at least one of 'feature', 'bug', 'breaking', 'infra', 'docs', 'deprecation', 'release', 'housekeeping' or 'dependencies' labels"
2424
exit 1

krpc/krpc-test/src/jvmMain/kotlin/kotlinx/rpc/krpc/test/KrpcTestService.kt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ import kotlinx.coroutines.flow.SharedFlow
99
import kotlinx.coroutines.flow.StateFlow
1010
import kotlinx.rpc.RemoteService
1111
import kotlinx.rpc.annotations.Rpc
12+
import kotlinx.serialization.Contextual
13+
import kotlinx.serialization.Serializable
14+
import java.time.LocalDate
15+
import java.time.LocalDateTime
1216

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

35+
suspend fun nonSerializableClass(localDate: @Contextual LocalDate): LocalDate
36+
suspend fun nonSerializableClassWithSerializer(
37+
localDateTime: @Serializable(LocalDateTimeSerializer::class) LocalDateTime,
38+
): @Serializable(LocalDateTimeSerializer::class) LocalDateTime
39+
3140
suspend fun incomingStreamSyncCollect(arg1: Flow<String>): Int
3241
suspend fun incomingStreamAsyncCollect(arg1: Flow<String>): Int
3342
suspend fun outgoingStream(): Flow<String>

krpc/krpc-test/src/jvmMain/kotlin/kotlinx/rpc/krpc/test/KrpcTestServiceBackend.kt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ package kotlinx.rpc.krpc.test
77
import kotlinx.coroutines.*
88
import kotlinx.coroutines.flow.*
99
import kotlinx.serialization.Serializable
10+
import java.time.LocalDate
11+
import java.time.LocalDateTime
12+
import java.time.format.DateTimeFormatter
1013
import kotlin.coroutines.CoroutineContext
1114
import kotlin.coroutines.resumeWithException
1215
import kotlin.test.assertEquals
@@ -79,6 +82,14 @@ class KrpcTestServiceBackend(override val coroutineContext: CoroutineContext) :
7982
return TestList(3)
8083
}
8184

85+
override suspend fun nonSerializableClass(localDate: LocalDate): LocalDate {
86+
return localDate.plusDays(1)
87+
}
88+
89+
override suspend fun nonSerializableClassWithSerializer(localDateTime: LocalDateTime): String {
90+
return localDateTime.plusDays(1).format(DateTimeFormatter.ISO_DATE_TIME)
91+
}
92+
8293
override suspend fun incomingStreamSyncCollect(arg1: Flow<String>): Int {
8394
return arg1.count()
8495
}

krpc/krpc-test/src/jvmMain/kotlin/kotlinx/rpc/krpc/test/KrpcTransportTestBase.kt

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,60 @@ import kotlinx.rpc.krpc.server.KrpcServer
1818
import kotlinx.rpc.krpc.streamScoped
1919
import kotlinx.rpc.registerService
2020
import kotlinx.rpc.withService
21+
import kotlinx.serialization.KSerializer
22+
import kotlinx.serialization.descriptors.PrimitiveKind
23+
import kotlinx.serialization.descriptors.PrimitiveSerialDescriptor
24+
import kotlinx.serialization.descriptors.SerialDescriptor
25+
import kotlinx.serialization.encoding.Decoder
26+
import kotlinx.serialization.encoding.Encoder
27+
import kotlinx.serialization.modules.SerializersModule
2128
import org.junit.Assert.assertEquals
2229
import org.junit.Rule
2330
import org.junit.rules.Timeout
31+
import java.time.LocalDate
32+
import java.time.LocalDateTime
33+
import java.time.format.DateTimeFormatter
2434
import java.util.concurrent.Semaphore
2535
import java.util.concurrent.TimeUnit
2636
import java.util.concurrent.atomic.AtomicBoolean
2737
import kotlin.coroutines.cancellation.CancellationException
2838
import kotlin.test.*
2939

40+
internal object LocalDateSerializer : KSerializer<LocalDate> {
41+
override val descriptor: SerialDescriptor = PrimitiveSerialDescriptor("LocalDate", PrimitiveKind.STRING)
42+
43+
override fun serialize(
44+
encoder: Encoder,
45+
value: LocalDate,
46+
) {
47+
encoder.encodeString(value.format(DateTimeFormatter.ISO_DATE))
48+
}
49+
50+
override fun deserialize(decoder: Decoder): LocalDate {
51+
return LocalDate.parse(decoder.decodeString(), DateTimeFormatter.ISO_DATE)
52+
}
53+
}
54+
55+
internal object LocalDateTimeSerializer : KSerializer<LocalDateTime> {
56+
override val descriptor: SerialDescriptor = PrimitiveSerialDescriptor("LocalDateTime", PrimitiveKind.STRING)
57+
58+
override fun serialize(
59+
encoder: Encoder,
60+
value: LocalDateTime,
61+
) {
62+
encoder.encodeString(value.format(DateTimeFormatter.ISO_DATE_TIME))
63+
}
64+
65+
override fun deserialize(decoder: Decoder): LocalDateTime {
66+
return LocalDateTime.parse(decoder.decodeString(), DateTimeFormatter.ISO_DATE_TIME)
67+
}
68+
}
69+
3070
abstract class KrpcTransportTestBase {
71+
protected val serializersModule = SerializersModule {
72+
contextual(LocalDate::class) { LocalDateSerializer }
73+
}
74+
3175
protected abstract val serializationConfig: KrpcSerialFormatConfiguration.() -> Unit
3276

3377
private val serverConfig by lazy {
@@ -112,6 +156,20 @@ abstract class KrpcTransportTestBase {
112156
}
113157
}
114158

159+
@Test
160+
@Ignore // todo will fix in next PR
161+
fun nonSerializableParameter() {
162+
runBlocking {
163+
val localDate = LocalDate.of(2001, 8, 23)
164+
val resultDate = client.nonSerializableClass(localDate)
165+
assertEquals(localDate.plusDays(1), resultDate)
166+
167+
val localDateTime = LocalDateTime.of(2001, 8, 23, 0, 0)
168+
val resultDateTime = client.nonSerializableClassWithSerializer(localDateTime)
169+
assertEquals(localDateTime.plusDays(1).format(DateTimeFormatter.ISO_DATE_TIME), resultDateTime)
170+
}
171+
}
172+
115173
@Test
116174
fun doubleGenericReturnType() {
117175
val result = runBlocking { client.doubleGenericReturnType() }

krpc/krpc-test/src/jvmTest/kotlin/kotlinx/rpc/krpc/test/LocalTransportTest.kt

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,22 +23,28 @@ abstract class LocalTransportTest : KrpcTransportTestBase() {
2323

2424
class JsonLocalTransportTest : LocalTransportTest() {
2525
override val serializationConfig: KrpcSerialFormatConfiguration.() -> Unit = {
26-
json()
26+
json {
27+
serializersModule = this@JsonLocalTransportTest.serializersModule
28+
}
2729
}
2830
}
2931

3032
class CborLocalTransportTest : LocalTransportTest() {
3133
@OptIn(ExperimentalSerializationApi::class)
3234
override val serializationConfig: KrpcSerialFormatConfiguration.() -> Unit = {
33-
cbor()
35+
cbor {
36+
serializersModule = this@CborLocalTransportTest.serializersModule
37+
}
3438
}
3539
}
3640

3741
@Suppress("detekt.EmptyFunctionBlock")
3842
class ProtoBufLocalTransportTest : LocalTransportTest() {
3943
@OptIn(ExperimentalSerializationApi::class)
4044
override val serializationConfig: KrpcSerialFormatConfiguration.() -> Unit = {
41-
protobuf()
45+
protobuf {
46+
serializersModule = this@ProtoBufLocalTransportTest.serializersModule
47+
}
4248
}
4349

4450
// 'null' is not supported in ProtoBuf

0 commit comments

Comments
 (0)