Skip to content

Commit 47f62eb

Browse files
committed
Don't try and launch and tear down dbs in CI
1 parent 1f58315 commit 47f62eb

File tree

1 file changed

+39
-35
lines changed

1 file changed

+39
-35
lines changed

Tests/AppTests/Helpers/DatabasePool.swift

Lines changed: 39 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -43,36 +43,47 @@ actor DatabasePool {
4343
// Call DotEnvFile.load once to ensure env variables are set
4444
await DotEnvFile.load(for: .testing, fileio: .init(threadPool: .singleton))
4545

46-
// Re-use up to maxCount running dbs
47-
let runningDbs = try await runningDatabases()
48-
for db in runningDbs.prefix(maxCount) {
49-
availableDatabases.insert(db)
50-
}
46+
if isRunningInCI() {
47+
// We don't have docker available in CI to probe for running dbs.
48+
// Instead, we have a hard-coded list of dbs we launch in the GH workflow
49+
// file and correspondingly, we hard-code their ports here.
50+
availableDatabases = Set((6000..<6008).map(Database.init))
51+
} else {
52+
// Re-use up to maxCount running dbs
53+
let runningDbs = try await runningDatabases()
54+
for db in runningDbs.prefix(maxCount) {
55+
availableDatabases.insert(db)
56+
}
5157

52-
do { // Delete overprovisioned dbs
53-
let overprovisioned = runningDbs.dropFirst(maxCount)
54-
try await tearDown(databases: overprovisioned)
55-
}
58+
do { // Delete overprovisioned dbs
59+
let overprovisioned = runningDbs.dropFirst(maxCount)
60+
try await tearDown(databases: overprovisioned)
61+
}
5662

57-
do { // Create missing dbs
58-
let underprovisionedCount = max(maxCount - availableDatabases.count, 0)
59-
try await withThrowingTaskGroup(of: Database.self) { group in
60-
for _ in (0..<underprovisionedCount) {
61-
group.addTask {
62-
let db = try await self.launchDB()
63-
try await db.setup(for: .testing)
64-
return db
63+
do { // Create missing dbs
64+
let underprovisionedCount = max(maxCount - availableDatabases.count, 0)
65+
try await withThrowingTaskGroup(of: Database.self) { group in
66+
for _ in (0..<underprovisionedCount) {
67+
group.addTask {
68+
let db = try await self.launchDB()
69+
try await db.setup(for: .testing)
70+
return db
71+
}
72+
}
73+
for try await db in group {
74+
availableDatabases.insert(db)
6575
}
66-
}
67-
for try await db in group {
68-
availableDatabases.insert(db)
6976
}
7077
}
7178
}
7279
}
7380

7481
func tearDown() async throws {
75-
try await tearDown(databases: runningDatabases())
82+
if isRunningInCI() {
83+
// Let CI's tear down deal with the databases, there's nothing we can or should do here.
84+
} else {
85+
try await tearDown(databases: runningDatabases())
86+
}
7687
}
7788

7889
func tearDown(databases: any Collection<Database>) async throws {
@@ -99,20 +110,13 @@ actor DatabasePool {
99110
}
100111

101112
private func runningDatabases() async throws -> [Database] {
102-
if isRunningInCI() {
103-
// We don't have docker available in CI to probe for running dbs.
104-
// Instead, we have a hard-coded list of dbs we launch in the GH workflow
105-
// file and correspondingly, we hard-code their ports here.
106-
return (6000..<6008).map(Database.init)
107-
} else {
108-
let stdout = try await ShellOut.shellOut(to: .getContainerNames).stdout
109-
return stdout
110-
.components(separatedBy: "\n")
111-
.filter { $0.starts(with: "spi_test_") }
112-
.map { String($0.dropFirst("spi_test_".count)) }
113-
.compactMap(Int.init)
114-
.map(Database.init(port:))
115-
}
113+
let stdout = try await ShellOut.shellOut(to: .getContainerNames).stdout
114+
return stdout
115+
.components(separatedBy: "\n")
116+
.filter { $0.starts(with: "spi_test_") }
117+
.map { String($0.dropFirst("spi_test_".count)) }
118+
.compactMap(Int.init)
119+
.map(Database.init(port:))
116120
}
117121

118122
private func retainDatabase() async throws -> Database {

0 commit comments

Comments
 (0)