1212// See the License for the specific language governing permissions and
1313// limitations under the License.
1414
15- import Testing
16-
1715@testable import App
1816
1917import Fluent
18+ import FluentPostgresDriver
19+ import NIOConcurrencyHelpers
2020import SPIManifest
21- @preconcurrency import ShellOut
2221import SnapshotTesting
22+ import Testing
2323import Vapor
24- import NIOConcurrencyHelpers
24+
25+ @preconcurrency import ShellOut
26+
2527
2628extension ShellOutCommand {
2729 static func launchDB( port: Int ) -> ShellOutCommand {
@@ -46,9 +48,9 @@ extension ShellOutCommand {
4648}
4749
4850
49- private func setupApp( _ environment: Environment ) async throws -> Application {
51+ private func setupApp( _ environment: Environment , databasePort : Int ) async throws -> Application {
5052 let app = try await Application . make ( environment)
51- try await configure ( app)
53+ try await configure ( app, databasePort : databasePort )
5254
5355 // Silence app logging
5456 app. logger = . init( label: " noop " ) { _ in SwiftLogNoOpLogHandler ( ) }
@@ -59,11 +61,13 @@ private func setupApp(_ environment: Environment) async throws -> Application {
5961 return app
6062}
6163
64+
6265private func relaunchDB( on port: Int ) async throws {
6366 _ = try ? await ShellOut . shellOut ( to: . removeDB( port: port) )
6467 try await ShellOut . shellOut ( to: . launchDB( port: port) )
6568}
6669
70+
6771private func setupDB( on port: Int , app: Application ) async throws {
6872 let deadline = Date . now. addingTimeInterval ( 1 )
6973 var dbIsReady = false
@@ -76,12 +80,19 @@ private func setupDB(on port: Int, app: Application) async throws {
7680 try #require( dbIsReady)
7781}
7882
83+
84+ private let dbIndex = ActorIsolated ( 0 )
85+
86+
7987private func withApp( _ environment: Environment , _ test: ( Application , CapturingLogger ) async throws -> Void ) async throws {
80- let port = 5432
81- setenv ( " DATABASE_PORT " , " \( port) " , 1 )
88+ let dbOffset = await dbIndex. withValue { index in
89+ index = index + 1
90+ return index
91+ }
92+ let port = 10_000 + dbOffset
8293 try await relaunchDB ( on: port)
8394
84- let app = try await setupApp ( environment)
95+ let app = try await setupApp ( environment, databasePort : port )
8596
8697 let logger = CapturingLogger ( )
8798 Current . setLogger ( . init( label: " test " , factory: { _ in logger } ) )
0 commit comments