Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

swift: add test for closing gRPC streams #509

Merged
merged 1 commit into from
Oct 16, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
}
}