@@ -683,13 +683,13 @@ final class JobsTests: XCTestCase {
683
683
group. cancelAll ( )
684
684
}
685
685
}
686
-
687
- func testJobActions ( ) async throws {
686
+
687
+ func testResumableAndPausableJobs ( ) async throws {
688
688
struct TestParameters : JobParameters {
689
- static let jobName = " testJobActions "
689
+ static let jobName = " testResumableAndPausableJobs "
690
690
let value : Int
691
691
}
692
- let expectation = XCTestExpectation ( description: " TestJob.execute was called " , expectedFulfillmentCount: 3 )
692
+ let expectation = XCTestExpectation ( description: " TestJob.execute was called " , expectedFulfillmentCount: 2 )
693
693
let jobExecutionSequence : NIOLockedValueBox < [ Int ] > = . init( [ ] )
694
694
695
695
let jobQueue = try await self . createJobQueue ( numWorkers: 1 , configuration: . init( ) , function: #function)
@@ -713,21 +713,70 @@ final class JobsTests: XCTestCase {
713
713
714
714
try await jobQueue. pauseJob ( jobID: firstJob)
715
715
716
+ try await queue. push (
717
+ TestParameters ( value: 2025 ) ,
718
+ options: . init(
719
+ priority: . normal( )
720
+ )
721
+ )
722
+
723
+ try await withThrowingTaskGroup ( of: Void . self) { group in
724
+ let serviceGroup = ServiceGroup ( services: [ queue] , logger: queue. logger)
725
+
726
+ let processingJobs = try await jobQueue. queue. getJobs ( withStatus: . pending)
727
+ XCTAssertEqual ( processingJobs. count, 1 )
728
+
729
+ group. addTask {
730
+ try await serviceGroup. run ( )
731
+ }
732
+
733
+ let pausedJobs = try await jobQueue. queue. getJobs ( withStatus: . paused)
734
+ XCTAssertEqual ( pausedJobs. count, 1 )
735
+
736
+ try await jobQueue. resumeJob ( jobID: firstJob)
737
+
738
+ await fulfillment ( of: [ expectation] , timeout: 10 )
739
+ await serviceGroup. triggerGracefulShutdown ( )
740
+ }
741
+ }
742
+ XCTAssertEqual ( jobExecutionSequence. withLockedValue { $0 } , [ 2025 , 20 ] )
743
+ }
744
+
745
+ func testCancellableJob( ) async throws {
746
+ struct TestParameters : JobParameters {
747
+ static let jobName = " testCancellableJob "
748
+ let value : Int
749
+ }
750
+ let expectation = XCTestExpectation ( description: " TestJob.execute was called " , expectedFulfillmentCount: 1 )
751
+ let jobExecutionSequence : NIOLockedValueBox < [ Int ] > = . init( [ ] )
752
+
753
+ let jobQueue = try await self . createJobQueue ( numWorkers: 1 , configuration: . init( ) , function: #function)
754
+
755
+ try await testPriorityJobQueue ( jobQueue: jobQueue) { queue in
756
+ queue. registerJob ( parameters: TestParameters . self) { parameters, context in
757
+ context. logger. info ( " Parameters= \( parameters. value) " )
758
+ jobExecutionSequence. withLockedValue {
759
+ $0. append ( parameters. value)
760
+ }
761
+ try await Task . sleep ( for: . milliseconds( Int . random ( in: 10 ..< 50 ) ) )
762
+ expectation. fulfill ( )
763
+ }
764
+
716
765
try await queue. push (
717
766
TestParameters ( value: 2025 ) ,
718
767
options: . init(
719
768
priority: . highest( )
720
769
)
721
770
)
722
771
723
- let cancelID = try await queue. push (
772
+ let cancellableJob = try await queue. push (
724
773
TestParameters ( value: 42 ) ,
725
774
options: . init(
726
775
priority: . lower( )
727
776
)
728
777
)
729
778
730
- try await jobQueue. cancelJob ( jobID: cancelID )
779
+ try await jobQueue. cancelJob ( jobID: cancellableJob )
731
780
732
781
try await withThrowingTaskGroup ( of: Void . self) { group in
733
782
let serviceGroup = ServiceGroup ( services: [ queue] , logger: queue. logger)
@@ -739,18 +788,16 @@ final class JobsTests: XCTestCase {
739
788
try await serviceGroup. run ( )
740
789
}
741
790
742
- let pausedJobs = try await jobQueue . queue . getJobs ( withStatus : . paused )
743
- XCTAssertEqual ( pausedJobs . count , 1 )
791
+ await fulfillment ( of : [ expectation ] , timeout : 10 )
792
+
744
793
let cancelledJobs = try await jobQueue. queue. getJobs ( withStatus: . cancelled)
745
794
XCTAssertEqual ( cancelledJobs. count, 1 )
746
795
747
- try await jobQueue. resumeJob ( jobID: firstJob)
748
- try await jobQueue. resumeJob ( jobID: cancelID)
749
-
750
- await fulfillment ( of: [ expectation] , timeout: 10 )
751
796
await serviceGroup. triggerGracefulShutdown ( )
797
+
798
+ try await jobQueue. queue. delete ( jobID: cancellableJob)
752
799
}
753
800
}
754
- XCTAssertEqual ( jobExecutionSequence. withLockedValue { $0 } , [ 2025 , 20 , 42 ] )
801
+ XCTAssertEqual ( jobExecutionSequence. withLockedValue { $0 } , [ 2025 ] )
755
802
}
756
803
}
0 commit comments