InteroperabilityTestCases.swift 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030
  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. import GRPC
  18. import GRPCInteroperabilityTestModels
  19. import NIOHTTP1
  20. import NIOHPACK
  21. /// This test verifies that implementations support zero-size messages. Ideally, client
  22. /// implementations would verify that the request and response were zero bytes serialized, but
  23. /// this is generally prohibitive to perform, so is not required.
  24. ///
  25. /// Server features:
  26. /// - EmptyCall
  27. ///
  28. /// Procedure:
  29. /// 1. Client calls EmptyCall with the default Empty message
  30. ///
  31. /// Client asserts:
  32. /// - call was successful
  33. /// - response is non-null
  34. class EmptyUnary: InteroperabilityTest {
  35. func run(using connection: ClientConnection) throws {
  36. let client = Grpc_Testing_TestServiceClient(channel: connection)
  37. let call = client.emptyCall(Grpc_Testing_Empty())
  38. try waitAndAssertEqual(call.response, Grpc_Testing_Empty())
  39. try waitAndAssertEqual(call.status.map { $0.code }, .ok)
  40. }
  41. }
  42. /// This test verifies that gRPC requests marked as cacheable use GET verb instead of POST, and
  43. /// that server sets appropriate cache control headers for the response to be cached by a proxy.
  44. /// This test requires that the server is behind a caching proxy. Use of current timestamp in the
  45. /// request prevents accidental cache matches left over from previous tests.
  46. ///
  47. /// Server features:
  48. /// - CacheableUnaryCall
  49. ///
  50. /// Procedure:
  51. /// 1. Client calls CacheableUnaryCall with SimpleRequest request with payload set to current
  52. /// timestamp. Timestamp format is irrelevant, and resolution is in nanoseconds. Client adds a
  53. /// x-user-ip header with value 1.2.3.4 to the request. This is done since some proxys such as
  54. /// GFE will not cache requests from localhost. Client marks the request as cacheable by
  55. /// setting the cacheable flag in the request context. Longer term this should be driven by
  56. /// the method option specified in the proto file itself.
  57. /// 2. Client calls CacheableUnaryCall again immediately with the same request and configuration
  58. /// as the previous call.
  59. ///
  60. /// Client asserts:
  61. /// - Both calls were successful
  62. /// - The payload body of both responses is the same.
  63. class CacheableUnary: InteroperabilityTest {
  64. func run(using connection: ClientConnection) throws {
  65. let client = Grpc_Testing_TestServiceClient(channel: connection)
  66. var timestamp = DispatchTime.now().rawValue
  67. let request = Grpc_Testing_SimpleRequest.withPayload(of: .bytes(of: &timestamp))
  68. let headers: HPACKHeaders = ["x-user-ip": "1.2.3.4"]
  69. let callOptions = CallOptions(customMetadata: headers, cacheable: true)
  70. let call1 = client.cacheableUnaryCall(request, callOptions: callOptions)
  71. let call2 = client.cacheableUnaryCall(request, callOptions: callOptions)
  72. // The server ignores the request payload so we must not validate against it.
  73. try waitAndAssertEqual(call1.response.map { $0.payload }, call2.response.map { $0.payload })
  74. try waitAndAssertEqual(call1.status.map { $0.code }, .ok)
  75. try waitAndAssertEqual(call2.status.map { $0.code }, .ok)
  76. }
  77. }
  78. /// This test verifies unary calls succeed in sending messages, and touches on flow control (even
  79. /// if compression is enabled on the channel).
  80. ///
  81. /// Server features:
  82. /// - UnaryCall
  83. ///
  84. /// Procedure:
  85. /// 1. Client calls UnaryCall with:
  86. /// ```
  87. /// {
  88. /// response_size: 314159
  89. /// payload:{
  90. /// body: 271828 bytes of zeros
  91. /// }
  92. /// }
  93. /// ```
  94. ///
  95. /// Client asserts:
  96. /// - call was successful
  97. /// - response payload body is 314159 bytes in size
  98. /// - clients are free to assert that the response payload body contents are zero and comparing
  99. /// the entire response message against a golden response
  100. class LargeUnary: InteroperabilityTest {
  101. func run(using connection: ClientConnection) throws {
  102. let client = Grpc_Testing_TestServiceClient(channel: connection)
  103. let request = Grpc_Testing_SimpleRequest.with { request in
  104. request.responseSize = 314_159
  105. request.payload = .zeros(count: 271_828)
  106. }
  107. let call = client.unaryCall(request)
  108. try waitAndAssertEqual(call.response.map { $0.payload }, .zeros(count: 314_159))
  109. try waitAndAssertEqual(call.status.map { $0.code }, .ok)
  110. }
  111. }
  112. /// This test verifies the client can compress unary messages by sending two unary calls, for
  113. /// compressed and uncompressed payloads. It also sends an initial probing request to verify
  114. /// whether the server supports the CompressedRequest feature by checking if the probing call
  115. /// fails with an `INVALID_ARGUMENT` status.
  116. ///
  117. /// Server features:
  118. /// - UnaryCall
  119. /// - CompressedRequest
  120. ///
  121. /// Procedure:
  122. /// 1. Client calls UnaryCall with the feature probe, an *uncompressed* message:
  123. /// ```
  124. /// {
  125. /// expect_compressed:{
  126. /// value: true
  127. /// }
  128. /// response_size: 314159
  129. /// payload:{
  130. /// body: 271828 bytes of zeros
  131. /// }
  132. /// }
  133. /// ```
  134. /// 2. Client calls UnaryCall with the *compressed* message:
  135. /// ```
  136. /// {
  137. /// expect_compressed:{
  138. /// value: true
  139. /// }
  140. /// response_size: 314159
  141. /// payload:{
  142. /// body: 271828 bytes of zeros
  143. /// }
  144. /// }
  145. /// ```
  146. /// 3. Client calls UnaryCall with the *uncompressed* message:
  147. /// ```
  148. /// {
  149. /// expect_compressed:{
  150. /// value: false
  151. /// }
  152. /// response_size: 314159
  153. /// payload:{
  154. /// body: 271828 bytes of zeros
  155. /// }
  156. /// }
  157. /// ```
  158. ///
  159. /// Client asserts:
  160. /// - First call failed with `INVALID_ARGUMENT` status.
  161. /// - Subsequent calls were successful.
  162. /// - Response payload body is 314159 bytes in size.
  163. /// - Clients are free to assert that the response payload body contents are zeros and comparing the
  164. /// entire response message against a golden response.
  165. class ClientCompressedUnary: InteroperabilityTest {
  166. func run(using connection: ClientConnection) throws {
  167. let client = Grpc_Testing_TestServiceClient(channel: connection)
  168. let compressedRequest = Grpc_Testing_SimpleRequest.with { request in
  169. request.expectCompressed = true
  170. request.responseSize = 314_159
  171. request.payload = .zeros(count: 271_828)
  172. }
  173. var uncompressedRequest = compressedRequest
  174. uncompressedRequest.expectCompressed = false
  175. // For unary RPCs we disable compression at the call level.
  176. // With compression expected but *disabled*.
  177. let probe = client.unaryCall(compressedRequest)
  178. try waitAndAssertEqual(probe.status.map { $0.code }, .invalidArgument)
  179. // With compression expected and enabled.
  180. let options = CallOptions(messageEncoding: .enabled(.init(forRequests: .gzip, decompressionLimit: .absolute(1024 * 1024))))
  181. let compressed = client.unaryCall(compressedRequest, callOptions: options)
  182. try waitAndAssertEqual(compressed.response.map { $0.payload }, .zeros(count: 314_159))
  183. try waitAndAssertEqual(compressed.status.map { $0.code }, .ok)
  184. // With compression not expected and disabled.
  185. let uncompressed = client.unaryCall(uncompressedRequest)
  186. try waitAndAssertEqual(uncompressed.response.map { $0.payload }, .zeros(count: 314_159))
  187. try waitAndAssertEqual(uncompressed.status.map { $0.code }, .ok)
  188. }
  189. }
  190. /// This test verifies the server can compress unary messages. It sends two unary
  191. /// requests, expecting the server's response to be compressed or not according to
  192. /// the `response_compressed` boolean.
  193. ///
  194. /// Whether compression was actually performed is determined by the compression bit
  195. /// in the response's message flags. *Note that some languages may not have access
  196. /// to the message flags, in which case the client will be unable to verify that
  197. /// the `response_compressed` boolean is obeyed by the server*.
  198. ///
  199. ///
  200. /// Server features:
  201. /// - UnaryCall
  202. /// - CompressedResponse
  203. ///
  204. /// Procedure:
  205. /// 1. Client calls UnaryCall with `SimpleRequest`:
  206. /// ```
  207. /// {
  208. /// response_compressed:{
  209. /// value: true
  210. /// }
  211. /// response_size: 314159
  212. /// payload:{
  213. /// body: 271828 bytes of zeros
  214. /// }
  215. /// }
  216. /// ```
  217. /// ```
  218. /// {
  219. /// response_compressed:{
  220. /// value: false
  221. /// }
  222. /// response_size: 314159
  223. /// payload:{
  224. /// body: 271828 bytes of zeros
  225. /// }
  226. /// }
  227. /// ```
  228. ///
  229. /// Client asserts:
  230. /// - call was successful
  231. /// - if supported by the implementation, when `response_compressed` is true, the response MUST have
  232. /// the compressed message flag set.
  233. /// - if supported by the implementation, when `response_compressed` is false, the response MUST NOT
  234. /// have the compressed message flag set.
  235. /// - response payload body is 314159 bytes in size in both cases.
  236. /// - clients are free to assert that the response payload body contents are zero and comparing the
  237. /// entire response message against a golden response
  238. class ServerCompressedUnary: InteroperabilityTest {
  239. func run(using connection: ClientConnection) throws {
  240. let client = Grpc_Testing_TestServiceClient(channel: connection)
  241. let compressedRequest = Grpc_Testing_SimpleRequest.with { request in
  242. request.responseCompressed = true
  243. request.responseSize = 314_159
  244. request.payload = .zeros(count: 271_828)
  245. }
  246. let options = CallOptions(messageEncoding: .enabled(.responsesOnly(decompressionLimit: .absolute(1024 * 1024))))
  247. let compressed = client.unaryCall(compressedRequest, callOptions: options)
  248. // We can't verify that the compression bit was set, instead we verify that the encoding header
  249. // was sent by the server. This isn't quite the same since as it can still be set but the
  250. // compression may be not set.
  251. try waitAndAssert(compressed.initialMetadata) { headers in
  252. return headers.first(name: "grpc-encoding") != nil
  253. }
  254. try waitAndAssertEqual(compressed.response.map { $0.payload }, .zeros(count: 314_159))
  255. try waitAndAssertEqual(compressed.status.map { $0.code }, .ok)
  256. var uncompressedRequest = compressedRequest
  257. uncompressedRequest.responseCompressed.value = false
  258. let uncompressed = client.unaryCall(uncompressedRequest)
  259. // We can't check even check for the 'grpc-encoding' header here since it could be set with the
  260. // compression bit on the message not set.
  261. try waitAndAssertEqual(uncompressed.response.map { $0.payload }, .zeros(count: 314_159))
  262. try waitAndAssertEqual(uncompressed.status.map { $0.code }, .ok)
  263. }
  264. }
  265. /// This test verifies that client-only streaming succeeds.
  266. ///
  267. /// Server features:
  268. /// - StreamingInputCall
  269. ///
  270. /// Procedure:
  271. /// 1. Client calls StreamingInputCall
  272. /// 2. Client sends:
  273. /// ```
  274. /// {
  275. /// payload:{
  276. /// body: 27182 bytes of zeros
  277. /// }
  278. /// }
  279. /// ```
  280. /// 3. Client then sends:
  281. /// ```
  282. /// {
  283. /// payload:{
  284. /// body: 8 bytes of zeros
  285. /// }
  286. /// }
  287. /// ```
  288. /// 4. Client then sends:
  289. /// ```
  290. /// {
  291. /// payload:{
  292. /// body: 1828 bytes of zeros
  293. /// }
  294. /// }
  295. /// ```
  296. /// 5. Client then sends:
  297. /// ```
  298. /// {
  299. /// payload:{
  300. /// body: 45904 bytes of zeros
  301. /// }
  302. /// }
  303. /// ```
  304. /// 6. Client half-closes
  305. ///
  306. /// Client asserts:
  307. /// - call was successful
  308. /// - response aggregated_payload_size is 74922
  309. class ClientStreaming: InteroperabilityTest {
  310. func run(using connection: ClientConnection) throws {
  311. let client = Grpc_Testing_TestServiceClient(channel: connection)
  312. let call = client.streamingInputCall()
  313. let messagesSent = call.newMessageQueue().flatMap {
  314. call.sendMessage(.withPayload(of: .zeros(count: 27_182)))
  315. }.flatMap {
  316. call.sendMessage(.withPayload(of: .zeros(count: 8)))
  317. }.flatMap {
  318. call.sendMessage(.withPayload(of: .zeros(count: 1_828)))
  319. }.flatMap {
  320. call.sendMessage(.withPayload(of: .zeros(count: 45_904)))
  321. }.flatMap {
  322. call.sendEnd()
  323. }
  324. try messagesSent.wait()
  325. try waitAndAssertEqual(call.response.map { $0.aggregatedPayloadSize }, 74_922)
  326. try waitAndAssertEqual(call.status.map { $0.code }, .ok)
  327. }
  328. }
  329. /// This test verifies the client can compress requests on per-message basis by performing a
  330. /// two-request streaming call. It also sends an initial probing request to verify whether the
  331. /// server supports the `CompressedRequest` feature by checking if the probing call fails with
  332. /// an `INVALID_ARGUMENT` status.
  333. ///
  334. /// Procedure:
  335. /// 1. Client calls `StreamingInputCall` and sends the following feature-probing
  336. /// *uncompressed* `StreamingInputCallRequest` message
  337. ///
  338. /// ```
  339. /// {
  340. /// expect_compressed:{
  341. /// value: true
  342. /// }
  343. /// payload:{
  344. /// body: 27182 bytes of zeros
  345. /// }
  346. /// }
  347. /// ```
  348. /// If the call does not fail with `INVALID_ARGUMENT`, the test fails.
  349. /// Otherwise, we continue.
  350. ///
  351. /// 2. Client calls `StreamingInputCall` again, sending the *compressed* message
  352. ///
  353. /// ```
  354. /// {
  355. /// expect_compressed:{
  356. /// value: true
  357. /// }
  358. /// payload:{
  359. /// body: 27182 bytes of zeros
  360. /// }
  361. /// }
  362. /// ```
  363. ///
  364. /// 3. And finally, the *uncompressed* message
  365. /// ```
  366. /// {
  367. /// expect_compressed:{
  368. /// value: false
  369. /// }
  370. /// payload:{
  371. /// body: 45904 bytes of zeros
  372. /// }
  373. /// }
  374. /// ```
  375. ///
  376. /// 4. Client half-closes
  377. ///
  378. /// Client asserts:
  379. /// - First call fails with `INVALID_ARGUMENT`.
  380. /// - Next calls succeeds.
  381. /// - Response aggregated payload size is 73086.
  382. class ClientCompressedStreaming: InteroperabilityTest {
  383. func run(using connection: ClientConnection) throws {
  384. let client = Grpc_Testing_TestServiceClient(channel: connection)
  385. // Does the server support this test? To find out we need to send an uncompressed probe. However
  386. // we need to disable compression at the RPC level as we don't have access to whether the
  387. // compression byte is set on messages. As such the corresponding code in the service
  388. // implementation checks against the 'grpc-encoding' header as a best guess. Disabling
  389. // compression here will stop that header from being sent.
  390. let probe = client.streamingInputCall()
  391. let probeRequest: Grpc_Testing_StreamingInputCallRequest = .with { request in
  392. request.expectCompressed = true
  393. request.payload = .zeros(count: 27_182)
  394. }
  395. // Compression is disabled at the RPC level.
  396. probe.sendMessage(probeRequest, promise: nil)
  397. probe.sendEnd(promise: nil)
  398. // We *expect* invalid argument here. If not then the server doesn't support this test.
  399. try waitAndAssertEqual(probe.status.map { $0.code }, .invalidArgument)
  400. // Now for the actual test.
  401. // The first message is identical to the probe message, we'll reuse that.
  402. // The second should not be compressed.
  403. let secondMessage: Grpc_Testing_StreamingInputCallRequest = .with { request in
  404. request.expectCompressed = false
  405. request.payload = .zeros(count: 45_904)
  406. }
  407. let options = CallOptions(messageEncoding: .enabled(.init(forRequests: .gzip, decompressionLimit: .ratio(10))))
  408. let streaming = client.streamingInputCall(callOptions: options)
  409. streaming.sendMessage(probeRequest, compression: .enabled, promise: nil)
  410. streaming.sendMessage(secondMessage, compression: .disabled, promise: nil)
  411. streaming.sendEnd(promise: nil)
  412. try waitAndAssertEqual(streaming.response.map { $0.aggregatedPayloadSize }, 73_086)
  413. try waitAndAssertEqual(streaming.status.map { $0.code }, .ok)
  414. }
  415. }
  416. /// This test verifies that server-only streaming succeeds.
  417. ///
  418. /// Server features:
  419. /// - StreamingOutputCall
  420. ///
  421. /// Procedure:
  422. /// 1. Client calls StreamingOutputCall with StreamingOutputCallRequest:
  423. /// ```
  424. /// {
  425. /// response_parameters:{
  426. /// size: 31415
  427. /// }
  428. /// response_parameters:{
  429. /// size: 9
  430. /// }
  431. /// response_parameters:{
  432. /// size: 2653
  433. /// }
  434. /// response_parameters:{
  435. /// size: 58979
  436. /// }
  437. /// }
  438. /// ```
  439. ///
  440. /// Client asserts:
  441. /// - call was successful
  442. /// - exactly four responses
  443. /// - response payload bodies are sized (in order): 31415, 9, 2653, 58979
  444. /// - clients are free to assert that the response payload body contents are zero and
  445. /// comparing the entire response messages against golden responses
  446. class ServerStreaming: InteroperabilityTest {
  447. func run(using connection: ClientConnection) throws {
  448. let client = Grpc_Testing_TestServiceClient(channel: connection)
  449. let responseSizes = [31_415, 9, 2_653, 58_979]
  450. let request = Grpc_Testing_StreamingOutputCallRequest.with { request in
  451. request.responseParameters = responseSizes.map { .size($0) }
  452. }
  453. var payloads: [Grpc_Testing_Payload] = []
  454. let call = client.streamingOutputCall(request) { response in
  455. payloads.append(response.payload)
  456. }
  457. // Wait for the status first to ensure we've finished collecting responses.
  458. try waitAndAssertEqual(call.status.map { $0.code }, .ok)
  459. try assertEqual(payloads, responseSizes.map { .zeros(count: $0) })
  460. }
  461. }
  462. /// This test verifies that the server can compress streaming messages and disable compression on
  463. /// individual messages, expecting the server's response to be compressed or not according to the
  464. /// `response_compressed` boolean.
  465. ///
  466. /// Whether compression was actually performed is determined by the compression bit in the
  467. /// response's message flags. *Note that some languages may not have access to the message flags, in
  468. /// which case the client will be unable to verify that the `response_compressed` boolean is obeyed
  469. /// by the server*.
  470. ///
  471. /// Server features:
  472. /// - StreamingOutputCall
  473. /// - CompressedResponse
  474. ///
  475. /// Procedure:
  476. /// 1. Client calls StreamingOutputCall with `StreamingOutputCallRequest`:
  477. /// ```
  478. /// {
  479. /// response_parameters:{
  480. /// compressed: {
  481. /// value: true
  482. /// }
  483. /// size: 31415
  484. /// }
  485. /// response_parameters:{
  486. /// compressed: {
  487. /// value: false
  488. /// }
  489. /// size: 92653
  490. /// }
  491. /// }
  492. /// ```
  493. ///
  494. /// Client asserts:
  495. /// - call was successful
  496. /// - exactly two responses
  497. /// - if supported by the implementation, when `response_compressed` is false, the response's
  498. /// messages MUST NOT have the compressed message flag set.
  499. /// - if supported by the implementation, when `response_compressed` is true, the response's
  500. /// messages MUST have the compressed message flag set.
  501. /// - response payload bodies are sized (in order): 31415, 92653
  502. /// - clients are free to assert that the response payload body contents are zero and comparing the
  503. /// entire response messages against golden responses
  504. class ServerCompressedStreaming: InteroperabilityTest {
  505. func run(using connection: ClientConnection) throws {
  506. let client = Grpc_Testing_TestServiceClient(channel: connection)
  507. let request: Grpc_Testing_StreamingOutputCallRequest = .with { request in
  508. request.responseParameters = [
  509. .with {
  510. $0.compressed = true
  511. $0.size = 31_415
  512. },
  513. .with {
  514. $0.compressed = false
  515. $0.size = 92_653
  516. }
  517. ]
  518. }
  519. let options = CallOptions(messageEncoding: .enabled(.responsesOnly(decompressionLimit: .absolute(1024 * 1024))))
  520. var payloads: [Grpc_Testing_Payload] = []
  521. let rpc = client.streamingOutputCall(request, callOptions: options) { response in
  522. payloads.append(response.payload)
  523. }
  524. // We can't verify that the compression bit was set, instead we verify that the encoding header
  525. // was sent by the server. This isn't quite the same since as it can still be set but the
  526. // compression may be not set.
  527. try waitAndAssert(rpc.initialMetadata) { headers in
  528. return headers.first(name: "grpc-encoding") != nil
  529. }
  530. let responseSizes = [31_415, 92_653]
  531. // Wait for the status first to ensure we've finished collecting responses.
  532. try waitAndAssertEqual(rpc.status.map { $0.code }, .ok)
  533. try assertEqual(payloads, responseSizes.map { .zeros(count: $0) })
  534. }
  535. }
  536. /// This test verifies that full duplex bidi is supported.
  537. ///
  538. /// Server features:
  539. /// - FullDuplexCall
  540. ///
  541. /// Procedure:
  542. /// 1. Client calls FullDuplexCall with:
  543. /// ```
  544. /// {
  545. /// response_parameters:{
  546. /// size: 31415
  547. /// }
  548. /// payload:{
  549. /// body: 27182 bytes of zeros
  550. /// }
  551. /// }
  552. /// ```
  553. /// 2. After getting a reply, it sends:
  554. /// ```
  555. /// {
  556. /// response_parameters:{
  557. /// size: 9
  558. /// }
  559. /// payload:{
  560. /// body: 8 bytes of zeros
  561. /// }
  562. /// }
  563. /// ```
  564. /// 3. After getting a reply, it sends:
  565. /// ```
  566. /// {
  567. /// response_parameters:{
  568. /// size: 2653
  569. /// }
  570. /// payload:{
  571. /// body: 1828 bytes of zeros
  572. /// }
  573. /// }
  574. /// ```
  575. /// 4. After getting a reply, it sends:
  576. /// ```
  577. /// {
  578. /// response_parameters:{
  579. /// size: 58979
  580. /// }
  581. /// payload:{
  582. /// body: 45904 bytes of zeros
  583. /// }
  584. /// }
  585. /// ```
  586. /// 5. After getting a reply, client half-closes
  587. ///
  588. /// Client asserts:
  589. /// - call was successful
  590. /// - exactly four responses
  591. /// - response payload bodies are sized (in order): 31415, 9, 2653, 58979
  592. /// - clients are free to assert that the response payload body contents are zero and
  593. /// comparing the entire response messages against golden responses
  594. class PingPong: InteroperabilityTest {
  595. func run(using connection: ClientConnection) throws {
  596. let client = Grpc_Testing_TestServiceClient(channel: connection)
  597. let requestSizes = [27_182, 8, 1_828, 45_904]
  598. let responseSizes = [31_415, 9, 2_653, 58_979]
  599. let responseReceived = DispatchSemaphore(value: 0)
  600. var payloads: [Grpc_Testing_Payload] = []
  601. let call = client.fullDuplexCall { response in
  602. payloads.append(response.payload)
  603. responseReceived.signal()
  604. }
  605. try zip(requestSizes, responseSizes).map { requestSize, responseSize in
  606. Grpc_Testing_StreamingOutputCallRequest.with { request in
  607. request.payload = .zeros(count: requestSize)
  608. request.responseParameters = [.size(responseSize)]
  609. }
  610. }.forEach { request in
  611. call.sendMessage(request, promise: nil)
  612. try assertEqual(responseReceived.wait(timeout: .now() + .seconds(1)), .success)
  613. }
  614. call.sendEnd(promise: nil)
  615. try waitAndAssertEqual(call.status.map { $0.code }, .ok)
  616. try assertEqual(payloads, responseSizes.map { .zeros(count: $0) })
  617. }
  618. }
  619. /// This test verifies that streams support having zero-messages in both directions.
  620. ///
  621. /// Server features:
  622. /// - FullDuplexCall
  623. ///
  624. /// Procedure:
  625. /// 1. Client calls FullDuplexCall and then half-closes
  626. ///
  627. /// Client asserts:
  628. /// - call was successful
  629. /// - exactly zero responses
  630. class EmptyStream: InteroperabilityTest {
  631. func run(using connection: ClientConnection) throws {
  632. let client = Grpc_Testing_TestServiceClient(channel: connection)
  633. var responses: [Grpc_Testing_StreamingOutputCallResponse] = []
  634. let call = client.fullDuplexCall { response in
  635. responses.append(response)
  636. }
  637. try call.sendEnd().wait()
  638. try waitAndAssertEqual(call.status.map { $0.code }, .ok)
  639. try assertEqual(responses, [])
  640. }
  641. }
  642. /// This test verifies that custom metadata in either binary or ascii format can be sent as
  643. /// initial-metadata by the client and as both initial- and trailing-metadata by the server.
  644. ///
  645. /// Server features:
  646. /// - UnaryCall
  647. /// - FullDuplexCall
  648. /// - Echo Metadata
  649. ///
  650. /// Procedure:
  651. /// 1. The client attaches custom metadata with the following keys and values
  652. /// to a UnaryCall with request:
  653. /// - key: "x-grpc-test-echo-initial", value: "test_initial_metadata_value"
  654. /// - key: "x-grpc-test-echo-trailing-bin", value: 0xababab
  655. /// ```
  656. /// {
  657. /// response_size: 314159
  658. /// payload:{
  659. /// body: 271828 bytes of zeros
  660. /// }
  661. /// }
  662. /// ```
  663. /// 2. The client attaches custom metadata with the following keys and values
  664. /// to a FullDuplexCall with request:
  665. /// - key: "x-grpc-test-echo-initial", value: "test_initial_metadata_value"
  666. /// - key: "x-grpc-test-echo-trailing-bin", value: 0xababab
  667. /// ```
  668. /// {
  669. /// response_parameters:{
  670. /// size: 314159
  671. /// }
  672. /// payload:{
  673. /// body: 271828 bytes of zeros
  674. /// }
  675. /// }
  676. /// ```
  677. /// and then half-closes
  678. ///
  679. /// Client asserts:
  680. /// - call was successful
  681. /// - metadata with key "x-grpc-test-echo-initial" and value "test_initial_metadata_value" is
  682. /// received in the initial metadata for calls in Procedure steps 1 and 2.
  683. /// - metadata with key "x-grpc-test-echo-trailing-bin" and value 0xababab is received in the
  684. /// trailing metadata for calls in Procedure steps 1 and 2.
  685. class CustomMetadata: InteroperabilityTest {
  686. let initialMetadataName = "x-grpc-test-echo-initial"
  687. let initialMetadataValue = "test_initial_metadata_value"
  688. let trailingMetadataName = "x-grpc-test-echo-trailing-bin"
  689. let trailingMetadataValue = Data([0xab, 0xab, 0xab]).base64EncodedString()
  690. func checkMetadata<SpecificClientCall>(call: SpecificClientCall) throws where SpecificClientCall: ClientCall {
  691. let initialName = call.initialMetadata.map { $0[self.initialMetadataName] }
  692. try waitAndAssertEqual(initialName, [self.initialMetadataValue])
  693. let trailingName = call.trailingMetadata.map { $0[self.trailingMetadataName] }
  694. try waitAndAssertEqual(trailingName, [self.trailingMetadataValue])
  695. try waitAndAssertEqual(call.status.map { $0.code }, .ok)
  696. }
  697. func run(using connection: ClientConnection) throws {
  698. let client = Grpc_Testing_TestServiceClient(channel: connection)
  699. let unaryRequest = Grpc_Testing_SimpleRequest.with { request in
  700. request.responseSize = 314_159
  701. request.payload = .zeros(count: 217_828)
  702. }
  703. let customMetadata: HPACKHeaders = [
  704. self.initialMetadataName: self.initialMetadataValue,
  705. self.trailingMetadataName: self.trailingMetadataValue
  706. ]
  707. let callOptions = CallOptions(customMetadata: customMetadata)
  708. let unaryCall = client.unaryCall(unaryRequest, callOptions: callOptions)
  709. try self.checkMetadata(call: unaryCall)
  710. let duplexCall = client.fullDuplexCall(callOptions: callOptions) { _ in }
  711. let duplexRequest = Grpc_Testing_StreamingOutputCallRequest.with { request in
  712. request.responseParameters = [.size(314_159)]
  713. request.payload = .zeros(count: 271_828)
  714. }
  715. let messagesSent = duplexCall.newMessageQueue().flatMap {
  716. duplexCall.sendMessage(duplexRequest)
  717. }.flatMap {
  718. duplexCall.sendEnd()
  719. }
  720. try messagesSent.wait()
  721. try self.checkMetadata(call: duplexCall)
  722. }
  723. }
  724. /// This test verifies unary calls succeed in sending messages, and propagate back status code and
  725. /// message sent along with the messages.
  726. ///
  727. /// Server features:
  728. /// - UnaryCall
  729. /// - FullDuplexCall
  730. /// - Echo Status
  731. ///
  732. /// Procedure:
  733. /// 1. Client calls UnaryCall with:
  734. /// ```
  735. /// {
  736. /// response_status:{
  737. /// code: 2
  738. /// message: "test status message"
  739. /// }
  740. /// }
  741. /// ```
  742. /// 2. Client calls FullDuplexCall with:
  743. /// ```
  744. /// {
  745. /// response_status:{
  746. /// code: 2
  747. /// message: "test status message"
  748. /// }
  749. /// }
  750. /// ```
  751. /// 3. and then half-closes
  752. ///
  753. /// Client asserts:
  754. /// - received status code is the same as the sent code for both Procedure steps 1 and 2
  755. /// - received status message is the same as the sent message for both Procedure steps 1 and 2
  756. class StatusCodeAndMessage: InteroperabilityTest {
  757. let expectedCode = 2
  758. let expectedMessage = "test status message"
  759. func checkStatus<SpecificClientCall>(call: SpecificClientCall) throws where SpecificClientCall: ClientCall {
  760. try waitAndAssertEqual(call.status.map { $0.code.rawValue }, self.expectedCode)
  761. try waitAndAssertEqual(call.status.map { $0.message }, self.expectedMessage)
  762. }
  763. func run(using connection: ClientConnection) throws {
  764. let client = Grpc_Testing_TestServiceClient(channel: connection)
  765. let echoStatus = Grpc_Testing_EchoStatus(code: Int32(self.expectedCode), message: self.expectedMessage)
  766. let unaryCall = client.unaryCall(.withStatus(of: echoStatus))
  767. try self.checkStatus(call: unaryCall)
  768. var responses: [Grpc_Testing_StreamingOutputCallResponse] = []
  769. let duplexCall = client.fullDuplexCall { response in
  770. responses.append(response)
  771. }
  772. try duplexCall.newMessageQueue().flatMap {
  773. duplexCall.sendMessage(.withStatus(of: echoStatus))
  774. }.wait()
  775. try self.checkStatus(call: duplexCall)
  776. try assertEqual(responses, [])
  777. }
  778. }
  779. /// This test verifies Unicode and whitespace is correctly processed in status message. "\t" is
  780. /// horizontal tab. "\r" is carriage return. "\n" is line feed.
  781. ///
  782. /// Server features:
  783. /// - UnaryCall
  784. /// - Echo Status
  785. ///
  786. /// Procedure:
  787. /// 1. Client calls UnaryCall with:
  788. /// ```
  789. /// {
  790. /// response_status:{
  791. /// code: 2
  792. /// message: "\t\ntest with whitespace\r\nand Unicode BMP ☺ and non-BMP 😈\t\n"
  793. /// }
  794. /// }
  795. /// ```
  796. ///
  797. /// Client asserts:
  798. /// - received status code is the same as the sent code for Procedure step 1
  799. /// - received status message is the same as the sent message for Procedure step 1, including all
  800. /// whitespace characters
  801. class SpecialStatusMessage: InteroperabilityTest {
  802. func run(using connection: ClientConnection) throws {
  803. let client = Grpc_Testing_TestServiceClient(channel: connection)
  804. let code = 2
  805. let message = "\t\ntest with whitespace\r\nand Unicode BMP ☺ and non-BMP 😈\t\n"
  806. let call = client.unaryCall(.withStatus(of: .init(code: Int32(code), message: message)))
  807. try waitAndAssertEqual(call.status.map { $0.code.rawValue }, code)
  808. try waitAndAssertEqual(call.status.map { $0.message }, message)
  809. }
  810. }
  811. /// This test verifies that calling an unimplemented RPC method returns the UNIMPLEMENTED status
  812. /// code.
  813. ///
  814. /// Server features: N/A
  815. ///
  816. /// Procedure:
  817. /// 1. Client calls grpc.testing.TestService/UnimplementedCall with an empty request (defined as
  818. /// grpc.testing.Empty):
  819. /// ```
  820. /// {
  821. /// }
  822. /// ```
  823. ///
  824. /// Client asserts:
  825. /// - received status code is 12 (UNIMPLEMENTED)
  826. class UnimplementedMethod: InteroperabilityTest {
  827. func run(using connection: ClientConnection) throws {
  828. let client = Grpc_Testing_TestServiceClient(channel: connection)
  829. let call = client.unimplementedCall(Grpc_Testing_Empty())
  830. try waitAndAssertEqual(call.status.map { $0.code }, .unimplemented)
  831. }
  832. }
  833. /// This test verifies calling an unimplemented server returns the UNIMPLEMENTED status code.
  834. ///
  835. /// Server features: N/A
  836. ///
  837. /// Procedure:
  838. /// 1. Client calls grpc.testing.UnimplementedService/UnimplementedCall with an empty request
  839. /// (defined as grpc.testing.Empty):
  840. /// ```
  841. /// {
  842. /// }
  843. /// ```
  844. ///
  845. /// Client asserts:
  846. /// - received status code is 12 (UNIMPLEMENTED)
  847. class UnimplementedService: InteroperabilityTest {
  848. func run(using connection: ClientConnection) throws {
  849. let client = Grpc_Testing_UnimplementedServiceClient(channel: connection)
  850. let call = client.unimplementedCall(Grpc_Testing_Empty())
  851. try waitAndAssertEqual(call.status.map { $0.code }, .unimplemented)
  852. }
  853. }
  854. /// This test verifies that a request can be cancelled after metadata has been sent but before
  855. /// payloads are sent.
  856. ///
  857. /// Server features:
  858. /// - StreamingInputCall
  859. ///
  860. /// Procedure:
  861. /// 1. Client starts StreamingInputCall
  862. /// 2. Client immediately cancels request
  863. ///
  864. /// Client asserts:
  865. /// - Call completed with status CANCELLED
  866. class CancelAfterBegin: InteroperabilityTest {
  867. func run(using connection: ClientConnection) throws {
  868. let client = Grpc_Testing_TestServiceClient(channel: connection)
  869. let call = client.streamingInputCall()
  870. call.cancel(promise: nil)
  871. try waitAndAssertEqual(call.status.map { $0.code }, .cancelled)
  872. }
  873. }
  874. /// This test verifies that a request can be cancelled after receiving a message from the server.
  875. ///
  876. /// Server features:
  877. /// - FullDuplexCall
  878. ///
  879. /// Procedure:
  880. /// 1. Client starts FullDuplexCall with
  881. /// ```
  882. /// {
  883. /// response_parameters:{
  884. /// size: 31415
  885. /// }
  886. /// payload:{
  887. /// body: 27182 bytes of zeros
  888. /// }
  889. /// }
  890. /// ```
  891. /// 2. After receiving a response, client cancels request
  892. ///
  893. /// Client asserts:
  894. /// - Call completed with status CANCELLED
  895. class CancelAfterFirstResponse: InteroperabilityTest {
  896. func run(using connection: ClientConnection) throws {
  897. let client = Grpc_Testing_TestServiceClient(channel: connection)
  898. let promise = connection.eventLoop.makePromise(of: Void.self)
  899. let call = client.fullDuplexCall { _ in
  900. promise.succeed(())
  901. }
  902. promise.futureResult.whenSuccess {
  903. call.cancel(promise: nil)
  904. }
  905. let request = Grpc_Testing_StreamingOutputCallRequest.with { request in
  906. request.responseParameters = [.size(31_415)]
  907. request.payload = .zeros(count: 27_182)
  908. }
  909. call.sendMessage(request, promise: nil)
  910. try waitAndAssertEqual(call.status.map { $0.code }, .cancelled)
  911. }
  912. }
  913. /// This test verifies that an RPC request whose lifetime exceeds its configured timeout value
  914. /// will end with the DeadlineExceeded status.
  915. ///
  916. /// Server features:
  917. /// - FullDuplexCall
  918. ///
  919. /// Procedure:
  920. /// 1. Client calls FullDuplexCall with the following request and sets its timeout to 1ms
  921. /// ```
  922. /// {
  923. /// payload:{
  924. /// body: 27182 bytes of zeros
  925. /// }
  926. /// }
  927. /// ```
  928. /// 2. Client waits
  929. ///
  930. /// Client asserts:
  931. /// - Call completed with status DEADLINE_EXCEEDED.
  932. class TimeoutOnSleepingServer: InteroperabilityTest {
  933. func run(using connection: ClientConnection) throws {
  934. let client = Grpc_Testing_TestServiceClient(channel: connection)
  935. let callOptions = CallOptions(timeout: try .milliseconds(1))
  936. let call = client.fullDuplexCall(callOptions: callOptions) { _ in }
  937. try waitAndAssertEqual(call.status.map { $0.code }, .deadlineExceeded)
  938. }
  939. }