@@ -69,6 +69,20 @@ final class BufferedStreamTests: XCTestCase {
6969 XCTAssertNil ( readData2)
7070 }
7171
72+ func test_read_throwsErrorWhenStreamIsClosedWithError( ) throws {
73+ let subject = BufferedStream ( )
74+ try subject. write ( contentsOf: testData)
75+ subject. closeWithError ( TestError . error)
76+ do {
77+ let readData1 = try subject. read ( upToCount: Int . max)
78+ XCTFail ( " Error was expected to be thrown " )
79+ } catch TestError . error {
80+ // Test passes
81+ } catch {
82+ XCTFail ( " Unexpected error thrown: \( error. localizedDescription) " )
83+ }
84+ }
85+
7286 // MARK: - readToEnd()
7387
7488 func test_readToEnd_readsToEnd( ) throws {
@@ -83,6 +97,20 @@ final class BufferedStreamTests: XCTestCase {
8397 XCTAssertNil ( readData2)
8498 }
8599
100+ func test_readToEnd_throwsErrorWhenStreamIsClosedWithError( ) throws {
101+ let subject = BufferedStream ( )
102+ try subject. write ( contentsOf: testData)
103+ subject. closeWithError ( TestError . error)
104+ do {
105+ let readData1 = try subject. readToEnd ( )
106+ XCTFail ( " Error was expected to be thrown " )
107+ } catch TestError . error {
108+ // Test passes
109+ } catch {
110+ XCTFail ( " Unexpected error thrown: \( error. localizedDescription) " )
111+ }
112+ }
113+
86114 // MARK: - readToEndAsync()
87115
88116 func test_readToEndAsync_readsToEnd( ) async throws {
@@ -97,6 +125,20 @@ final class BufferedStreamTests: XCTestCase {
97125 XCTAssertNil ( readData2)
98126 }
99127
128+ func test_readToEndAsync_throwsErrorWhenStreamIsClosedWithError( ) async throws {
129+ let subject = BufferedStream ( )
130+ try subject. write ( contentsOf: testData)
131+ subject. closeWithError ( TestError . error)
132+ do {
133+ let readData1 = try await subject. readToEndAsync ( )
134+ XCTFail ( " Error was expected to be thrown " )
135+ } catch TestError . error {
136+ // Test passes
137+ } catch {
138+ XCTFail ( " Unexpected error thrown: \( error. localizedDescription) " )
139+ }
140+ }
141+
100142 // MARK: - readAsync(upToCount:)
101143
102144 func test_readAsync_readsAsynchronously( ) async throws {
@@ -142,6 +184,20 @@ final class BufferedStreamTests: XCTestCase {
142184 XCTAssertNil ( readData3)
143185 }
144186
187+ func test_readAsync_throwsErrorWhenStreamIsClosedWithError( ) async throws {
188+ let subject = BufferedStream ( )
189+ try subject. write ( contentsOf: testData)
190+ subject. closeWithError ( TestError . error)
191+ do {
192+ let readData1 = try await subject. readAsync ( upToCount: Int . max)
193+ XCTFail ( " Error was expected to be thrown " )
194+ } catch TestError . error {
195+ // Test passes
196+ } catch {
197+ XCTFail ( " Unexpected error thrown: \( error. localizedDescription) " )
198+ }
199+ }
200+
145201 // MARK: - write(contentsOf:)
146202
147203 func test_write_appendsWrittenDataToBuffer( ) throws {
@@ -189,3 +245,7 @@ final class BufferedStreamTests: XCTestCase {
189245 XCTAssertEqual ( sut. length, testData. count)
190246 }
191247}
248+
249+ private enum TestError : Error , Equatable {
250+ case error
251+ }
0 commit comments