Skip to content

Commit 44247df

Browse files
authored
Remove invalid model definition from test (#203)
* Remove invalid model definition from test that doesn't use it to begin with anyway. (Model used @id(key:) with a non-UUID value) * CI update (add concurrency spec, use API breakage workflow, add 5.8 nightly test, enable TSan) * More CI cleanup (reduce redundant jobs), reduce excess thread created in tests * Add some missing imports * Fix hanging tests behavior caused by misuse of Fluent.
1 parent 21b0788 commit 44247df

File tree

7 files changed

+58
-92
lines changed

7 files changed

+58
-92
lines changed

.github/workflows/test.yml

Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
name: test
2+
concurrency:
3+
group: ${{ github.workflow }}-${{ github.ref }}
4+
cancel-in-progress: true
25
on:
36
pull_request: { branches: ['*'] }
47
push: { branches: ['main'] }
@@ -23,7 +26,10 @@ jobs:
2326
# Baseline test run for code coverage stats
2427
codecov:
2528
strategy:
26-
matrix: { dbimage: ['postgres:15'], dbauth: ['scram-sha-256'] }
29+
matrix:
30+
include:
31+
- dbimage: postgres:15
32+
dbauth: scram-sha-256
2733
runs-on: ubuntu-latest
2834
container: swift:5.7-jammy
2935
services:
@@ -69,29 +75,21 @@ jobs:
6975
steps:
7076
- name: Check out package
7177
uses: actions/checkout@v3
72-
with:
73-
fetch-depth: 0
74-
# https://github.com/actions/checkout/issues/766
75-
- name: Mark the workspace as safe
76-
run: git config --global --add safe.directory ${GITHUB_WORKSPACE}
77-
- name: Check for API breaking changes
78-
run: swift package diagnose-api-breaking-changes origin/main
78+
with: { 'fetch-depth': 0 }
79+
- name: Run API breakage check action
80+
uses: vapor/ci/.github/actions/ci-swift-check-api-breakage@reusable-workflows
7981

8082
linux-all:
8183
if: github.event_name == 'pull_request'
8284
strategy:
8385
fail-fast: false
8486
matrix:
85-
dbimage: ['postgres:15', 'postgres:13', 'postgres:11']
86-
swiftver: [
87-
'swift:5.5-bionic', 'swift:5.6-focal', 'swift:5.7-jammy',
88-
'swiftlang/swift:nightly-main-jammy'
89-
]
90-
include: [
91-
{dbimage: 'postgres:15', dbauth: 'scram-sha-256'},
92-
{dbimage: 'postgres:13', dbauth: 'md5'},
93-
{dbimage: 'postgres:11', dbauth: 'trust'}
94-
]
87+
include:
88+
- {dbimage: 'postgres:11', dbauth: 'trust', swiftver: 'swift:5.5-bionic'}
89+
- {dbimage: 'postgres:13', dbauth: 'md5', swiftver: 'swift:5.6-focal'}
90+
- {dbimage: 'postgres:15', dbauth: 'scram-sha-256', swiftver: 'swift:5.7-jammy'}
91+
- {dbimage: 'postgres:15', dbauth: 'scram-sha-256', swiftver: 'swiftlang/swift:nightly-5.8-jammy'}
92+
- {dbimage: 'postgres:15', dbauth: 'scram-sha-256', swiftver: 'swiftlang/swift:nightly-main-jammy'}
9593
container: ${{ matrix.swiftver }}
9694
runs-on: ubuntu-latest
9795
services:
@@ -115,17 +113,18 @@ jobs:
115113
- name: Check out package
116114
uses: actions/checkout@v3
117115
- name: Run all tests
118-
run: swift test
116+
run: swift test --sanitize=thread
119117

120118
macos-all:
121119
if: github.event_name == 'pull_request'
122120
strategy:
123121
fail-fast: false
124122
matrix:
125-
dbimage: ['postgresql@14'] # N.B.: As of this writing, Homebrew didn't have Postgres 15 yet
126-
dbauth: ['scram-sha-256']
127-
macos: ['macos-11', 'macos-12']
128-
xcode: ['latest-stable']
123+
include:
124+
- dbimage: postgresql@14
125+
dbauth: scram-sha-256
126+
macos: macos-12
127+
xcode: latest-stable
129128
runs-on: ${{ matrix.macos }}
130129
env:
131130
LOG_LEVEL: debug
@@ -159,15 +158,14 @@ jobs:
159158
- name: Checkout code
160159
uses: actions/checkout@v3
161160
- name: Run all tests
162-
run: swift test
161+
run: swift test --sanitize=thread
163162

164163
test-exports:
165164
name: Test exports
166165
runs-on: ubuntu-latest
167166
steps:
168167
- name: Check out Vapor
169168
uses: actions/checkout@v3
170-
with:
171-
fetch-depth: 0
169+
with: { 'fetch-depth': 0 }
172170
- name: Build
173171
run: swift build -Xswiftc -DBUILDING_DOCC

Sources/FluentPostgresDriver/Exports.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,4 @@ extension DatabaseID {
1414
public static var psql: DatabaseID {
1515
return .init(string: "psql")
1616
}
17-
}
17+
}

