@@ -157,58 +157,96 @@ final class AsyncChannelKitTests: XCTestCase {
157157 let channel = AsyncChannel < String > ( )
158158 let ready = expectation ( description: " ready " )
159159 let task : Task < String ? , Never > = Task {
160- var iterator = channel. makeAsyncIterator ( )
161- ready. fulfill ( )
162- return await iterator. next ( )
160+ var iterator = channel. makeAsyncIterator ( )
161+ ready. fulfill ( )
162+ return await iterator. next ( )
163163 }
164- wait ( for: [ ready] , timeout: 1.0 )
164+
165+ await waitForExpectations ( timeout: 0.1 )
165166 task. cancel ( )
166- let value = await task. value
167- XCTAssertNil ( value)
167+
168+ let done = expectation ( description: " done " )
169+
170+ Task {
171+ let value = await task. value
172+ XCTAssertNil ( value)
173+ done. fulfill ( )
174+ }
175+
176+ await waitForExpectations ( timeout: 1.0 )
168177 }
169178
170179 func testThrowingChannelCancelled( ) async throws {
171180 let channel = AsyncThrowingChannel < String , Error > ( )
172181 let ready = expectation ( description: " ready " )
173182 let task : Task < String ? , Error > = Task {
174- var iterator = channel. makeAsyncIterator ( )
175- ready. fulfill ( )
176- return try await iterator. next ( )
183+ var iterator = channel. makeAsyncIterator ( )
184+ ready. fulfill ( )
185+ return try await iterator. next ( )
177186 }
178- wait ( for: [ ready] , timeout: 1.0 )
187+
188+ await waitForExpectations ( timeout: 0.1 )
179189 task. cancel ( )
180- let value = try await task. value
181- XCTAssertNil ( value)
190+
191+ let done = expectation ( description: " done " )
192+
193+ Task {
194+ let value = try await task. value
195+ XCTAssertNil ( value)
196+ done. fulfill ( )
197+ }
198+
199+ await waitForExpectations ( timeout: 1.0 )
182200 }
183201
184- func testChannelCancelledOnSend( ) async {
185- let channel = AsyncChannel < Int > ( )
186- let notYetDone = expectation ( description: " not yet done " )
187- notYetDone. isInverted = true
188- let done = expectation ( description: " done " )
189- let task = Task {
190- await channel. send ( 1 )
191- notYetDone. fulfill ( )
192- done. fulfill ( )
193- }
194- wait ( for: [ notYetDone] , timeout: 0.1 )
195- task. cancel ( )
196- wait ( for: [ done] , timeout: 1.0 )
202+ func testChannelCancelledOnSend( ) async throws {
203+ let channel = AsyncChannel < Int > ( )
204+ let input = " done "
205+ let notYetDone = expectation ( description: " not yet done " )
206+ notYetDone. isInverted = true
207+ let task : Task < String , Never > = Task {
208+ await channel. send ( 1 )
209+ notYetDone. fulfill ( )
210+ return input
211+ }
212+
213+ await waitForExpectations ( timeout: 0.1 )
214+ task. cancel ( )
215+
216+ let done = expectation ( description: " done " )
217+
218+ Task {
219+ let output = await task. value
220+ XCTAssertEqual ( input, output)
221+ done. fulfill ( )
222+ }
223+
224+ await waitForExpectations ( timeout: 1.0 )
197225 }
198226
199- func testThrowingChannelCancelledOnSend( ) async {
200- let channel = AsyncThrowingChannel < Int , Error > ( )
201- let notYetDone = expectation ( description: " not yet done " )
202- notYetDone. isInverted = true
203- let done = expectation ( description: " done " )
204- let task = Task {
205- await channel. send ( 1 )
206- notYetDone. fulfill ( )
207- done. fulfill ( )
208- }
209- wait ( for: [ notYetDone] , timeout: 0.1 )
210- task. cancel ( )
211- wait ( for: [ done] , timeout: 1.0 )
227+ func testThrowingChannelCancelledOnSend( ) async throws {
228+ let channel = AsyncThrowingChannel < Int , Error > ( )
229+ let input = " done "
230+ let notYetDone = expectation ( description: " not yet done " )
231+ notYetDone. isInverted = true
232+ let task : Task < String , Never > = Task {
233+ await channel. send ( 1 )
234+ notYetDone. fulfill ( )
235+ return input
236+ }
237+
238+ await waitForExpectations ( timeout: 0.1 )
239+ task. cancel ( )
240+
241+ let done = expectation ( description: " done " )
242+
243+ Task {
244+ let output = await task. value
245+ XCTAssertEqual ( input, output)
246+ done. fulfill ( )
247+ }
248+
249+ await waitForExpectations ( timeout: 1.0 )
212250 }
213251
214252 private func send< Element> ( elements: [ Element ] , channel: AsyncChannel < Element > , delay: Double = 0.1 ) async throws {
0 commit comments