Skip to content

Commit

Permalink
fix queue tests
Browse files Browse the repository at this point in the history
  • Loading branch information
joshuawright11 committed Jul 27, 2024
1 parent a093146 commit 00ae0de
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 17 deletions.
5 changes: 4 additions & 1 deletion Alchemy/Queue/Commands/WorkCommand.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ struct WorkCommand: Command {
/// Should the scheduler run in process, scheduling any recurring work.
@Flag var schedule: Bool = false

/// Should the worker continue dequeuing jobs after it runs one.
@Flag var empty: Bool = false

var queue: Queue {
Container.require(id: name)
}
Expand All @@ -28,7 +31,7 @@ struct WorkCommand: Command {
}

for _ in 0..<workers {
queue.startWorker(for: channels.components(separatedBy: ","))
queue.startWorker(for: channels.components(separatedBy: ","), untilEmpty: empty)
}

try await Life.runServices()
Expand Down
1 change: 1 addition & 0 deletions AlchemyTest/TestCase/TestCase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ open class TestCase<A: Application>: XCTestCase {

open override func tearDown() async throws {
try await super.tearDown()
await app.stop()
try await app.didRun()
app.container.reset()
}
Expand Down
23 changes: 7 additions & 16 deletions Tests/Queues/QueueTests.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
@testable
import Alchemy
import AlchemyTest
import NIOEmbedded

final class QueueTests: TestCase<TestApp> {
private lazy var allTests = [
Expand Down Expand Up @@ -82,24 +81,18 @@ final class QueueTests: TestCase<TestApp> {
let exp = expectation(description: "")
ConfirmableJob.didRun = { exp.fulfill() }
try await ConfirmableJob().dispatch()

let loop = EmbeddedEventLoop()
Q.startWorker()
loop.advanceTime(by: .seconds(5))

await fulfillment(of: [exp], timeout: kMinTimeout)
app.background("queue:work")
await fulfillment(of: [exp], timeout: 2)
}

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

let loop = EmbeddedEventLoop()
Q.startWorker()
loop.advanceTime(by: .seconds(5))

await fulfillment(of: [exp], timeout: kMinTimeout)
app.background("queue:work")

await fulfillment(of: [exp], timeout: 2)
AssertNil(try await Q.dequeue(from: ["default"]))
}

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

let loop = EmbeddedEventLoop()
Q.startWorker(untilEmpty: false)
loop.advanceTime(by: .seconds(5))
await fulfillment(of: [exp], timeout: kMinTimeout)
app.background("queue:work")
await fulfillment(of: [exp], timeout: 1.1)

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

0 comments on commit 00ae0de

Please sign in to comment.