Skip to content

Commit 5f26c53

Browse files
committed
Simplify unit tests
1 parent c89db83 commit 5f26c53

File tree

2 files changed

+11
-38
lines changed

2 files changed

+11
-38
lines changed

Tests/GRPCCoreTests/Call/Server/Internal/ServerRPCExecutorTests.swift

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -403,14 +403,11 @@ final class ServerRPCExecutorTests: XCTestCase {
403403
}
404404

405405
let harness = ServerRPCExecutorTestHarness(
406-
interceptors: [.throwInProducer(CustomError(), after: .milliseconds(10))]
406+
interceptors: [.throwInProducer(CustomError())]
407407
)
408408
try await harness.execute(handler: .echo) { inbound in
409409
try await inbound.write(.metadata(["foo": "bar"]))
410410
try await inbound.write(.message([0]))
411-
try await Task.sleep(for: .milliseconds(50))
412-
try await inbound.write(.message([1]))
413-
await inbound.finish()
414411
} consumer: { outbound in
415412
let parts = try await outbound.collect()
416413
let status = Status(code: .alreadyExists, message: "foobar")

Tests/GRPCCoreTests/Test Utilities/Call/Server/ServerInterceptors.swift

Lines changed: 10 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ extension ServerInterceptor where Self == RejectAllServerInterceptor {
2626
RejectAllServerInterceptor(throw: error)
2727
}
2828

29-
static func throwInProducer(_ error: any Error, after duration: Duration) -> Self {
30-
RejectAllServerInterceptor(throwInProducer: error, after: duration)
29+
static func throwInProducer(_ error: any Error) -> Self {
30+
RejectAllServerInterceptor(throwInProducer: error)
3131
}
3232

3333
static func throwInMessageSequence(_ error: any Error) -> Self {
@@ -51,7 +51,7 @@ struct RejectAllServerInterceptor: ServerInterceptor {
5151
/// Reject the RPC with a given error.
5252
case reject(RPCError)
5353
/// Throw in the producer closure returned.
54-
case throwInProducer(any Error, after: Duration)
54+
case throwInProducer(any Error)
5555
/// Throw in the async sequence that stream inbound messages.
5656
case throwInMessageSequence(any Error)
5757
}
@@ -72,8 +72,8 @@ struct RejectAllServerInterceptor: ServerInterceptor {
7272
self.mode = .reject(error)
7373
}
7474

75-
init(throwInProducer error: any Error, after duration: Duration) {
76-
self.mode = .throwInProducer(error, after: duration)
75+
init(throwInProducer error: any Error) {
76+
self.mode = .throwInProducer(error)
7777
}
7878

7979
init(throwInMessageSequence error: any Error) {
@@ -93,45 +93,21 @@ struct RejectAllServerInterceptor: ServerInterceptor {
9393
throw error
9494
case .reject(let error):
9595
return StreamingServerResponse(error: error)
96-
case .throwInProducer(let error, let duration):
96+
case .throwInProducer(let error):
9797
var response = try await next(request, context)
9898
switch response.accepted {
9999
case .success(var success):
100100
let wrappedProducer = success.producer
101101
success.producer = { writer in
102-
let result: Result<Metadata, any Error> = await withTaskGroup(of: TimeoutResult.self) { group in
102+
try await withThrowingTaskGroup(of: Metadata.self) { group in
103103
group.addTask {
104-
do {
105-
try await Task.sleep(for: duration, tolerance: .nanoseconds(1))
106-
} catch {
107-
return .cancelled
108-
}
109-
return .throw(error)
104+
try await wrappedProducer(writer)
110105
}
111106

112-
group.addTask {
113-
do {
114-
return .result(try await wrappedProducer(writer))
115-
} catch {
116-
return .throw(error)
117-
}
118-
}
119-
120-
let first = await group.next()!
121107
group.cancelAll()
122-
let second = await group.next()!
123-
124-
switch (first, second) {
125-
case (.throw(let error), _):
126-
return .failure(error)
127-
case (.result(let metadata), _):
128-
return .success(metadata)
129-
case (.cancelled, _):
130-
return .failure(CancellationError())
131-
}
108+
_ = try await group.next()!
109+
throw error
132110
}
133-
134-
return try result.get()
135111
}
136112

137113
response.accepted = .success(success)

0 commit comments

Comments
 (0)