Sources/FluentPostgresDriver/FluentPostgresDatabase.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
import FluentKit
12
import FluentSQL
23
import Logging
34
import PostgresKit
5+
import SQLKit
46

57
struct _FluentPostgresDatabase {
68
let database: PostgresDatabase

Sources/FluentPostgresDriver/PostgresConverterDelegate.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
import FluentKit
12
import FluentSQL
3+
import SQLKit
24

35
struct PostgresConverterDelegate: SQLConverterDelegate {
46
func customDataType(_ dataType: DatabaseSchema.DataType) -> SQLExpression? {

Sources/FluentPostgresDriver/PostgresError+Database.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import FluentKit
12
import FluentSQL
23
import PostgresKit
34

Tests/FluentPostgresDriverTests/FluentPostgresDriverTests.swift

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -60,18 +60,6 @@ final class FluentPostgresDriverTests: XCTestCase {
6060
}
6161

6262
func testBlob() throws {
63-
final class Foo: Model {
64-
static let schema = "foos"
65-
66-
@ID(key: "id")
67-
var id: Int?
68-
69-
@Field(key: "data")
70-
var data: [UInt8]
71-
72-
init() { }
73-
}
74-
7563
struct CreateFoo: Migration {
7664
func prepare(on database: Database) -> EventLoopFuture<Void> {
7765
return database.schema("foos")
@@ -193,8 +181,8 @@ final class FluentPostgresDriverTests: XCTestCase {
193181
try super.setUpWithError()
194182

195183
XCTAssert(isLoggingConfigured)
196-
self.eventLoopGroup = MultiThreadedEventLoopGroup(numberOfThreads: System.coreCount)
197-
self.threadPool = NIOThreadPool(numberOfThreads: System.coreCount)
184+
self.eventLoopGroup = MultiThreadedEventLoopGroup(numberOfThreads: Swift.min(System.coreCount, 2))
185+
self.threadPool = NIOThreadPool(numberOfThreads: 1)
198186
self.dbs = Databases(threadPool: threadPool, on: self.eventLoopGroup)
199187

200188
self.dbs.use(.testPostgres(subconfig: "A"), as: .a)

Tests/FluentPostgresDriverTests/FluentPostgresTransactionControlTests.swift

Lines changed: 26 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -7,77 +7,52 @@ import PostgresKit
77

88
final class FluentPostgresTransactionControlTests: XCTestCase {
99

10-
func testTransactionControl() throws {
11-
try (self.db as! TransactionControlDatabase).beginTransaction().wait()
12-
13-
let todo1 = Todo(title: "Test")
14-
let todo2 = Todo(title: "Test2")
15-
try todo1.save(on: self.db).wait()
16-
try todo2.save(on: self.db).wait()
17-
18-
try (self.db as! TransactionControlDatabase).commitTransaction().wait()
19-
20-
let count = try Todo.query(on: self.db).count().wait()
21-
XCTAssertEqual(count, 2)
22-
}
23-
2410
func testRollback() throws {
25-
try (self.db as! TransactionControlDatabase).beginTransaction().wait()
26-
27-
let todo1 = Todo(title: "Test")
28-
29-
try todo1.save(on: self.db).wait()
30-
31-
let duplicate = Todo(title: "Test")
32-
var errorCaught = false
33-
3411
do {
35-
try duplicate.create(on: self.db).wait()
12+
try self.db.withConnection { db -> EventLoopFuture<Void> in
13+
(db as! TransactionControlDatabase).beginTransaction().flatMap { () -> EventLoopFuture<Void> in
14+
let todo1 = Todo(title: "Test")
15+
return todo1.save(on: db)
16+
}.flatMap { () -> EventLoopFuture<Void> in
17+
let duplicate = Todo(title: "Test")
18+
return duplicate.create(on: db)
19+
.flatMap {
20+
(db as! TransactionControlDatabase).commitTransaction()
21+
}.flatMapError { (e: Error) -> EventLoopFuture<Void> in
22+
return (db as! TransactionControlDatabase).rollbackTransaction()
23+
.flatMap { db.eventLoop.makeFailedFuture(e) }
24+
}
25+
}
26+
}.wait()
27+
XCTFail("Expected error but none was thrown")
28+
} catch PostgresError.server(let e) where e.fields[.sqlState] == "23505" {
29+
// ignore
3630
} catch {
37-
errorCaught = true
38-
try (self.db as! TransactionControlDatabase).rollbackTransaction().wait()
31+
XCTFail("Expected SQL state 23505 but got \(error)")
3932
}
40-
41-
if !errorCaught {
42-
try (self.db as! TransactionControlDatabase).commitTransaction().wait()
43-
}
44-
45-
XCTAssertTrue(errorCaught)
33+
4634
let count2 = try Todo.query(on: self.db).count().wait()
4735
XCTAssertEqual(count2, 0)
4836
}
4937

50-
var benchmarker: FluentBenchmarker {
51-
return .init(databases: self.dbs)
52-
}
5338
var eventLoopGroup: EventLoopGroup!
5439
var threadPool: NIOThreadPool!
5540
var dbs: Databases!
56-
var db: Database {
57-
self.benchmarker.database
58-
}
59-
var postgres: PostgresDatabase {
60-
self.db as! PostgresDatabase
61-
}
41+
var db: Database!
6242

6343
override func setUpWithError() throws {
6444
try super.setUpWithError()
6545

6646
XCTAssert(isLoggingConfigured)
67-
self.eventLoopGroup = MultiThreadedEventLoopGroup(numberOfThreads: 1)
47+
self.eventLoopGroup = MultiThreadedEventLoopGroup(numberOfThreads: Swift.min(System.coreCount, 2))
6848
self.threadPool = NIOThreadPool(numberOfThreads: 1)
6949
self.dbs = Databases(threadPool: threadPool, on: self.eventLoopGroup)
7050

7151
self.dbs.use(.testPostgres(subconfig: "A"), as: .a)
72-
self.dbs.use(.testPostgres(subconfig: "B"), as: .b)
73-
74-
let a = self.dbs.database(.a, logger: Logger(label: "test.fluent.a"), on: self.eventLoopGroup.next())
75-
_ = try (a as! PostgresDatabase).query("drop schema public cascade").wait()
76-
_ = try (a as! PostgresDatabase).query("create schema public").wait()
7752

78-
let b = self.dbs.database(.b, logger: Logger(label: "test.fluent.b"), on: self.eventLoopGroup.next())
79-
_ = try (b as! PostgresDatabase).query("drop schema public cascade").wait()
80-
_ = try (b as! PostgresDatabase).query("create schema public").wait()
53+
self.db = self.dbs.database(.a, logger: Logger(label: "test.fluent.a"), on: self.eventLoopGroup.any())
54+
_ = try (self.db as! PostgresDatabase).query("drop schema public cascade").wait()
55+
_ = try (self.db as! PostgresDatabase).query("create schema public").wait()
8156

8257
try CreateTodo().prepare(on: self.db).wait()
8358
}
@@ -99,7 +74,7 @@ final class FluentPostgresTransactionControlTests: XCTestCase {
9974
@Field(key: "title")
10075
var title: String
10176

102-
init() { }
77+
init() {}
10378
init(title: String) { self.title = title; id = nil }
10479
}
10580

0 commit comments

Comments
 (0)