@@ -55,49 +55,50 @@ struct NIODecodedAsyncSequenceTests {
5555
5656 @Test ( arguments: Self . testingArguments)
5757 func decodingWorks( elementCount: Int , chunkSize: Int ) async throws {
58- let baseSequence = AsyncStream< ByteBuffer> . makeStream( )
59-
60- var randomElements : [ UInt8 ] = ( 0 ..< elementCount) . map {
61- _ in UInt8 . random ( in: . min ... . max)
58+ var randomElements : [ Int32 ] = ( 0 ..< elementCount) . map {
59+ _ in Int32 . random ( in: . min ... . max)
6260 }
6361
64- let buffers =
65- randomElements
66- . chunks ( ofSize: chunkSize)
67- . map ( ByteBuffer . init ( bytes: ) )
62+ var buffer = ByteBuffer ( )
63+ buffer. reserveCapacity ( minimumWritableBytes: elementCount)
64+ for element in randomElements {
65+ buffer. writeInteger ( element)
66+ }
6867
69- for buffer in buffers {
70- baseSequence. continuation. yield ( buffer)
68+ let baseSequence = AsyncStream< ByteBuffer> . makeStream( )
69+ while buffer. readableBytes > 0 {
70+ let length = min ( buffer. readableBytes, chunkSize)
71+ let _slice = buffer. readSlice ( length: length)
72+ let slice = try #require( _slice)
73+ baseSequence. continuation. yield ( slice)
7174 }
7275 baseSequence. continuation. finish ( )
7376
7477 let decodedSequence = baseSequence. stream. decode ( using: ByteToInt32Decoder ( ) )
7578
7679 for try await element in decodedSequence {
77- // Create an Int32 from the first 4 UInt8s
78- let int32 = randomElements [ 0 ..< 4 ] . enumerated ( ) . reduce ( into: Int32 ( 0 ) ) { result, next in
79- result |= Int32 ( next. element) << ( ( 3 - next. offset) * 8 )
80- }
81- randomElements = Array ( randomElements [ 4 ... ] )
82- #expect( element == int32)
80+ #expect( element == randomElements. removeFirst ( ) )
8381 }
8482 }
8583
8684 @Test ( arguments: Self . testingArguments)
8785 func decodingThrowsWhenDecoderThrows( elementCount: Int , chunkSize: Int ) async throws {
88- let baseSequence = AsyncStream< ByteBuffer> . makeStream( )
89-
90- let randomElements : [ UInt8 ] = ( 0 ..< elementCount) . map {
91- _ in UInt8 . random ( in: . min ... . max)
86+ let randomElements : [ Int32 ] = ( 0 ..< elementCount) . map {
87+ _ in Int32 . random ( in: . min ... . max)
9288 }
9389
94- let buffers =
95- randomElements
96- . chunks ( ofSize: chunkSize)
97- . map ( ByteBuffer . init ( bytes: ) )
90+ var buffer = ByteBuffer ( )
91+ buffer. reserveCapacity ( minimumWritableBytes: elementCount)
92+ for element in randomElements {
93+ buffer. writeInteger ( element)
94+ }
9895
99- for buffer in buffers {
100- baseSequence. continuation. yield ( buffer)
96+ let baseSequence = AsyncStream< ByteBuffer> . makeStream( )
97+ while buffer. readableBytes > 0 {
98+ let length = min ( buffer. readableBytes, chunkSize)
99+ let _slice = buffer. readSlice ( length: length)
100+ let slice = try #require( _slice)
101+ baseSequence. continuation. yield ( slice)
101102 }
102103 baseSequence. continuation. finish ( )
103104
@@ -114,9 +115,6 @@ struct NIODecodedAsyncSequenceTests {
114115 struct StreamError : Error { }
115116
116117 let baseSequence = AsyncThrowingStream < ByteBuffer , any Error > . makeStream ( )
117-
118- /// Sleep for 50ms to simulate asynchronous work
119- try await Task . sleep ( nanoseconds: 50_000_000 )
120118 baseSequence. continuation. finish ( throwing: StreamError ( ) )
121119
122120 let decodedSequence = baseSequence. stream. decode ( using: ByteToInt32Decoder ( ) )
@@ -128,11 +126,3 @@ struct NIODecodedAsyncSequenceTests {
128126 }
129127 }
130128}
131-
132- extension Array {
133- fileprivate func chunks( ofSize size: Int ) -> [ [ Element ] ] {
134- stride ( from: 0 , to: self . count, by: size) . map {
135- Array ( self [ $0..< Swift . min ( $0 + size, self . count) ] )
136- }
137- }
138- }
0 commit comments