@@ -683,4 +683,148 @@ final class JobsTests: XCTestCase {
683
683
group. cancelAll ( )
684
684
}
685
685
}
686
+
687
+ func testResumableAndPausableJobs( ) async throws {
688
+ struct TestParameters : JobParameters {
689
+ static let jobName = " TestJob "
690
+ }
691
+ struct ResumableJob : JobParameters {
692
+ static let jobName = " ResumanableJob "
693
+ }
694
+ let expectation = XCTestExpectation ( description: " TestJob.execute was called " , expectedFulfillmentCount: 2 )
695
+ let didResumableJobRun : NIOLockedValueBox < Bool > = . init( false )
696
+ let didTestJobRun : NIOLockedValueBox < Bool > = . init( false )
697
+
698
+ let jobQueue = try await self . createJobQueue ( numWorkers: 1 , configuration: . init( ) , function: #function)
699
+
700
+ try await testPriorityJobQueue ( jobQueue: jobQueue) { queue in
701
+ queue. registerJob ( parameters: TestParameters . self) { parameters, _ in
702
+ didTestJobRun. withLockedValue {
703
+ $0 = true
704
+ }
705
+ try await Task . sleep ( for: . milliseconds( Int . random ( in: 10 ..< 50 ) ) )
706
+ expectation. fulfill ( )
707
+ }
708
+
709
+ queue. registerJob ( parameters: ResumableJob . self) { parameters, _ in
710
+ didResumableJobRun. withLockedValue {
711
+ $0 = true
712
+ }
713
+ try await Task . sleep ( for: . milliseconds( Int . random ( in: 10 ..< 50 ) ) )
714
+ expectation. fulfill ( )
715
+ }
716
+
717
+ let resumableJob = try await queue. push (
718
+ ResumableJob ( ) ,
719
+ options: . init(
720
+ priority: . lowest( )
721
+ )
722
+ )
723
+
724
+ try await queue. push (
725
+ TestParameters ( ) ,
726
+ options: . init(
727
+ priority: . normal( )
728
+ )
729
+ )
730
+
731
+ try await jobQueue. pauseJob ( jobID: resumableJob)
732
+
733
+ try await withThrowingTaskGroup ( of: Void . self) { group in
734
+ let serviceGroup = ServiceGroup ( services: [ queue] , logger: queue. logger)
735
+
736
+ let processingJobs = try await jobQueue. queue. getJobs ( withStatus: . pending)
737
+ XCTAssertEqual ( processingJobs. count, 1 )
738
+
739
+ group. addTask {
740
+ try await serviceGroup. run ( )
741
+ }
742
+
743
+ let processingJobCount = try await jobQueue. queue. getJobs ( withStatus: . processing)
744
+ XCTAssertEqual ( processingJobCount. count, 0 )
745
+
746
+ let pausedJobs = try await jobQueue. queue. getJobs ( withStatus: . paused)
747
+ XCTAssertEqual ( pausedJobs. count, 1 )
748
+
749
+ try await jobQueue. resumeJob ( jobID: resumableJob)
750
+
751
+ await fulfillment ( of: [ expectation] , timeout: 10 )
752
+ await serviceGroup. triggerGracefulShutdown ( )
753
+ }
754
+ }
755
+ XCTAssertEqual ( didTestJobRun. withLockedValue { $0 } , true )
756
+ XCTAssertEqual ( didResumableJobRun. withLockedValue { $0 } , true )
757
+ }
758
+
759
+ func testCancellableJob( ) async throws {
760
+ struct TestParameters : JobParameters {
761
+ static let jobName = " testCancellableJob "
762
+ let value : Int
763
+ }
764
+ struct NoneCancelledJobParameters : JobParameters {
765
+ static let jobName = " NoneCancelledJob "
766
+ let value : Int
767
+ }
768
+ let expectation = XCTestExpectation ( description: " TestJob.execute was called " , expectedFulfillmentCount: 1 )
769
+ let didRunCancelledJob : NIOLockedValueBox < Bool > = . init( false )
770
+ let didRunNoneCancelledJob : NIOLockedValueBox < Bool > = . init( false )
771
+
772
+ let jobQueue = try await self . createJobQueue ( numWorkers: 1 , configuration: . init( ) , function: #function)
773
+
774
+ try await testPriorityJobQueue ( jobQueue: jobQueue) { queue in
775
+ queue. registerJob ( parameters: TestParameters . self) { parameters, context in
776
+ context. logger. info ( " Parameters= \( parameters. value) " )
777
+ didRunCancelledJob. withLockedValue {
778
+ $0 = true
779
+ }
780
+ try await Task . sleep ( for: . milliseconds( Int . random ( in: 10 ..< 50 ) ) )
781
+ expectation. fulfill ( )
782
+ }
783
+
784
+ queue. registerJob ( parameters: NoneCancelledJobParameters . self) { parameters, context in
785
+ context. logger. info ( " Parameters= \( parameters. value) " )
786
+ didRunNoneCancelledJob. withLockedValue {
787
+ $0 = true
788
+ }
789
+ try await Task . sleep ( for: . milliseconds( Int . random ( in: 10 ..< 50 ) ) )
790
+ expectation. fulfill ( )
791
+ }
792
+
793
+ let cancellableJob = try await queue. push (
794
+ TestParameters ( value: 42 ) ,
795
+ options: . init(
796
+ priority: . lower( )
797
+ )
798
+ )
799
+
800
+ try await queue. push (
801
+ NoneCancelledJobParameters ( value: 2025 ) ,
802
+ options: . init(
803
+ priority: . highest( )
804
+ )
805
+ )
806
+
807
+ try await jobQueue. cancelJob ( jobID: cancellableJob)
808
+
809
+ try await withThrowingTaskGroup ( of: Void . self) { group in
810
+ let serviceGroup = ServiceGroup ( services: [ queue] , logger: queue. logger)
811
+
812
+ let processingJobs = try await jobQueue. queue. getJobs ( withStatus: . pending)
813
+ XCTAssertEqual ( processingJobs. count, 1 )
814
+
815
+ group. addTask {
816
+ try await serviceGroup. run ( )
817
+ }
818
+
819
+ await fulfillment ( of: [ expectation] , timeout: 10 )
820
+ // Jobs has been removed
821
+ let cancelledJobs = try await jobQueue. queue. getJobs ( withStatus: . cancelled)
822
+ XCTAssertEqual ( cancelledJobs. count, 0 )
823
+
824
+ await serviceGroup. triggerGracefulShutdown ( )
825
+ }
826
+ }
827
+ XCTAssertEqual ( didRunCancelledJob. withLockedValue { $0 } , false )
828
+ XCTAssertEqual ( didRunNoneCancelledJob. withLockedValue { $0 } , true )
829
+ }
686
830
}
0 commit comments