Skip to content

Commit

Permalink
add Application.background() for running a full blown, live app in th…
Browse files Browse the repository at this point in the history
…e background during a test
  • Loading branch information
joshuawright11 committed Jul 17, 2024
1 parent f29032b commit efc6615
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 6 deletions.
13 changes: 13 additions & 0 deletions AlchemyTest/Utilities/Application+Utilities.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import Alchemy

extension Application {
/// Starts the application in a background task.
public func background(_ args: String...) {
background(args)
}

/// Starts the application in a background task.
public func background(_ args: [String]) {
Task { try await run(args) }
}
}
4 changes: 2 additions & 2 deletions Tests/HTTP/Commands/ServeCommandTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ final class ServeCommandTests: TestCase<TestApp> {

func testServe() async throws {
app.get("/foo", use: { _ in "hello" })
Task { try await app.run("--port", "3000") }
app.background("--port", "3000")
try await Http.get("http://127.0.0.1:3000/foo")
.assertBody("hello")

Expand All @@ -21,7 +21,7 @@ final class ServeCommandTests: TestCase<TestApp> {

func testServeWithSideEffects() async throws {
app.get("/foo", use: { _ in "hello" })
Task { try await app.run("--workers", "2", "--schedule", "--migrate") }
app.background("--workers", "2", "--schedule", "--migrate")
try await Http.get("http://127.0.0.1:3000/foo")
.assertBody("hello")

Expand Down
6 changes: 3 additions & 3 deletions Tests/Queues/Commands/WorkCommandTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ final class WorkCommandTests: TestCase<TestApp> {
}

func testRun() async throws {
Task { try await app.run("queue:work", "--workers", "5") }
app.background("queue:work", "--workers", "5")

// wait for services to boot up
try await Task.sleep(for: .milliseconds(10))
Expand All @@ -20,7 +20,7 @@ final class WorkCommandTests: TestCase<TestApp> {

func testRunName() async throws {
Queue.fake("a")
Task { try await app.run("queue:work", "--name", "a", "--workers", "5") }
app.background("queue:work", "--name", "a", "--workers", "5")

// wait for services to boot up
try await Task.sleep(for: .milliseconds(10))
Expand All @@ -31,7 +31,7 @@ final class WorkCommandTests: TestCase<TestApp> {
}

func testRunCLI() async throws {
Task { try await app.run("queue:work", "--workers", "3", "--schedule") }
app.background("queue:work", "--workers", "3", "--schedule")

// wait for services to boot up
try await Task.sleep(for: .milliseconds(10))
Expand Down
2 changes: 1 addition & 1 deletion Tests/Routing/RouterTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ final class RouterTests: TestCase<TestApp> {
}
}

Task { try await app.run() }
app.background()

var expected = ["foo", "bar", "baz"]
try await Http
Expand Down

0 comments on commit efc6615

Please sign in to comment.