Skip to content

Conversation

finestructure
Copy link
Member

@finestructure finestructure commented Feb 4, 2025

⚠️ Not to be merged ⚠️

This works in principle but currently has major drawbacks.

Locally, the tests run a lot slower, because each test spins up its own PG docker container (and then sets up the schema). The serial tests instead re-use the container and only reset the schema, which is a lot faster.

We can fix this (and I've done the same in the past in a python project) by spinning up a pool of PG containers at the start of the test and then scheduling tests against that pool just like we do serially. We just need to mutex each PG instance.

However, that's probably not going to be possible with XCTest based testing, because from initial testing it looks like each test is actually launched as a separate process. I.e. unless we manage the mutex in the file system or somewhere else centrally we won't be able to record which test is using which db. I was planning to do this simply via an actor or a Mutex guarded entity but each process would have their own copy.

I believe the new Swift Testing does not have this problem but I'll run a test with both to see which one would work.

Unfortunately that may mean that we will need to move the tests to Swift Testing to be able to run them in parallel. However, thanks to swift-testing-revolutionary this isn't such a huge deal actually. I've done this for the AnalyzerTests suite and it is mostly an automatic conversion with some touch up.

@cla-bot cla-bot bot added the cla-signed label Feb 4, 2025
@finestructure
Copy link
Member Author

Ok, we will definitely have to move to Swift Testing for this to work.

I've set up two simple tests that increment an int and sleep for 2s. It's clear that with XCTest these are all independent counts, they all show a count of 1:

❯ swift build --build-tests && time swift test --filter ParallelXCTest --parallel
Building for debugging...
[2/2] Write swift-version-5BDAB9E9C0126B9D.txt
Build complete! (0.60s)
Building for debugging...
[2/2] Write swift-version-5BDAB9E9C0126B9D.txt
Build complete! (0.57s)
[5/5] Testing AppTests.ParallelXCTest/test_1
Test Suite 'Selected tests' started at 2025-02-05 09:34:10.376.
Test Suite 'SPI-ServerPackageTests.xctest' started at 2025-02-05 09:34:10.379.
Test Suite 'ParallelXCTest' started at 2025-02-05 09:34:10.379.
Test Case '-[AppTests.ParallelXCTest test_4]' started.
/Users/sas/Projects/SPI/spi-server/Tests/AppTests/ParallelXCTest.swift:42: error: -[AppTests.ParallelXCTest test_4] : XCTAssertEqual failed: ("1") is not equal to ("0")
Test Case '-[AppTests.ParallelXCTest test_4]' failed (3.560 seconds).
Test Suite 'ParallelXCTest' failed at 2025-02-05 09:34:13.939.
	 Executed 1 test, with 1 failure (0 unexpected) in 3.560 (3.560) seconds
Test Suite 'SPI-ServerPackageTests.xctest' failed at 2025-02-05 09:34:13.939.
	 Executed 1 test, with 1 failure (0 unexpected) in 3.560 (3.560) seconds
Test Suite 'Selected tests' failed at 2025-02-05 09:34:13.939.
	 Executed 1 test, with 1 failure (0 unexpected) in 3.560 (3.563) seconds
⚠️ count: 1

Test Suite 'Selected tests' started at 2025-02-05 09:34:10.376.
Test Suite 'SPI-ServerPackageTests.xctest' started at 2025-02-05 09:34:10.379.
Test Suite 'ParallelXCTest' started at 2025-02-05 09:34:10.379.
Test Case '-[AppTests.ParallelXCTest test_3]' started.
/Users/sas/Projects/SPI/spi-server/Tests/AppTests/ParallelXCTest.swift:35: error: -[AppTests.ParallelXCTest test_3] : XCTAssertEqual failed: ("1") is not equal to ("0")
Test Case '-[AppTests.ParallelXCTest test_3]' failed (3.559 seconds).
Test Suite 'ParallelXCTest' failed at 2025-02-05 09:34:13.939.
	 Executed 1 test, with 1 failure (0 unexpected) in 3.559 (3.559) seconds
Test Suite 'SPI-ServerPackageTests.xctest' failed at 2025-02-05 09:34:13.939.
	 Executed 1 test, with 1 failure (0 unexpected) in 3.559 (3.559) seconds
Test Suite 'Selected tests' failed at 2025-02-05 09:34:13.939.
	 Executed 1 test, with 1 failure (0 unexpected) in 3.559 (3.562) seconds
⚠️ count: 1

Test Suite 'Selected tests' started at 2025-02-05 09:34:10.376.
Test Suite 'SPI-ServerPackageTests.xctest' started at 2025-02-05 09:34:10.379.
Test Suite 'ParallelXCTest' started at 2025-02-05 09:34:10.379.
Test Case '-[AppTests.ParallelXCTest test_2]' started.
/Users/sas/Projects/SPI/spi-server/Tests/AppTests/ParallelXCTest.swift:28: error: -[AppTests.ParallelXCTest test_2] : XCTAssertEqual failed: ("1") is not equal to ("0")
Test Case '-[AppTests.ParallelXCTest test_2]' failed (3.561 seconds).
Test Suite 'ParallelXCTest' failed at 2025-02-05 09:34:13.940.
	 Executed 1 test, with 1 failure (0 unexpected) in 3.561 (3.561) seconds
Test Suite 'SPI-ServerPackageTests.xctest' failed at 2025-02-05 09:34:13.940.
	 Executed 1 test, with 1 failure (0 unexpected) in 3.561 (3.561) seconds
Test Suite 'Selected tests' failed at 2025-02-05 09:34:13.940.
	 Executed 1 test, with 1 failure (0 unexpected) in 3.561 (3.564) seconds
⚠️ count: 1

Test Suite 'Selected tests' started at 2025-02-05 09:34:10.376.
Test Suite 'SPI-ServerPackageTests.xctest' started at 2025-02-05 09:34:10.379.
Test Suite 'ParallelXCTest' started at 2025-02-05 09:34:10.379.
Test Case '-[AppTests.ParallelXCTest test_0]' started.
/Users/sas/Projects/SPI/spi-server/Tests/AppTests/ParallelXCTest.swift:14: error: -[AppTests.ParallelXCTest test_0] : XCTAssertEqual failed: ("1") is not equal to ("0")
Test Case '-[AppTests.ParallelXCTest test_0]' failed (3.559 seconds).
Test Suite 'ParallelXCTest' failed at 2025-02-05 09:34:13.939.
	 Executed 1 test, with 1 failure (0 unexpected) in 3.559 (3.559) seconds
Test Suite 'SPI-ServerPackageTests.xctest' failed at 2025-02-05 09:34:13.939.
	 Executed 1 test, with 1 failure (0 unexpected) in 3.559 (3.559) seconds
Test Suite 'Selected tests' failed at 2025-02-05 09:34:13.939.
	 Executed 1 test, with 1 failure (0 unexpected) in 3.559 (3.562) seconds
⚠️ count: 1

Test Suite 'Selected tests' started at 2025-02-05 09:34:10.376.
Test Suite 'SPI-ServerPackageTests.xctest' started at 2025-02-05 09:34:10.379.
Test Suite 'ParallelXCTest' started at 2025-02-05 09:34:10.379.
Test Case '-[AppTests.ParallelXCTest test_1]' started.
/Users/sas/Projects/SPI/spi-server/Tests/AppTests/ParallelXCTest.swift:21: error: -[AppTests.ParallelXCTest test_1] : XCTAssertEqual failed: ("1") is not equal to ("0")
Test Case '-[AppTests.ParallelXCTest test_1]' failed (3.560 seconds).
Test Suite 'ParallelXCTest' failed at 2025-02-05 09:34:13.939.
	 Executed 1 test, with 1 failure (0 unexpected) in 3.560 (3.560) seconds
Test Suite 'SPI-ServerPackageTests.xctest' failed at 2025-02-05 09:34:13.939.
	 Executed 1 test, with 1 failure (0 unexpected) in 3.560 (3.560) seconds
Test Suite 'Selected tests' failed at 2025-02-05 09:34:13.939.
	 Executed 1 test, with 1 failure (0 unexpected) in 3.560 (3.563) seconds
⚠️ count: 1

(I've made the tests to fail so the output is logged to the console. Otherwise it doesn't print anything.)

With Swift Testing this behaves as expected:

❯ swift build --build-tests && time swift test --filter ParallelSwiftTesting --parallel
Building for debugging...
[8/8] Linking SPI-ServerPackageTests
Build complete! (8.81s)
Building for debugging...
[2/2] Write swift-version-5BDAB9E9C0126B9D.txt
Build complete! (0.61s)
􀟈  Test run started.
􀄵  Testing Library Version: 102 (arm64e-apple-macos13.0)
􀟈  Suite ParallelSwiftTesting started.
􀟈  Test test_3() started.
􀟈  Test test_4() started.
􀟈  Test test_1() started.
􀟈  Test test_0() started.
􀟈  Test test_2() started.
⚠️ count: 2
⚠️ count: 3
⚠️ count: 4
⚠️ count: 5
⚠️ count: 3

@finestructure
Copy link
Member Author

Closing this for now, follow-up work is in progress.

@finestructure finestructure deleted the parallelise-tests branch March 17, 2025 12:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant