BasicEchoTestCase.swift 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /*
  2. * Copyright 2018, gRPC Authors All rights reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. import Dispatch
  17. import Foundation
  18. import NIO
  19. import NIOSSL
  20. import GRPC
  21. import GRPCSampleData
  22. import EchoModel
  23. import EchoImplementation
  24. import XCTest
  25. extension Echo_EchoRequest {
  26. init(text: String) {
  27. self = .with {
  28. $0.text = text
  29. }
  30. }
  31. }
  32. extension Echo_EchoResponse {
  33. init(text: String) {
  34. self = .with {
  35. $0.text = text
  36. }
  37. }
  38. }
  39. enum TransportSecurity {
  40. case none
  41. case anonymousClient
  42. case mutualAuthentication
  43. }
  44. class EchoTestCaseBase: GRPCTestCase {
  45. // Things can be slow when running under TSAN; bias towards a really long timeout so that we know
  46. // for sure a test is wedged rather than simply slow.
  47. var defaultTestTimeout: TimeInterval = 120.0
  48. var serverEventLoopGroup: EventLoopGroup!
  49. var clientEventLoopGroup: EventLoopGroup!
  50. var transportSecurity: TransportSecurity { return .none }
  51. var server: Server!
  52. var client: Echo_EchoClient!
  53. var port: Int!
  54. // Prefer POSIX: subclasses can override this and add availability checks to ensure NIOTS
  55. // variants run where possible.
  56. var networkPreference: NetworkPreference {
  57. return .userDefined(.posix)
  58. }
  59. func connectionBuilder() -> ClientConnection.Builder {
  60. switch self.transportSecurity {
  61. case .none:
  62. return ClientConnection.insecure(group: self.clientEventLoopGroup)
  63. case .anonymousClient:
  64. return ClientConnection.secure(group: self.clientEventLoopGroup)
  65. .withTLS(trustRoots: .certificates([SampleCertificate.ca.certificate]))
  66. case .mutualAuthentication:
  67. return ClientConnection.secure(group: self.clientEventLoopGroup)
  68. .withTLS(trustRoots: .certificates([SampleCertificate.ca.certificate]))
  69. .withTLS(certificateChain: [SampleCertificate.client.certificate])
  70. .withTLS(privateKey: SamplePrivateKey.client)
  71. }
  72. }
  73. func serverBuilder() -> Server.Builder {
  74. switch self.transportSecurity {
  75. case .none:
  76. return Server.insecure(group: self.serverEventLoopGroup)
  77. case .anonymousClient, .mutualAuthentication:
  78. return Server.secure(
  79. group: self.serverEventLoopGroup,
  80. certificateChain: [SampleCertificate.server.certificate],
  81. privateKey: SamplePrivateKey.server
  82. ).withTLS(trustRoots: .certificates([SampleCertificate.ca.certificate]))
  83. }
  84. }
  85. func makeServer() throws -> Server {
  86. return try self.serverBuilder()
  87. .withErrorDelegate(makeErrorDelegate())
  88. .withServiceProviders([makeEchoProvider()])
  89. .withLogger(self.serverLogger)
  90. .bind(host: "localhost", port: 0)
  91. .wait()
  92. }
  93. func makeClientConnection(port: Int) throws -> ClientConnection {
  94. return self.connectionBuilder()
  95. .withBackgroundActivityLogger(self.clientLogger)
  96. .connect(host: "localhost", port: port)
  97. }
  98. func makeEchoProvider() -> Echo_EchoProvider { return EchoProvider() }
  99. func makeErrorDelegate() -> ServerErrorDelegate? { return nil }
  100. func makeEchoClient(port: Int) throws -> Echo_EchoClient {
  101. return Echo_EchoClient(
  102. channel: try self.makeClientConnection(port: port),
  103. defaultCallOptions: self.callOptionsWithLogger
  104. )
  105. }
  106. override func setUp() {
  107. super.setUp()
  108. self.serverEventLoopGroup = PlatformSupport.makeEventLoopGroup(
  109. loopCount: 1,
  110. networkPreference: self.networkPreference)
  111. self.server = try! self.makeServer()
  112. self.port = self.server.channel.localAddress!.port!
  113. self.clientEventLoopGroup = PlatformSupport.makeEventLoopGroup(
  114. loopCount: 1,
  115. networkPreference: self.networkPreference)
  116. self.client = try! self.makeEchoClient(port: self.port)
  117. }
  118. override func tearDown() {
  119. // Some tests close the channel, so would throw here if called twice.
  120. try? self.client.channel.close().wait()
  121. XCTAssertNoThrow(try self.clientEventLoopGroup.syncShutdownGracefully())
  122. self.client = nil
  123. self.clientEventLoopGroup = nil
  124. XCTAssertNoThrow(try self.server.close().wait())
  125. XCTAssertNoThrow(try self.serverEventLoopGroup.syncShutdownGracefully())
  126. self.server = nil
  127. self.serverEventLoopGroup = nil
  128. self.port = nil
  129. super.tearDown()
  130. }
  131. }
  132. extension EchoTestCaseBase {
  133. func makeExpectation(description: String, expectedFulfillmentCount: Int = 1, assertForOverFulfill: Bool = true) -> XCTestExpectation {
  134. let expectation = self.expectation(description: description)
  135. expectation.expectedFulfillmentCount = expectedFulfillmentCount
  136. expectation.assertForOverFulfill = assertForOverFulfill
  137. return expectation
  138. }
  139. func makeStatusExpectation(expectedFulfillmentCount: Int = 1) -> XCTestExpectation {
  140. return makeExpectation(description: "Expecting status received",
  141. expectedFulfillmentCount: expectedFulfillmentCount)
  142. }
  143. func makeResponseExpectation(expectedFulfillmentCount: Int = 1) -> XCTestExpectation {
  144. return makeExpectation(description: "Expecting \(expectedFulfillmentCount) response(s)",
  145. expectedFulfillmentCount: expectedFulfillmentCount)
  146. }
  147. func makeRequestExpectation(expectedFulfillmentCount: Int = 1) -> XCTestExpectation {
  148. return makeExpectation(
  149. description: "Expecting \(expectedFulfillmentCount) request(s) to have been sent",
  150. expectedFulfillmentCount: expectedFulfillmentCount)
  151. }
  152. func makeInitialMetadataExpectation() -> XCTestExpectation {
  153. return makeExpectation(description: "Expecting initial metadata")
  154. }
  155. }