Repository navigation
Disconnect every database connection when the test environment is torn down - #62471
Conversation
|
Thanks for submitting a PR! Note that draft PRs are not reviewed. If you would like a review, please mark your pull request as ready for review in the GitHub user interface. Pull requests that are abandoned in draft may be closed due to inactivity. |
caf0a97 to
c06c895
Compare
|
@Outsidaz that might solve the 2nd issue you reported in mongodb/laravel-mongodb#3665 (comment) |
|
Tested this on 13.x (
The placement also looks right: it runs after |
|
@raghavji2050 it would be helpful to test this patch on your real project if you use multiple database connections and experience issues. |
|
I don't have a real project with multiple connections, so I tried it on a fresh app with sqlite, MySQL and Postgres. Without the patch, 34 MySQL and 34 Postgres connections stayed open until PHPUnit exited. With it they're closed after every test, and all 52 tests still pass, including |
…wn (#62600) Mocking the DB facade with DB::shouldReceive() swaps the db binding for a Mockery mock of DatabaseManager. The teardown added in #62471 then calls getConnections() on the mock, and tests fail with BadMethodCallException although their own assertions pass. Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Test suites that boot one application per test keep the database connections of every application open until the cycle collector runs. The database manager and its connections form a reference cycle, and neither
Application::flush()nor the test teardown closes the handles. A parallel suite of a few thousand tests can leave thousands of connections open at once, and the server refuses new ones.RefreshDatabaseandDatabaseTransactionsalready disconnect the connections they transact, but only forconnectionsToTransact(), which defaults to the default connection. Every other connection stays open.This disconnects every resolved connection of the application just before it is flushed, after the
beforeApplicationDestroyedcallbacks so they can still query.