Skip to content

Commit 2ea83c2

Browse files
committed
Fix some bugs
1 parent 0d056c0 commit 2ea83c2

File tree

3 files changed

+41
-39
lines changed

3 files changed

+41
-39
lines changed

Sources/GRPC/ConnectionManagerChannelProvider.swift

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -266,14 +266,6 @@ internal struct DefaultChannelProvider: ConnectionManagerChannelProvider {
266266
return channel.eventLoop.makeFailedFuture(error)
267267
}
268268

269-
#if canImport(Network)
270-
if #available(macOS 10.14, iOS 12.0, watchOS 6.0, tvOS 12.0, *),
271-
let configurator = self.nwParametersConfigurator,
272-
let transportServicesBootstrap = bootstrap as? NIOTSConnectionBootstrap {
273-
_ = transportServicesBootstrap.configureNWParameters(configurator)
274-
}
275-
#endif
276-
277269
// Run the debug initializer, if there is one.
278270
if let debugInitializer = self.debugChannelInitializer {
279271
return debugInitializer(channel)
@@ -286,6 +278,14 @@ internal struct DefaultChannelProvider: ConnectionManagerChannelProvider {
286278
_ = bootstrap.connectTimeout(connectTimeout)
287279
}
288280

281+
#if canImport(Network)
282+
if #available(macOS 10.14, iOS 12.0, watchOS 6.0, tvOS 12.0, *),
283+
let configurator = self.nwParametersConfigurator,
284+
let transportServicesBootstrap = bootstrap as? NIOTSConnectionBootstrap {
285+
_ = transportServicesBootstrap.configureNWParameters(configurator)
286+
}
287+
#endif
288+
289289
return bootstrap.connect(to: self.connectionTarget)
290290
}
291291
}

Tests/GRPCTests/ConnectionManagerTests.swift

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1421,49 +1421,49 @@ extension ConnectionManagerTests {
14211421

14221422
#if canImport(Network)
14231423
func testDefaultChannelProvider_NWParametersConfigurator() throws {
1424+
// For this test, we want an actual connection to be established, since otherwise the parameters
1425+
// configurator won't be run: NIOTS will only apply the parameters on the NWConnection at the
1426+
// point of activating it.
1427+
1428+
// Start a server
1429+
let serverConfig = Server.Configuration.default(
1430+
target: .hostAndPort("localhost", 0),
1431+
eventLoopGroup: NIOTSEventLoopGroup.singleton,
1432+
serviceProviders: []
1433+
)
1434+
let server = try Server.start(configuration: serverConfig).wait()
1435+
defer {
1436+
try? server.close().wait()
1437+
}
1438+
1439+
// Create a connection manager, and configure it to increase a counter in its NWParameters
1440+
// configurator closure.
14241441
let counter = NIOLockedValueBox(0)
14251442
let group = NIOTSEventLoopGroup.singleton
14261443
var configuration = ClientConnection.Configuration.default(
1427-
target: .unixDomainSocket("/ignored"),
1444+
target: .socketAddress(server.channel.localAddress!),
14281445
eventLoopGroup: group
14291446
)
14301447
configuration.nwParametersConfigurator = { _ in
14311448
counter.withLockedValue { $0 += 1 }
14321449
}
1433-
1434-
// We need to trigger the connection to apply the parameters configurator.
1435-
// However, we don't actually care about establishing the connection: only triggering it.
1436-
// In other tests, we used mocked channel providers to simply return completed futures when "creating"
1437-
// the channels. However, in this test we want to make sure that the `DefaultChannelProvider`,
1438-
// which is the one we actually use, correctly sets and executes the configurator.
1439-
// For this reason, we can wait on a promise that is succeeded after the configurator has been called,
1440-
// in the debug channel initializer. This promise will always succeed regardless of the actual
1441-
// connection being established. And because this closure is executed after the parameters configurator,
1442-
// we know the counter should be updated by the time the promise has been completed.
1443-
let promise = group.next().makePromise(of: Void.self)
1444-
configuration.debugChannelInitializer = { channel in
1445-
promise.succeed()
1446-
return promise.futureResult
1447-
}
1448-
14491450
let manager = ConnectionManager(
14501451
configuration: configuration,
14511452
connectivityDelegate: self.monitor,
14521453
idleBehavior: .closeWhenIdleTimeout,
14531454
logger: self.logger
14541455
)
1456+
defer {
1457+
try? manager.shutdown().wait()
1458+
}
14551459

1456-
// Start connecting. We don't care about the actual result of the connection.
1457-
_ = manager.getHTTP2Multiplexer()
1458-
1459-
// Wait until the configurator has been called.
1460-
try promise.futureResult.wait()
1460+
// Wait for the connection to be established.
1461+
_ = try manager.getHTTP2Multiplexer().wait()
14611462

1463+
// At this point, the configurator must have been called.
14621464
XCTAssertEqual(1, counter.withLockedValue({ $0 }))
14631465
}
14641466
#endif
1465-
1466-
// TODO: add test using the default channel provider that uses the parameter configurator and make sure it gets called
14671467
}
14681468

