GRPCMessageFramerTests.swift 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /*
  2. * Copyright 2024, 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 NIOCore
  17. import NIOEmbedded
  18. import XCTest
  19. @testable import GRPCNIOTransportCore
  20. @available(gRPCSwiftNIOTransport 2.0, *)
  21. final class GRPCMessageFramerTests: XCTestCase {
  22. func testSingleWrite() throws {
  23. var framer = GRPCMessageFramer()
  24. framer.append(ByteBuffer(repeating: 42, count: 128), promise: nil)
  25. var buffer = try XCTUnwrap(framer.next()).bytes
  26. let (compressed, length) = try XCTUnwrap(buffer.readMessageHeader())
  27. XCTAssertFalse(compressed)
  28. XCTAssertEqual(length, 128)
  29. XCTAssertEqual(buffer.readSlice(length: Int(length)), ByteBuffer(repeating: 42, count: 128))
  30. XCTAssertEqual(buffer.readableBytes, 0)
  31. // No more bufers.
  32. XCTAssertNil(try framer.next())
  33. }
  34. private func testSingleWrite(compressionMethod: Zlib.Method) throws {
  35. let compressor = Zlib.Compressor(method: compressionMethod)
  36. defer {
  37. compressor.end()
  38. }
  39. var framer = GRPCMessageFramer()
  40. let message = ByteBuffer(repeating: 42, count: 128)
  41. framer.append(message, promise: nil)
  42. var buffer = ByteBuffer()
  43. let testCompressor = Zlib.Compressor(method: compressionMethod)
  44. let compressedSize = try testCompressor.compress(message, into: &buffer)
  45. let compressedMessage = buffer.readSlice(length: compressedSize)
  46. defer {
  47. testCompressor.end()
  48. }
  49. buffer = try XCTUnwrap(framer.next(compressor: compressor)).bytes
  50. let (compressed, length) = try XCTUnwrap(buffer.readMessageHeader())
  51. XCTAssertTrue(compressed)
  52. XCTAssertEqual(length, UInt32(compressedSize))
  53. XCTAssertEqual(buffer.readSlice(length: Int(length)), compressedMessage)
  54. XCTAssertEqual(buffer.readableBytes, 0)
  55. // No more bufers.
  56. XCTAssertNil(try framer.next())
  57. }
  58. func testSingleWriteDeflateCompressed() throws {
  59. try self.testSingleWrite(compressionMethod: .deflate)
  60. }
  61. func testSingleWriteGZIPCompressed() throws {
  62. try self.testSingleWrite(compressionMethod: .gzip)
  63. }
  64. func testMultipleWrites() throws {
  65. var framer = GRPCMessageFramer()
  66. let eventLoop = EmbeddedEventLoop()
  67. // Create 100 messages and link a different promise with each of them.
  68. let messagesCount = 100
  69. var promises = [EventLoopPromise<Void>]()
  70. promises.reserveCapacity(messagesCount)
  71. for _ in 0 ..< messagesCount {
  72. let promise = eventLoop.makePromise(of: Void.self)
  73. promises.append(promise)
  74. framer.append(ByteBuffer(repeating: 42, count: 128), promise: promise)
  75. }
  76. let nextFrame = try XCTUnwrap(framer.next())
  77. // Assert the messages have been framed all together in the same frame.
  78. var buffer = nextFrame.bytes
  79. for _ in 0 ..< messagesCount {
  80. let (compressed, length) = try XCTUnwrap(buffer.readMessageHeader())
  81. XCTAssertFalse(compressed)
  82. XCTAssertEqual(length, 128)
  83. XCTAssertEqual(buffer.readSlice(length: Int(length)), ByteBuffer(repeating: 42, count: 128))
  84. }
  85. XCTAssertEqual(buffer.readableBytes, 0)
  86. // Assert the promise returned from the framer is the promise linked to the
  87. // first message appended to the framer.
  88. let returnedPromise = nextFrame.promise
  89. XCTAssertEqual(returnedPromise?.futureResult, promises.first?.futureResult)
  90. // Succeed the returned promise to simulate a write into the channel
  91. // succeeding, and assert that all other promises have been chained and are
  92. // also succeeded as a result.
  93. returnedPromise?.succeed()
  94. XCTAssertEqual(promises.count, messagesCount)
  95. for promise in promises {
  96. try promise.futureResult.assertSuccess().wait()
  97. }
  98. // No more frames.
  99. XCTAssertNil(try framer.next())
  100. }
  101. }
  102. extension ByteBuffer {
  103. mutating func readMessageHeader() -> (Bool, UInt32)? {
  104. if let (compressed, length) = self.readMultipleIntegers(as: (UInt8, UInt32).self) {
  105. return (compressed != 0, length)
  106. } else {
  107. return nil
  108. }
  109. }
  110. }