Skip to content

Commit d41064e

Browse files
committed
re-use allocator in tests and generate random data
1 parent 7c909ec commit d41064e

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

Tests/ClientRuntimeTests/NetworkingTests/Http/NIO/NIOHTTPClientStreamBridgeTests.swift

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import enum Smithy.ByteStream
1717
@testable import ClientRuntime
1818

1919
class NIOHTTPClientStreamBridgeTests: XCTestCase {
20+
let allocator = ByteBufferAllocator()
2021

2122
func test_convertResponseBody_streamsAllDataCorrectly() async throws {
2223

@@ -44,7 +45,6 @@ class NIOHTTPClientStreamBridgeTests: XCTestCase {
4445
let originalData = Data(bytes: randomBuffer, count: dataSize)
4546

4647
// Create a mock AsyncHTTPClient response with the test data
47-
let allocator = ByteBufferAllocator()
4848
var buffer = allocator.buffer(capacity: originalData.count)
4949
buffer.writeBytes(originalData)
5050

@@ -64,7 +64,6 @@ class NIOHTTPClientStreamBridgeTests: XCTestCase {
6464
}
6565

6666
func test_convertRequestBody_withNoStream() async throws {
67-
let allocator = ByteBufferAllocator()
6867
let byteStream = ByteStream.noStream
6968

7069
let result = try await NIOHTTPClientStreamBridge.convertRequestBody(
@@ -82,7 +81,6 @@ class NIOHTTPClientStreamBridgeTests: XCTestCase {
8281
}
8382

8483
func test_convertRequestBody_withData() async throws {
85-
let allocator = ByteBufferAllocator()
8684
let testData = "Hello, World!".data(using: .utf8)!
8785
let byteStream = ByteStream.data(testData)
8886

@@ -100,8 +98,16 @@ class NIOHTTPClientStreamBridgeTests: XCTestCase {
10098
}
10199

102100
func test_convertRequestBody_withStream() async throws {
103-
let allocator = ByteBufferAllocator()
104-
let testData = Data(repeating: 42, count: 1000)
101+
// Create random test data
102+
let dataSize = 1000
103+
let randomBuffer = UnsafeMutablePointer<UInt8>.allocate(capacity: dataSize)
104+
defer { randomBuffer.deallocate() }
105+
106+
for i in 0..<dataSize {
107+
randomBuffer[i] = UInt8.random(in: UInt8.min...UInt8.max)
108+
}
109+
110+
let testData = Data(bytes: randomBuffer, count: dataSize)
105111
let bufferedStream = BufferedStream(data: testData, isClosed: true)
106112
let byteStream = ByteStream.stream(bufferedStream)
107113

0 commit comments

Comments
 (0)