@@ -602,4 +602,116 @@ class TaskQueueTests: XCTestCase {
602602 }
603603 }
604604 }
605+
606+ func testCancellableAndNonCancellableTasksOnSingleQueue( ) async throws {
607+ let queue = TaskQueue ( )
608+ await Self . checkExecInterval ( durationInSeconds: 0 ) {
609+ await withThrowingTaskGroup ( of: Void . self) { group in
610+ group. addTask {
611+ try await queue. exec {
612+ try await Self . sleep ( seconds: 2 )
613+ }
614+ }
615+ group. addTask {
616+ try await queue. exec {
617+ try await Self . sleep ( seconds: 3 )
618+ }
619+ }
620+ group. addTask {
621+ await queue. exec {
622+ do {
623+ try await Self . sleep ( seconds: 4 )
624+ XCTFail ( " Unexpected task progression " )
625+ } catch {
626+ XCTAssertTrue (
627+ type ( of: error) == CancellationError . self
628+ )
629+ }
630+ }
631+ }
632+ group. cancelAll ( )
633+ }
634+ }
635+ }
636+
637+ func testCancellableAndNonCancellableTasksOnSingleQueueWithBarrier( )
638+ async throws
639+ {
640+ let queue = TaskQueue ( )
641+ try await Self . checkExecInterval ( durationInSeconds: 3 ) {
642+ try await withThrowingTaskGroup ( of: Void . self) { group in
643+ group. addTask {
644+ try await queue. exec {
645+ try await Self . sleep ( seconds: 1 )
646+ }
647+ }
648+ group. addTask {
649+ try await queue. exec {
650+ try await Self . sleep ( seconds: 2 )
651+ }
652+ }
653+ group. addTask {
654+ try await queue. exec {
655+ try await Self . sleep ( seconds: 3 )
656+ }
657+ }
658+ // Make sure previous tasks started
659+ try await Self . sleep ( forSeconds: 0.01 )
660+ await group. addTaskAndStart {
661+ try await queue. exec ( flags: . barrier) {
662+ try await Self . sleep ( seconds: 2 )
663+ }
664+ }
665+ // Make sure previous tasks started
666+ try await Self . sleep ( forSeconds: 0.01 )
667+ group. addTask {
668+ try await queue. exec {
669+ try await Self . sleep ( seconds: 2 )
670+ }
671+ }
672+ group. addTask {
673+ await queue. exec {
674+ do {
675+ try await Self . sleep ( seconds: 3 )
676+ XCTFail ( " Unexpected task progression " )
677+ } catch {
678+ XCTAssertTrue (
679+ type ( of: error) == CancellationError . self
680+ )
681+ }
682+ }
683+ }
684+ group. addTask {
685+ await queue. exec {
686+ do {
687+ try await Self . sleep ( seconds: 4 )
688+ XCTFail ( " Unexpected task progression " )
689+ } catch {
690+ XCTAssertTrue (
691+ type ( of: error) == CancellationError . self
692+ )
693+ }
694+ }
695+ }
696+
697+ for _ in 0 ..< 3 { try await group. next ( ) }
698+ group. cancelAll ( )
699+ }
700+ }
701+ }
702+
703+ func testTaskExecutionWithJustAddingTasks( ) async throws {
704+ let queue = TaskQueue ( )
705+ queue. addTask ( flags: . barrier) {
706+ try await Self . sleep ( seconds: 2 )
707+ }
708+ // Make sure previous tasks started
709+ try await Self . sleep ( forSeconds: 0.01 )
710+ await Self . checkExecInterval ( durationInSeconds: 2 ) {
711+ queue. addTask {
712+ try ! await Self . sleep ( seconds: 2 )
713+ }
714+ await queue. wait ( )
715+ }
716+ }
605717}
0 commit comments