@@ -456,67 +456,35 @@ final class AsyncTaskQueueTests: XCTestCase {
456
456
wait ( for: [ setUpExpectation] , timeout: 1 )
457
457
}
458
458
459
- func test_that_it_runs_in_parallel_until_it_finds_a_barrier_then_it_waits_for_barrier_to_end_to_continue( ) {
459
+ func test_that_it_runs_in_parallel_until_it_finds_a_barrier_then_it_waits_for_barrier_to_end_to_continue( ) async {
460
460
let asyncTaskQueue = AsyncTaskQueue ( maxConcurrentTasks: 2 )
461
-
462
461
let operation1 = BlockingOperation ( )
463
462
let operation2 = BlockingOperation ( )
464
463
let operation3 = BlockingOperation ( )
465
464
let operation4 = BlockingOperation ( )
466
465
467
- Task ( priority: . first) {
468
- do {
466
+ // Create a task group to manage all operations
467
+ await withThrowingTaskGroup ( of: Void . self) { group in
468
+ // First concurrent operations
469
+ group. addTask {
469
470
try await asyncTaskQueue. enqueue ( operation: {
470
471
await operation1. setUp ( )
471
472
await operation1. perform ( )
472
473
} )
473
- } catch {
474
- XCTFail ( " unexpected error thrown: \( error) " )
475
474
}
476
- }
477
475
478
- Task ( priority: . second) {
479
- do {
476
+ group. addTask {
480
477
try await asyncTaskQueue. enqueue ( operation: {
481
478
await operation2. setUp ( )
482
479
await operation2. perform ( )
483
480
} )
484
- } catch {
485
- XCTFail ( " unexpected error thrown: \( error) " )
486
- }
487
- }
488
-
489
- Task ( priority: . third) {
490
- do {
491
- try await asyncTaskQueue. enqueueBarrier ( operation: { completion in
492
- defer { completion ( ) }
493
-
494
- await operation3. setUp ( )
495
- await operation3. perform ( )
496
- } )
497
- } catch {
498
- XCTFail ( " unexpected error thrown: \( error) " )
499
- }
500
- }
501
-
502
- Task ( priority: . fourth) {
503
- do {
504
- try await asyncTaskQueue. enqueue ( operation: {
505
- await operation4. setUp ( )
506
- await operation4. perform ( )
507
- } )
508
- } catch {
509
- XCTFail ( " unexpected error thrown: \( error) " )
510
481
}
511
- }
512
-
513
- let setUpExpectation = expectation ( description: " set_up_expectation " )
514
482
515
- Task { @ MainActor in
483
+ // Wait for first two operations to be set up
516
484
await operation1. waitForSetUp ( )
517
485
await operation2. waitForSetUp ( )
518
- try ? await Task . sleep ( nanoseconds : 1000 )
519
-
486
+
487
+ // Verify initial state
520
488
let operation1HasStarted = await operation1. hasStarted
521
489
let operation2HasStarted = await operation2. hasStarted
522
490
var operation3HasStarted = await operation3. hasStarted
@@ -529,10 +497,23 @@ final class AsyncTaskQueueTests: XCTestCase {
529
497
XCTAssertFalse ( operation4HasStarted)
530
498
XCTAssertEqual ( runningTasks, 2 )
531
499
500
+ // Complete first two operations
532
501
await operation1. resume ( )
533
502
await operation2. resume ( )
503
+
504
+ // Add barrier operation
505
+ group. addTask {
506
+ try await asyncTaskQueue. enqueueBarrier ( operation: { completion in
507
+ defer { completion ( ) }
508
+ await operation3. setUp ( )
509
+ await operation3. perform ( )
510
+ } )
511
+ }
512
+
513
+ // Wait for barrier operation to start
534
514
await operation3. waitForSetUp ( )
535
515
516
+ // Verify state after barrier starts
536
517
let operation1HasCompleted = await operation1. hasCompleted
537
518
let operation2HasCompleted = await operation2. hasCompleted
538
519
operation3HasStarted = await operation3. hasStarted
@@ -545,9 +526,21 @@ final class AsyncTaskQueueTests: XCTestCase {
545
526
XCTAssertFalse ( operation4HasStarted)
546
527
XCTAssertEqual ( runningTasks, 1 )
547
528
529
+ // Complete barrier operation
548
530
await operation3. resume ( )
531
+
532
+ // Add final concurrent operation
533
+ group. addTask {
534
+ try await asyncTaskQueue. enqueue ( operation: {
535
+ await operation4. setUp ( )
536
+ await operation4. perform ( )
537
+ } )
538
+ }
539
+
540
+ // Wait for final operation to start
549
541
await operation4. waitForSetUp ( )
550
542
543
+ // Verify final state
551
544
let operation3HasCompleted = await operation3. hasCompleted
552
545
operation4HasStarted = await operation4. hasStarted
553
546
runningTasks = await asyncTaskQueue. runningTasks
@@ -558,14 +551,12 @@ final class AsyncTaskQueueTests: XCTestCase {
558
551
XCTAssertTrue ( operation4HasStarted)
559
552
XCTAssertEqual ( runningTasks, 1 )
560
553
561
- setUpExpectation. fulfill ( )
554
+ // Complete final operation
555
+ await operation4. resume ( )
562
556
}
563
-
564
- wait ( for: [ setUpExpectation] , timeout: 1 )
565
557
}
566
558
567
559
func test_that_enqueue_continues_to_work_even_if_previous_operation_threw_an_error( ) {
568
-
569
560
let asyncTaskQueue = AsyncTaskQueue ( maxConcurrentTasks: 1 )
570
561
571
562
Task ( priority: . first) {
0 commit comments