@@ -26,136 +26,134 @@ fileprivate struct TestCase {
2626}
2727
2828final class AsyncSequenceCollectTests : XCTestCase {
29- func testAsyncSequenceCollect( ) {
29+ func testAsyncSequenceCollect( ) async throws {
3030 guard #available( macOS 10 . 15 , iOS 13 . 0 , watchOS 6 . 0 , tvOS 13 . 0 , * ) else { return }
31- XCTAsyncTest ( timeout: 5 ) {
32- let testCases = [
33- TestCase ( [
34- [ ] ,
35- ] ) ,
36- TestCase ( [
37- [ ] ,
38- [ ] ,
39- ] ) ,
40- TestCase ( [
41- [ 0 ] ,
42- [ ] ,
43- ] ) ,
44- TestCase ( [
45- [ ] ,
46- [ 0 ] ,
47- ] ) ,
48- TestCase ( [
49- [ 0 ] ,
50- [ 1 ] ,
51- ] ) ,
52- TestCase ( [
53- [ 0 ] ,
54- [ 1 ] ,
55- ] ) ,
56- TestCase ( [
57- [ 0 ] ,
58- [ 1 ] ,
59- [ 2 ] ,
60- ] ) ,
61- TestCase ( [
62- [ ] ,
63- [ 0 ] ,
64- [ ] ,
65- [ 1 ] ,
66- [ ] ,
67- [ 2 ] ,
68- [ ] ,
69- ] ) ,
70- TestCase ( [
71- [ 0 ] ,
72- [ 1 ] ,
73- [ 2 ] ,
74- [ ] ,
75- [ ] ,
76- ] ) ,
77- TestCase ( [
78- Array ( 0 ..< 10 ) ,
79- ] ) ,
80- TestCase ( [
81- Array ( 0 ..< 10 ) ,
82- Array ( 10 ..< 20 ) ,
83- ] ) ,
84- TestCase ( [
85- Array ( 0 ..< 10 ) ,
86- Array ( 10 ..< 20 ) ,
87- Array ( 20 ..< 30 ) ,
88- ] ) ,
89- TestCase ( [
90- Array ( 0 ..< 10 ) ,
91- Array ( 10 ..< 20 ) ,
92- Array ( 20 ..< 30 ) ,
93- Array ( repeating: 99 , count: 1000 ) ,
94- ] ) ,
95- ]
96- for testCase in testCases {
97- let expectedBytes = testCase. buffers. flatMap ( { $0 } )
98-
99- // happy case where maxBytes is exactly the same as number of buffers received
100-
101- // test for the generic version
102- let accumulatedBytes1 = try await testCase. buffers
31+ let testCases = [
32+ TestCase ( [
33+ [ ] ,
34+ ] ) ,
35+ TestCase ( [
36+ [ ] ,
37+ [ ] ,
38+ ] ) ,
39+ TestCase ( [
40+ [ 0 ] ,
41+ [ ] ,
42+ ] ) ,
43+ TestCase ( [
44+ [ ] ,
45+ [ 0 ] ,
46+ ] ) ,
47+ TestCase ( [
48+ [ 0 ] ,
49+ [ 1 ] ,
50+ ] ) ,
51+ TestCase ( [
52+ [ 0 ] ,
53+ [ 1 ] ,
54+ ] ) ,
55+ TestCase ( [
56+ [ 0 ] ,
57+ [ 1 ] ,
58+ [ 2 ] ,
59+ ] ) ,
60+ TestCase ( [
61+ [ ] ,
62+ [ 0 ] ,
63+ [ ] ,
64+ [ 1 ] ,
65+ [ ] ,
66+ [ 2 ] ,
67+ [ ] ,
68+ ] ) ,
69+ TestCase ( [
70+ [ 0 ] ,
71+ [ 1 ] ,
72+ [ 2 ] ,
73+ [ ] ,
74+ [ ] ,
75+ ] ) ,
76+ TestCase ( [
77+ Array ( 0 ..< 10 ) ,
78+ ] ) ,
79+ TestCase ( [
80+ Array ( 0 ..< 10 ) ,
81+ Array ( 10 ..< 20 ) ,
82+ ] ) ,
83+ TestCase ( [
84+ Array ( 0 ..< 10 ) ,
85+ Array ( 10 ..< 20 ) ,
86+ Array ( 20 ..< 30 ) ,
87+ ] ) ,
88+ TestCase ( [
89+ Array ( 0 ..< 10 ) ,
90+ Array ( 10 ..< 20 ) ,
91+ Array ( 20 ..< 30 ) ,
92+ Array ( repeating: 99 , count: 1000 ) ,
93+ ] ) ,
94+ ]
95+ for testCase in testCases {
96+ let expectedBytes = testCase. buffers. flatMap ( { $0 } )
97+
98+ // happy case where maxBytes is exactly the same as number of buffers received
99+
100+ // test for the generic version
101+ let accumulatedBytes1 = try await testCase. buffers
102+ . asAsyncSequence ( )
103+ . collect ( upTo: expectedBytes. count, using: . init( ) )
104+ XCTAssertEqual (
105+ accumulatedBytes1,
106+ ByteBuffer ( bytes: expectedBytes) ,
107+ file: testCase. file,
108+ line: testCase. line
109+ )
110+
111+ // test for the `ByteBuffer` optimised version
112+ let accumulatedBytes2 = try await testCase. buffers
113+ . map ( ByteBuffer . init ( bytes: ) )
114+ . asAsyncSequence ( )
115+ . collect ( upTo: expectedBytes. count)
116+ XCTAssertEqual (
117+ accumulatedBytes2,
118+ ByteBuffer ( bytes: expectedBytes) ,
119+ file: testCase. file,
120+ line: testCase. line
121+ )
122+
123+ // unhappy case where maxBytes is one byte less than actually received
124+ guard expectedBytes. count >= 1 else {
125+ continue
126+ }
127+
128+ // test for the generic version
129+ await XCTAssertThrowsError (
130+ try await testCase. buffers
103131 . asAsyncSequence ( )
104- . collect ( upTo: expectedBytes. count, using: . init( ) )
105- XCTAssertEqual (
106- accumulatedBytes1,
107- ByteBuffer ( bytes: expectedBytes) ,
132+ . collect ( upTo: max ( expectedBytes. count - 1 , 0 ) , using: . init( ) ) ,
133+ file: testCase. file,
134+ line: testCase. line
135+ ) { error in
136+ XCTAssertTrue (
137+ error is NIOTooManyBytesError ,
108138 file: testCase. file,
109139 line: testCase. line
110140 )
111-
112- // test for the `ByteBuffer` optimised version
113- let accumulatedBytes2 = try await testCase. buffers
141+ }
142+
143+ // test for the `ByteBuffer` optimised version
144+ await XCTAssertThrowsError (
145+ try await testCase. buffers
114146 . map ( ByteBuffer . init ( bytes: ) )
115147 . asAsyncSequence ( )
116- . collect ( upTo: expectedBytes. count)
117- XCTAssertEqual (
118- accumulatedBytes2,
119- ByteBuffer ( bytes: expectedBytes) ,
148+ . collect ( upTo: max ( expectedBytes. count - 1 , 0 ) ) ,
149+ file: testCase. file,
150+ line: testCase. line
151+ ) { error in
152+ XCTAssertTrue (
153+ error is NIOTooManyBytesError ,
120154 file: testCase. file,
121155 line: testCase. line
122156 )
123-
124- // unhappy case where maxBytes is one byte less than actually received
125- guard expectedBytes. count >= 1 else {
126- continue
127- }
128-
129- // test for the generic version
130- await XCTAssertThrowsError (
131- try await testCase. buffers
132- . asAsyncSequence ( )
133- . collect ( upTo: max ( expectedBytes. count - 1 , 0 ) , using: . init( ) ) ,
134- file: testCase. file,
135- line: testCase. line
136- ) { error in
137- XCTAssertTrue (
138- error is NIOTooManyBytesError ,
139- file: testCase. file,
140- line: testCase. line
141- )
142- }
143-
144- // test for the `ByteBuffer` optimised version
145- await XCTAssertThrowsError (
146- try await testCase. buffers
147- . map ( ByteBuffer . init ( bytes: ) )
148- . asAsyncSequence ( )
149- . collect ( upTo: max ( expectedBytes. count - 1 , 0 ) ) ,
150- file: testCase. file,
151- line: testCase. line
152- ) { error in
153- XCTAssertTrue (
154- error is NIOTooManyBytesError ,
155- file: testCase. file,
156- line: testCase. line
157- )
158- }
159157 }
160158 }
161159 }
0 commit comments