Skip to content

Commit 9fd57dc

Browse files
committed
Add PeerInfo (de)serialiser for tests
1 parent da4a7ff commit 9fd57dc

File tree

1 file changed

+25
-8
lines changed

1 file changed

+25
-8
lines changed

Tests/GRPCInProcessTransportTests/InProcessTransportTests.swift

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2024, gRPC Authors All rights reserved.
2+
* Copyright 2024-2025, gRPC Authors All rights reserved.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -77,15 +77,13 @@ struct InProcessTransportTests {
7777
request: ClientRequest(message: ()),
7878
descriptor: .peerInfo,
7979
serializer: VoidSerializer(),
80-
deserializer: UTF8Deserializer(),
80+
deserializer: PeerInfoDeserializer(),
8181
options: .defaults
8282
) {
8383
try $0.message
8484
}
8585

86-
let maybeMatch = peerInfo.wholeMatch(of: /local: in-process:(\d+), remote: in-process:(\d+)/)
87-
let match = try #require(maybeMatch)
88-
#expect(match.1 == match.2)
86+
#expect(peerInfo.local == peerInfo.remote)
8987
}
9088
}
9189
}
@@ -123,8 +121,8 @@ private struct TestService: RegistrableRPCService {
123121
func peerInfo(
124122
request: ServerRequest<Void>,
125123
context: ServerContext
126-
) async throws -> ServerResponse<String> {
127-
let peerInfo = "local: \(context.localPeer), remote: \(context.remotePeer)"
124+
) async throws -> ServerResponse<PeerInfo> {
125+
let peerInfo = PeerInfo(local: context.localPeer, remote: context.remotePeer)
128126
return ServerResponse(message: peerInfo)
129127
}
130128

@@ -141,7 +139,7 @@ private struct TestService: RegistrableRPCService {
141139
router.registerHandler(
142140
forMethod: .peerInfo,
143141
deserializer: VoidDeserializer(),
144-
serializer: UTF8Serializer(),
142+
serializer: PeerInfoSerializer(),
145143
handler: {
146144
let response = try await self.peerInfo(
147145
request: ServerRequest<Void>(stream: $0),
@@ -165,6 +163,25 @@ extension MethodDescriptor {
165163
)
166164
}
167165

166+
private struct PeerInfo: Codable {
167+
var local: String
168+
var remote: String
169+
}
170+
171+
private struct PeerInfoSerializer: MessageSerializer {
172+
func serialize(_ message: PeerInfo) throws -> [UInt8] {
173+
Array("\(message.local) \(message.remote)".utf8)
174+
}
175+
}
176+
177+
private struct PeerInfoDeserializer: MessageDeserializer {
178+
func deserialize(_ serializedMessageBytes: [UInt8]) throws -> PeerInfo {
179+
let stringPeerInfo = String(decoding: serializedMessageBytes, as: UTF8.self)
180+
let peerInfoComponents = stringPeerInfo.split(separator: " ")
181+
return PeerInfo(local: String(peerInfoComponents[0]), remote: String(peerInfoComponents[1]))
182+
}
183+
}
184+
168185
private struct UTF8Serializer: MessageSerializer {
169186
func serialize(_ message: String) throws -> [UInt8] {
170187
Array(message.utf8)

0 commit comments

Comments
 (0)