Skip to content

Commit

Permalink
swift: add test for closing gRPC streams
Browse files Browse the repository at this point in the history
Adding a test to match Android (see [this comment](#472 (comment))) to validate that gRPC streams are closed with `nil` trailers, so as to force the stream to close with a data frame per the gRPC protocol spec.

Signed-off-by: Michael Rebello <[email protected]>
  • Loading branch information
rebello95 committed Oct 16, 2019
1 parent cf8f2c8 commit 9dd4144
Showing 1 changed file with 18 additions and 3 deletions.
21 changes: 18 additions & 3 deletions library/swift/test/GRPCStreamEmitterTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,14 @@ import XCTest
private let kMessageData = Data([1, 2, 3, 4])

private final class MockEmitter: StreamEmitter {
private let onSendData: (Data) -> Void
private let onSendData: (_ data: Data) -> Void
private let onClose: ((_ trailers: [String: [String]]?) -> Void)?

init(onSendData: @escaping (Data) -> Void) {
init(onSendData: @escaping (_ data: Data) -> Void,
onClose: ((_ trailers: [String: [String]]?) -> Void)? = nil)
{
self.onSendData = onSendData
self.onClose = onClose
}

func sendData(_ data: Data) -> StreamEmitter {
Expand All @@ -20,7 +24,10 @@ private final class MockEmitter: StreamEmitter {
return self
}

func close(trailers: [String: [String]]?) {}
func close(trailers: [String: [String]]?) {
self.onClose?(trailers)
}

func cancel() {}
}

Expand Down Expand Up @@ -59,4 +66,12 @@ final class GRPCStreamEmitterTests: XCTestCase {
grpcEmitter.sendMessage(kMessageData)
XCTAssertEqual(kMessageData, sentData.subdata(in: 5..<sentData.count))
}

func testCloseIsCalledWithNilTrailersInOrderToCloseWithEmptyDataFrame() {
var sentTrailers: [String: [String]]? = ["x": ["invalid"]]
let mockEmitter = MockEmitter(onSendData: { _ in }, onClose: { sentTrailers = $0 })
let grpcEmitter = GRPCStreamEmitter(emitter: mockEmitter)
grpcEmitter.close()
XCTAssertNil(sentTrailers)
}
}

0 comments on commit 9dd4144

Please sign in to comment.