Skip to content

Commit 39c567a

Browse files
authored
Add public API to create connection IDs (#1737)
Motivation: Each connection pool delegate method relies on a connection ID. IDs are opaque to users and are created by gRPC from the underlying connection manager. However, this makes testing a connection pool delegate difficult as users must create real connections. Modifications: - Add a public init to the connection ID Result: Users can test connection pool delegate implementations without having a connection pool.
1 parent ab82da8 commit 39c567a

File tree

1 file changed

+23
-3
lines changed

1 file changed

+23
-3
lines changed

Sources/GRPC/ConnectionPool/GRPCChannelPool.swift

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ import Logging
1717
import NIOCore
1818
import NIOPosix
1919

20+
import struct Foundation.UUID
21+
2022
public enum GRPCChannelPool {
2123
/// Make a new ``GRPCChannel`` on which calls may be made to gRPC services.
2224
///
@@ -290,14 +292,32 @@ extension GRPCChannelPool.Configuration {
290292

291293
/// The ID of a connection in the connection pool.
292294
public struct GRPCConnectionID: Hashable, Sendable, CustomStringConvertible {
293-
private let id: ConnectionManagerID
295+
private enum Value: Sendable, Hashable {
296+
case managerID(ConnectionManagerID)
297+
case uuid(UUID)
298+
}
299+
300+
private let id: Value
294301

295302
public var description: String {
296-
return String(describing: self.id)
303+
switch self.id {
304+
case .managerID(let id):
305+
return String(describing: id)
306+
case .uuid(let uuid):
307+
return String(describing: uuid)
308+
}
297309
}
298310

299311
internal init(_ id: ConnectionManagerID) {
300-
self.id = id
312+
self.id = .managerID(id)
313+
}
314+
315+
/// Create a new unique connection ID.
316+
///
317+
/// Normally you don't have to create connection IDs, gRPC will create them on your behalf.
318+
/// However creating them manually is useful when testing the ``GRPCConnectionPoolDelegate``.
319+
public init() {
320+
self.id = .uuid(UUID())
301321
}
302322
}
303323

0 commit comments

Comments
 (0)