Skip to content

Commit

Permalink
Async-ify the tests, such as they are
Browse files Browse the repository at this point in the history
  • Loading branch information
gwynne committed May 2, 2024
1 parent f36fcca commit 63f9ccb
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 13 deletions.
2 changes: 1 addition & 1 deletion Sources/QueuesFluentDriver/FluentQueuesDriver.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public struct FluentQueuesDriver: QueuesDriver {
public func shutdown() {}
}

private struct FailingQueue: Queue {
/*private*/ struct FailingQueue: Queue {
let failure: any Error
let context: QueueContext

Expand Down
85 changes: 73 additions & 12 deletions Tests/QueuesFluentDriverTests/QueuesFluentDriverTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import Logging
import FluentSQLiteDriver

final class QueuesFluentDriverTests: XCTestCase {
func testApplication() throws {
func testApplication() async throws {
let app = Application(.testing)
defer { app.shutdown() }

Expand All @@ -19,7 +19,7 @@ final class QueuesFluentDriverTests: XCTestCase {

app.queues.use(.fluent())

try app.autoMigrate().wait()
try await app.autoMigrate()

app.get("send-email") { req in
req.queue.dispatch(Email.self, .init(to: "[email protected]"))
Expand All @@ -31,19 +31,21 @@ final class QueuesFluentDriverTests: XCTestCase {
}

XCTAssertEqual(email.sent, [])
try app.queues.queue.worker.run().wait()
try await app.queues.queue.worker.run().get()
XCTAssertEqual(email.sent, [.init(to: "[email protected]")])

try await app.autoRevert()
}

func testFailedJobLoss() throws {
func testFailedJobLoss() async throws {
let app = Application(.testing)
defer { app.shutdown() }

app.databases.use(.sqlite(.memory), as: .sqlite)
app.queues.add(FailingJob())
app.queues.use(.fluent())
app.migrations.add(JobModelMigration())
try app.autoMigrate().wait()
try await app.autoMigrate()

let jobId = JobIdentifier()
app.get("test") { req in
Expand All @@ -55,15 +57,17 @@ final class QueuesFluentDriverTests: XCTestCase {
XCTAssertEqual(res.status, .ok)
}

XCTAssertThrowsError(try app.queues.queue.worker.run().wait()) {
await XCTAssertThrowsErrorAsync(try await app.queues.queue.worker.run().get()) {
XCTAssert($0 is FailingJob.Failure)
}

XCTAssertNotNil(try (app.databases.database(logger: .init(label: ""), on: app.eventLoopGroup.any())! as! any SQLDatabase)
.select().columns("*").from(JobModel.schema).where("id", .equal, jobId.string).first().wait())
await XCTAssertNotNilAsync(try await (app.databases.database(logger: .init(label: ""), on: app.eventLoopGroup.any())! as! any SQLDatabase)
.select().columns("*").from(JobModel.schema).where("id", .equal, jobId.string).first())

try await app.autoRevert()
}

func testDelayedJobIsRemovedFromProcessingQueue() throws {
func testDelayedJobIsRemovedFromProcessingQueue() async throws {
let app = Application(.testing)
defer { app.shutdown() }

Expand All @@ -74,7 +78,7 @@ final class QueuesFluentDriverTests: XCTestCase {
app.queues.use(.fluent())

app.migrations.add(JobModelMigration())
try app.autoMigrate().wait()
try await app.autoMigrate()

let jobId = JobIdentifier()
app.get("delay-job") { req in
Expand All @@ -88,9 +92,25 @@ final class QueuesFluentDriverTests: XCTestCase {
XCTAssertEqual(res.status, .ok)
}

XCTAssertEqual(try (app.databases.database(logger: .init(label: ""), on: app.eventLoopGroup.any())! as! any SQLDatabase)
await XCTAssertEqualAsync(try await (app.databases.database(logger: .init(label: ""), on: app.eventLoopGroup.any())! as! any SQLDatabase)
.select().columns("*").from(JobModel.schema).where("id", .equal, jobId.string)
.first(decoding: JobModel.self, keyDecodingStrategy: .convertFromSnakeCase).wait()?.state, .pending)
.first(decoding: JobModel.self, keyDecodingStrategy: .convertFromSnakeCase)?.state, .pending)

try await app.autoRevert()
}

func testCoverageForFailingQueue() {
let app = Application(.testing)
defer { app.shutdown() }
let queue = FailingQueue(
failure: QueuesFluentError.unsupportedDatabase,
context: .init(queueName: .init(string: ""), configuration: .init(), application: app, logger: .init(label: ""), on: app.eventLoopGroup.any())
)
XCTAssertThrowsError(try queue.get(.init()).wait())
XCTAssertThrowsError(try queue.set(.init(), to: JobData(payload: [], maxRetryCount: 0, jobName: "", delayUntil: nil, queuedAt: .init())).wait())
XCTAssertThrowsError(try queue.clear(.init()).wait())
XCTAssertThrowsError(try queue.push(.init()).wait())
XCTAssertThrowsError(try queue.pop().wait())
}

override func setUp() {
Expand Down Expand Up @@ -135,6 +155,47 @@ struct FailingJob: Job {
}
}

func XCTAssertEqualAsync<T>(
_ expression1: @autoclosure () async throws -> T,
_ expression2: @autoclosure () async throws -> T,
_ message: @autoclosure () -> String = "",
file: StaticString = #filePath, line: UInt = #line
) async where T: Equatable {
do {
let expr1 = try await expression1(), expr2 = try await expression2()
return XCTAssertEqual(expr1, expr2, message(), file: file, line: line)
} catch {
return XCTAssertEqual(try { () -> Bool in throw error }(), false, message(), file: file, line: line)
}
}

func XCTAssertThrowsErrorAsync<T>(
_ expression: @autoclosure () async throws -> T,
_ message: @autoclosure () -> String = "",
file: StaticString = #filePath, line: UInt = #line,
_ callback: (any Error) -> Void = { _ in }
) async {
do {
_ = try await expression()
XCTAssertThrowsError({}(), message(), file: file, line: line, callback)
} catch {
XCTAssertThrowsError(try { throw error }(), message(), file: file, line: line, callback)
}
}

func XCTAssertNotNilAsync(
_ expression: @autoclosure () async throws -> Any?,
_ message: @autoclosure () -> String = "",
file: StaticString = #filePath, line: UInt = #line
) async {
do {
let result = try await expression()
XCTAssertNotNil(result, message(), file: file, line: line)
} catch {
return XCTAssertNotNil(try { throw error }(), message(), file: file, line: line)
}
}

func env(_ name: String) -> String? {
return ProcessInfo.processInfo.environment[name]
}
Expand Down

0 comments on commit 63f9ccb

Please sign in to comment.