PlatformSupportTests.swift 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. /*
  2. * Copyright 2019, 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 Foundation
  17. @testable import GRPC
  18. import NIO
  19. import NIOTransportServices
  20. import XCTest
  21. class PlatformSupportTests: GRPCTestCase {
  22. var group: EventLoopGroup!
  23. override func tearDown() {
  24. XCTAssertNoThrow(try self.group?.syncShutdownGracefully())
  25. super.tearDown()
  26. }
  27. func testMakeEventLoopGroupReturnsMultiThreadedGroupForPosix() {
  28. self.group = PlatformSupport.makeEventLoopGroup(
  29. loopCount: 1,
  30. networkPreference: .userDefined(.posix)
  31. )
  32. XCTAssertTrue(self.group is MultiThreadedEventLoopGroup)
  33. }
  34. func testMakeEventLoopGroupReturnsNIOTSGroupForNetworkFramework() {
  35. // If we don't have Network.framework then we can't test this.
  36. #if canImport(Network)
  37. guard #available(macOS 10.14, *) else { return }
  38. self.group = PlatformSupport.makeEventLoopGroup(
  39. loopCount: 1,
  40. networkPreference: .userDefined(.networkFramework)
  41. )
  42. XCTAssertTrue(self.group is NIOTSEventLoopGroup)
  43. #endif
  44. }
  45. func testMakeClientBootstrapReturnsClientBootstrapForMultiThreadedGroup() {
  46. self.group = MultiThreadedEventLoopGroup(numberOfThreads: 1)
  47. let bootstrap = PlatformSupport.makeClientBootstrap(group: self.group)
  48. XCTAssertTrue(bootstrap is ClientBootstrap)
  49. }
  50. func testMakeClientBootstrapReturnsClientBootstrapForEventLoop() {
  51. self.group = MultiThreadedEventLoopGroup(numberOfThreads: 1)
  52. let eventLoop = self.group.next()
  53. let bootstrap = PlatformSupport.makeClientBootstrap(group: eventLoop)
  54. XCTAssertTrue(bootstrap is ClientBootstrap)
  55. }
  56. func testMakeClientBootstrapReturnsNIOTSConnectionBootstrapForNIOTSGroup() {
  57. // If we don't have Network.framework then we can't test this.
  58. #if canImport(Network)
  59. guard #available(macOS 10.14, *) else { return }
  60. self.group = NIOTSEventLoopGroup(loopCount: 1)
  61. let bootstrap = PlatformSupport.makeClientBootstrap(group: self.group)
  62. XCTAssertTrue(bootstrap is NIOTSConnectionBootstrap)
  63. #endif
  64. }
  65. func testMakeClientBootstrapReturnsNIOTSConnectionBootstrapForQoSEventLoop() {
  66. // If we don't have Network.framework then we can't test this.
  67. #if canImport(Network)
  68. guard #available(macOS 10.14, *) else { return }
  69. self.group = NIOTSEventLoopGroup(loopCount: 1)
  70. let eventLoop = self.group.next()
  71. XCTAssertTrue(eventLoop is QoSEventLoop)
  72. let bootstrap = PlatformSupport.makeClientBootstrap(group: eventLoop)
  73. XCTAssertTrue(bootstrap is NIOTSConnectionBootstrap)
  74. #endif
  75. }
  76. func testMakeServerBootstrapReturnsServerBootstrapForMultiThreadedGroup() {
  77. self.group = MultiThreadedEventLoopGroup(numberOfThreads: 1)
  78. let bootstrap = PlatformSupport.makeServerBootstrap(group: self.group)
  79. XCTAssertTrue(bootstrap is ServerBootstrap)
  80. }
  81. func testMakeServerBootstrapReturnsServerBootstrapForEventLoop() {
  82. self.group = MultiThreadedEventLoopGroup(numberOfThreads: 1)
  83. let eventLoop = self.group.next()
  84. let bootstrap = PlatformSupport.makeServerBootstrap(group: eventLoop)
  85. XCTAssertTrue(bootstrap is ServerBootstrap)
  86. }
  87. func testMakeServerBootstrapReturnsNIOTSListenerBootstrapForNIOTSGroup() {
  88. // If we don't have Network.framework then we can't test this.
  89. #if canImport(Network)
  90. guard #available(macOS 10.14, *) else { return }
  91. self.group = NIOTSEventLoopGroup(loopCount: 1)
  92. let bootstrap = PlatformSupport.makeServerBootstrap(group: self.group)
  93. XCTAssertTrue(bootstrap is NIOTSListenerBootstrap)
  94. #endif
  95. }
  96. func testMakeServerBootstrapReturnsNIOTSListenerBootstrapForQoSEventLoop() {
  97. // If we don't have Network.framework then we can't test this.
  98. #if canImport(Network)
  99. guard #available(macOS 10.14, *) else { return }
  100. self.group = NIOTSEventLoopGroup(loopCount: 1)
  101. let eventLoop = self.group.next()
  102. XCTAssertTrue(eventLoop is QoSEventLoop)
  103. let bootstrap = PlatformSupport.makeServerBootstrap(group: eventLoop)
  104. XCTAssertTrue(bootstrap is NIOTSListenerBootstrap)
  105. #endif
  106. }
  107. func testRequiresZeroLengthWorkaroundWithMTELG() {
  108. self.group = MultiThreadedEventLoopGroup(numberOfThreads: 1)
  109. // No MTELG or individual loop requires the workaround.
  110. XCTAssertFalse(
  111. PlatformSupport
  112. .requiresZeroLengthWriteWorkaround(group: self.group, hasTLS: true)
  113. )
  114. XCTAssertFalse(
  115. PlatformSupport
  116. .requiresZeroLengthWriteWorkaround(group: self.group, hasTLS: false)
  117. )
  118. XCTAssertFalse(
  119. PlatformSupport
  120. .requiresZeroLengthWriteWorkaround(group: self.group.next(), hasTLS: true)
  121. )
  122. XCTAssertFalse(
  123. PlatformSupport
  124. .requiresZeroLengthWriteWorkaround(group: self.group.next(), hasTLS: false)
  125. )
  126. }
  127. func testRequiresZeroLengthWorkaroundWithNetworkFramework() {
  128. // If we don't have Network.framework we can't test this.
  129. #if canImport(Network)
  130. guard #available(macOS 10.14, iOS 12.0, tvOS 12.0, watchOS 6.0, *) else { return }
  131. self.group = NIOTSEventLoopGroup(loopCount: 1)
  132. // We require the workaround for any of these loops when TLS is not enabled.
  133. XCTAssertFalse(
  134. PlatformSupport
  135. .requiresZeroLengthWriteWorkaround(group: self.group, hasTLS: true)
  136. )
  137. XCTAssertTrue(
  138. PlatformSupport
  139. .requiresZeroLengthWriteWorkaround(group: self.group, hasTLS: false)
  140. )
  141. XCTAssertFalse(
  142. PlatformSupport
  143. .requiresZeroLengthWriteWorkaround(group: self.group.next(), hasTLS: true)
  144. )
  145. XCTAssertTrue(
  146. PlatformSupport
  147. .requiresZeroLengthWriteWorkaround(group: self.group.next(), hasTLS: false)
  148. )
  149. #endif
  150. }
  151. func testIsTransportServicesGroup() {
  152. #if canImport(Network)
  153. guard #available(macOS 10.14, iOS 12.0, tvOS 12.0, watchOS 6.0, *) else { return }
  154. let tsGroup = NIOTSEventLoopGroup(loopCount: 1)
  155. defer {
  156. XCTAssertNoThrow(try tsGroup.syncShutdownGracefully())
  157. }
  158. XCTAssertTrue(PlatformSupport.isTransportServicesEventLoopGroup(tsGroup))
  159. XCTAssertTrue(PlatformSupport.isTransportServicesEventLoopGroup(tsGroup.next()))
  160. let group = MultiThreadedEventLoopGroup(numberOfThreads: 1)
  161. defer {
  162. XCTAssertNoThrow(try group.syncShutdownGracefully())
  163. }
  164. XCTAssertFalse(PlatformSupport.isTransportServicesEventLoopGroup(group))
  165. XCTAssertFalse(PlatformSupport.isTransportServicesEventLoopGroup(group.next()))
  166. #endif
  167. }
  168. func testIsTLSConfigruationCompatible() {
  169. #if canImport(Network)
  170. guard #available(macOS 10.14, iOS 12.0, tvOS 12.0, watchOS 6.0, *) else { return }
  171. let nwConfiguration = GRPCTLSConfiguration.makeClientConfigurationBackedByNetworkFramework()
  172. let nioSSLConfiguration = GRPCTLSConfiguration.makeClientConfigurationBackedByNIOSSL()
  173. let tsGroup = NIOTSEventLoopGroup(loopCount: 1)
  174. defer {
  175. XCTAssertNoThrow(try tsGroup.syncShutdownGracefully())
  176. }
  177. XCTAssertTrue(tsGroup.isCompatible(with: nwConfiguration))
  178. XCTAssertTrue(tsGroup.isCompatible(with: nioSSLConfiguration))
  179. XCTAssertTrue(tsGroup.next().isCompatible(with: nwConfiguration))
  180. XCTAssertTrue(tsGroup.next().isCompatible(with: nioSSLConfiguration))
  181. let group = MultiThreadedEventLoopGroup(numberOfThreads: 1)
  182. defer {
  183. XCTAssertNoThrow(try group.syncShutdownGracefully())
  184. }
  185. XCTAssertFalse(group.isCompatible(with: nwConfiguration))
  186. XCTAssertTrue(group.isCompatible(with: nioSSLConfiguration))
  187. XCTAssertFalse(group.next().isCompatible(with: nwConfiguration))
  188. XCTAssertTrue(group.next().isCompatible(with: nioSSLConfiguration))
  189. #endif
  190. }
  191. }