Skip to content

Commit 00ae0de

Browse files
fix queue tests
1 parent a093146 commit 00ae0de

File tree

3 files changed

+12
-17
lines changed

3 files changed

+12
-17
lines changed

Alchemy/Queue/Commands/WorkCommand.swift

+4-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ struct WorkCommand: Command {
1616
/// Should the scheduler run in process, scheduling any recurring work.
1717
@Flag var schedule: Bool = false
1818

19+
/// Should the worker continue dequeuing jobs after it runs one.
20+
@Flag var empty: Bool = false
21+
1922
var queue: Queue {
2023
Container.require(id: name)
2124
}
@@ -28,7 +31,7 @@ struct WorkCommand: Command {
2831
}
2932

3033
for _ in 0..<workers {
31-
queue.startWorker(for: channels.components(separatedBy: ","))
34+
queue.startWorker(for: channels.components(separatedBy: ","), untilEmpty: empty)
3235
}
3336

3437
try await Life.runServices()

AlchemyTest/TestCase/TestCase.swift

+1
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ open class TestCase<A: Application>: XCTestCase {
5151

5252
open override func tearDown() async throws {
5353
try await super.tearDown()
54+
await app.stop()
5455
try await app.didRun()
5556
app.container.reset()
5657
}

Tests/Queues/QueueTests.swift

+7-16
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
@testable
22
import Alchemy
33
import AlchemyTest
4-
import NIOEmbedded
54

65
final class QueueTests: TestCase<TestApp> {
76
private lazy var allTests = [
@@ -82,24 +81,18 @@ final class QueueTests: TestCase<TestApp> {
8281
let exp = expectation(description: "")
8382
ConfirmableJob.didRun = { exp.fulfill() }
8483
try await ConfirmableJob().dispatch()
85-
86-
let loop = EmbeddedEventLoop()
87-
Q.startWorker()
88-
loop.advanceTime(by: .seconds(5))
89-
90-
await fulfillment(of: [exp], timeout: kMinTimeout)
84+
app.background("queue:work")
85+
await fulfillment(of: [exp], timeout: 2)
9186
}
9287

9388
private func _testFailure(file: StaticString = #filePath, line: UInt = #line) async throws {
9489
let exp = expectation(description: "")
9590
FailureJob.didFinish = { exp.fulfill() }
9691
try await FailureJob().dispatch()
9792

98-
let loop = EmbeddedEventLoop()
99-
Q.startWorker()
100-
loop.advanceTime(by: .seconds(5))
101-
102-
await fulfillment(of: [exp], timeout: kMinTimeout)
93+
app.background("queue:work")
94+
95+
await fulfillment(of: [exp], timeout: 2)
10396
AssertNil(try await Q.dequeue(from: ["default"]))
10497
}
10598

@@ -108,10 +101,8 @@ final class QueueTests: TestCase<TestApp> {
108101
RetryJob.didFail = { exp.fulfill() }
109102
try await RetryJob(foo: "bar").dispatch()
110103

111-
let loop = EmbeddedEventLoop()
112-
Q.startWorker(untilEmpty: false)
113-
loop.advanceTime(by: .seconds(5))
114-
await fulfillment(of: [exp], timeout: kMinTimeout)
104+
app.background("queue:work")
105+
await fulfillment(of: [exp], timeout: 1.1)
115106

116107
guard let jobData = try await Q.dequeue(from: ["default"]) else {
117108
XCTFail("Failed to dequeue a job.", file: file, line: line)

0 commit comments

Comments
 (0)