14691469
internal struct Change: Hashable, CustomStringConvertible {

Tests/GRPCTests/ConnectionPool/GRPCChannelPoolTests.swift

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,11 @@ final class GRPCChannelPoolTests: GRPCTestCase {
6969
self.group = MultiThreadedEventLoopGroup(numberOfThreads: threads)
7070

7171
case .transportServicesEventLoopGroup:
72+
#if canImport(Network)
7273
self.group = NIOTSEventLoopGroup(loopCount: threads)
74+
#else
75+
fatalError("NIOTS is not available on this platform.")
76+
#endif
7377
}
7478
}
7579

@@ -127,9 +131,10 @@ final class GRPCChannelPoolTests: GRPCTestCase {
127131
private func setUpClientAndServer(
128132
withTLS tls: Bool,
129133
threads: Int = System.coreCount,
134+
eventLoopGroupType: TestEventLoopGroupType = .multiThreadedEventLoopGroup,
130135
_ configure: (inout GRPCChannelPool.Configuration) -> Void = { _ in }
131136
) {
132-
self.configureEventLoopGroup(threads: threads)
137+
self.configureEventLoopGroup(threads: threads, eventLoopGroupType: eventLoopGroupType)
133138
self.startServer(withTLS: tls)
134139
self.startChannel(withTLS: tls) {
135140
// We'll allow any number of waiters since we immediately fire off a bunch of RPCs and don't
@@ -639,19 +644,16 @@ final class GRPCChannelPoolTests: GRPCTestCase {
639644
}
640645

641646
#if canImport(Network)
642-
func testNWParametersConfigurator() {
647+
func testNWParametersConfigurator() throws {
643648
let counter = NIOLockedValueBox(0)
644-
self.configureEventLoopGroup(threads: 1, eventLoopGroupType: .transportServicesEventLoopGroup)
645-
self.startServer(withTLS: false)
646-
self.startChannel(withTLS: false) { configuration in
649+
self.setUpClientAndServer(withTLS: false, eventLoopGroupType: .transportServicesEventLoopGroup) { configuration in
647650
configuration.transportServices.nwParametersConfigurator = { _ in
648651
counter.withLockedValue { $0 += 1 }
649652
}
650653
}
651654

652655
// Execute an RPC to make sure a channel gets created/activated and the parameters configurator run.
653-
let rpc = self.echo.get(.with { $0.text = "" })
654-
XCTAssertNoThrow(try rpc.status.wait())
656+
try self.doTestUnaryRPCs(count: 1)
655657

656658
XCTAssertEqual(1, counter.withLockedValue({ $0 }))
657659
}

0 commit comments

Comments
 (0)