BasicEchoTestCase.swift 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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 EchoImplementation
  18. import EchoModel
  19. import Foundation
  20. import GRPC
  21. import GRPCSampleData
  22. import NIO
  23. import NIOSSL
  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(self.makeErrorDelegate())
  88. .withServiceProviders([self.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. )
  112. self.server = try! self.makeServer()
  113. self.port = self.server.channel.localAddress!.port!
  114. self.clientEventLoopGroup = PlatformSupport.makeEventLoopGroup(
  115. loopCount: 1,
  116. networkPreference: self.networkPreference
  117. )
  118. self.client = try! self.makeEchoClient(port: self.port)
  119. }
  120. override func tearDown() {
  121. // Some tests close the channel, so would throw here if called twice.
  122. try? self.client.channel.close().wait()
  123. XCTAssertNoThrow(try self.clientEventLoopGroup.syncShutdownGracefully())
  124. self.client = nil
  125. self.clientEventLoopGroup = nil
  126. XCTAssertNoThrow(try self.server.close().wait())
  127. XCTAssertNoThrow(try self.serverEventLoopGroup.syncShutdownGracefully())
  128. self.server = nil
  129. self.serverEventLoopGroup = nil
  130. self.port = nil
  131. super.tearDown()
  132. }
  133. }
  134. extension EchoTestCaseBase {
  135. func makeExpectation(
  136. description: String,
  137. expectedFulfillmentCount: Int = 1,
  138. assertForOverFulfill: Bool = true
  139. ) -> XCTestExpectation {
  140. let expectation = self.expectation(description: description)
  141. expectation.expectedFulfillmentCount = expectedFulfillmentCount
  142. expectation.assertForOverFulfill = assertForOverFulfill
  143. return expectation
  144. }
  145. func makeStatusExpectation(expectedFulfillmentCount: Int = 1) -> XCTestExpectation {
  146. return self.makeExpectation(
  147. description: "Expecting status received",
  148. expectedFulfillmentCount: expectedFulfillmentCount
  149. )
  150. }
  151. func makeResponseExpectation(expectedFulfillmentCount: Int = 1) -> XCTestExpectation {
  152. return self.makeExpectation(
  153. description: "Expecting \(expectedFulfillmentCount) response(s)",
  154. expectedFulfillmentCount: expectedFulfillmentCount
  155. )
  156. }
  157. func makeRequestExpectation(expectedFulfillmentCount: Int = 1) -> XCTestExpectation {
  158. return self.makeExpectation(
  159. description: "Expecting \(expectedFulfillmentCount) request(s) to have been sent",
  160. expectedFulfillmentCount: expectedFulfillmentCount
  161. )
  162. }
  163. func makeInitialMetadataExpectation() -> XCTestExpectation {
  164. return self.makeExpectation(description: "Expecting initial metadata")
  165. }
  166. }