ServerHandlerStateMachineTests.swift 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. /*
  2. * Copyright 2022, 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. #if compiler(>=5.6)
  17. @testable import GRPC
  18. import NIOCore
  19. import NIOEmbedded
  20. import NIOHPACK
  21. import XCTest
  22. @available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
  23. internal final class ServerHandlerStateMachineTests: GRPCTestCase {
  24. private enum InitialState {
  25. case idle
  26. case handling
  27. case draining
  28. case finished
  29. }
  30. private func makeStateMachine(inState state: InitialState = .idle) -> ServerHandlerStateMachine {
  31. var stateMachine = ServerHandlerStateMachine(
  32. userInfoRef: Ref(UserInfo()),
  33. context: self.makeCallHandlerContext()
  34. )
  35. switch state {
  36. case .idle:
  37. return stateMachine
  38. case .handling:
  39. stateMachine.handleMetadata().assertInvokeHandler()
  40. stateMachine.handlerInvoked(context: self.makeAsyncServerContext())
  41. return stateMachine
  42. case .draining:
  43. stateMachine.handleMetadata().assertInvokeHandler()
  44. stateMachine.handlerInvoked(context: self.makeAsyncServerContext())
  45. stateMachine.handleEnd().assertForward()
  46. return stateMachine
  47. case .finished:
  48. stateMachine.cancel().assertNone()
  49. return stateMachine
  50. }
  51. }
  52. private func makeCallHandlerContext() -> CallHandlerContext {
  53. let loop = EmbeddedEventLoop()
  54. defer {
  55. try! loop.syncShutdownGracefully()
  56. }
  57. return CallHandlerContext(
  58. logger: self.logger,
  59. encoding: .disabled,
  60. eventLoop: loop,
  61. path: "",
  62. responseWriter: NoOpResponseWriter(),
  63. allocator: ByteBufferAllocator(),
  64. closeFuture: loop.makeSucceededVoidFuture()
  65. )
  66. }
  67. private func makeAsyncServerContext() -> GRPCAsyncServerCallContext {
  68. return GRPCAsyncServerCallContext(
  69. headers: [:],
  70. logger: self.logger,
  71. userInfoRef: Ref(UserInfo())
  72. )
  73. }
  74. // MARK: - Test Cases
  75. func testHandleMetadataWhenIdle() {
  76. var stateMachine = self.makeStateMachine()
  77. // Receiving metadata is the signal to invoke the user handler.
  78. stateMachine.handleMetadata().assertInvokeHandler()
  79. // On invoking the handler we move to the next state. No output.
  80. stateMachine.handlerInvoked(context: self.makeAsyncServerContext())
  81. }
  82. func testHandleMetadataWhenHandling() {
  83. var stateMachine = self.makeStateMachine(inState: .handling)
  84. // Must not receive metadata more than once.
  85. stateMachine.handleMetadata().assertInvokeCancel()
  86. }
  87. func testHandleMetadataWhenDraining() {
  88. var stateMachine = self.makeStateMachine(inState: .draining)
  89. // We can't receive metadata more than once.
  90. stateMachine.handleMetadata().assertInvokeCancel()
  91. }
  92. func testHandleMetadataWhenFinished() {
  93. var stateMachine = self.makeStateMachine(inState: .finished)
  94. // We can't receive anything when finished.
  95. stateMachine.handleMetadata().assertInvokeCancel()
  96. }
  97. func testHandleMessageWhenIdle() {
  98. var stateMachine = self.makeStateMachine()
  99. // Metadata must be received first.
  100. stateMachine.handleMessage().assertCancel()
  101. }
  102. func testHandleMessageWhenHandling() {
  103. var stateMachine = self.makeStateMachine(inState: .handling)
  104. // Messages are good, we can forward those while handling.
  105. for _ in 0 ..< 10 {
  106. stateMachine.handleMessage().assertForward()
  107. }
  108. }
  109. func testHandleMessageWhenDraining() {
  110. var stateMachine = self.makeStateMachine(inState: .draining)
  111. // We entered the 'draining' state as we received 'end', another message is a protocol
  112. // violation so cancel.
  113. stateMachine.handleMessage().assertCancel()
  114. }
  115. func testHandleMessageWhenFinished() {
  116. var stateMachine = self.makeStateMachine(inState: .finished)
  117. // We can't receive anything when finished.
  118. stateMachine.handleMessage().assertCancel()
  119. }
  120. func testHandleEndWhenIdle() {
  121. var stateMachine = self.makeStateMachine()
  122. // Metadata must be received first.
  123. stateMachine.handleEnd().assertCancel()
  124. }
  125. func testHandleEndWhenHandling() {
  126. var stateMachine = self.makeStateMachine(inState: .handling)
  127. // End is good; it transitions us to the draining state.
  128. stateMachine.handleEnd().assertForward()
  129. }
  130. func testHandleEndWhenDraining() {
  131. var stateMachine = self.makeStateMachine(inState: .draining)
  132. // We entered the 'draining' state as we received 'end', another 'end' is a protocol
  133. // violation so cancel.
  134. stateMachine.handleEnd().assertCancel()
  135. }
  136. func testHandleEndWhenFinished() {
  137. var stateMachine = self.makeStateMachine(inState: .finished)
  138. // We can't receive anything when finished.
  139. stateMachine.handleEnd().assertCancel()
  140. }
  141. func testSendMessageWhenHandling() {
  142. var stateMachine = self.makeStateMachine(inState: .handling)
  143. // The first message should prompt headers to be sent as well.
  144. stateMachine.sendMessage().assertInterceptHeadersThenMessage()
  145. // Additional messages should be just the message.
  146. stateMachine.sendMessage().assertInterceptMessage()
  147. }
  148. func testSendMessageWhenDraining() {
  149. var stateMachine = self.makeStateMachine(inState: .draining)
  150. // The first message should prompt headers to be sent as well.
  151. stateMachine.sendMessage().assertInterceptHeadersThenMessage()
  152. // Additional messages should be just the message.
  153. stateMachine.sendMessage().assertInterceptMessage()
  154. }
  155. func testSendMessageWhenFinished() {
  156. var stateMachine = self.makeStateMachine(inState: .finished)
  157. // We can't send anything if we're finished.
  158. stateMachine.sendMessage().assertDrop()
  159. }
  160. func testSendStatusWhenHandling() {
  161. var stateMachine = self.makeStateMachine(inState: .handling)
  162. // This moves the state machine to the 'finished' state.
  163. stateMachine.sendStatus().assertIntercept()
  164. }
  165. func testSendStatusWhenDraining() {
  166. var stateMachine = self.makeStateMachine(inState: .draining)
  167. // This moves the state machine to the 'finished' state.
  168. stateMachine.sendStatus().assertIntercept()
  169. }
  170. func testSendStatusWhenFinished() {
  171. var stateMachine = self.makeStateMachine(inState: .finished)
  172. // We can't send anything if we're finished.
  173. stateMachine.sendStatus().assertDrop()
  174. }
  175. func testCancelWhenIdle() {
  176. var stateMachine = self.makeStateMachine()
  177. // Cancelling when idle is effectively a no-op; there's nothing to cancel.
  178. stateMachine.cancel().assertNone()
  179. }
  180. func testCancelWhenHandling() {
  181. var stateMachine = self.makeStateMachine(inState: .handling)
  182. // We have things to cancel in this state.
  183. stateMachine.cancel().assertDoCancel()
  184. }
  185. func testCancelWhenDraining() {
  186. var stateMachine = self.makeStateMachine(inState: .draining)
  187. // We have things to cancel in this state.
  188. stateMachine.cancel().assertDoCancel()
  189. }
  190. func testCancelWhenFinished() {
  191. var stateMachine = self.makeStateMachine(inState: .finished)
  192. stateMachine.cancel().assertDoCancel()
  193. }
  194. }
  195. // MARK: - Action Assertions
  196. extension ServerHandlerStateMachine.HandleMetadataAction {
  197. func assertInvokeHandler() {
  198. switch self {
  199. case .invokeHandler:
  200. ()
  201. case .cancel:
  202. XCTFail("Expected 'invokeHandler' but got \(self)")
  203. }
  204. }
  205. func assertInvokeCancel() {
  206. switch self {
  207. case .cancel:
  208. ()
  209. case .invokeHandler:
  210. XCTFail("Expected 'cancel' but got \(self)")
  211. }
  212. }
  213. }
  214. extension ServerHandlerStateMachine.HandleMessageAction {
  215. func assertForward() {
  216. XCTAssertEqual(self, .forward)
  217. }
  218. func assertCancel() {
  219. XCTAssertEqual(self, .cancel)
  220. }
  221. }
  222. extension ServerHandlerStateMachine.SendMessageAction {
  223. func assertInterceptHeadersThenMessage() {
  224. XCTAssertEqual(self, .intercept(headers: [:]))
  225. }
  226. func assertInterceptMessage() {
  227. XCTAssertEqual(self, .intercept(headers: nil))
  228. }
  229. func assertDrop() {
  230. XCTAssertEqual(self, .drop)
  231. }
  232. }
  233. extension ServerHandlerStateMachine.SendStatusAction {
  234. func assertIntercept() {
  235. XCTAssertEqual(self, .intercept(trailers: [:]))
  236. }
  237. func assertDrop() {
  238. XCTAssertEqual(self, .drop)
  239. }
  240. }
  241. extension ServerHandlerStateMachine.CancelAction {
  242. func assertNone() {
  243. XCTAssertEqual(self, .none)
  244. }
  245. func assertDoCancel() {
  246. XCTAssertEqual(self, .cancelAndNilOutHandlerComponents)
  247. }
  248. }
  249. #endif // compiler(>=5.6)