Files
synapse/tests
Eric EastwoodandGitHub 93f4ea43c7 Fix tests that use homeserver_to_use=GenericWorkerServer not being able to be run standalone (#20017)
*Spawning from
https://github.com/element-hq/synapse/pull/19057#discussion_r2427537811,*

Fix tests that use `homeserver_to_use=GenericWorkerServer` not being
able to be run standalone

Fix https://github.com/element-hq/synapse/issues/15671 (previously
https://github.com/matrix-org/synapse/issues/15671)

Before this change:

```shell
$ poetry run trial tests.storage.test_rollback_worker.WorkerSchemaTests.test_rolling_back
tests.storage.test_rollback_worker
  WorkerSchemaTests
    test_rolling_back ...                                               [ERROR]

===============================================================================
[ERROR]
Traceback (most recent call last):
  File "synapse/tests/unittest.py", line 129, in new
    return code(orig, *args, **kwargs)
  File "synapse/tests/unittest.py", line 223, in setUp
    return orig()
  File "synapse/tests/unittest.py", line 398, in setUp
    self.hs = self.make_homeserver(self.reactor, self.clock)
  File "synapse/tests/storage/test_rollback_worker.py", line 54, in make_homeserver
    hs = self.setup_test_homeserver(homeserver_to_use=GenericWorkerServer)
  File "synapse/tests/unittest.py", line 669, in setup_test_homeserver
    hs = setup_test_homeserver(
  File "synapse/tests/server.py", line 1260, in setup_test_homeserver
    prepare_database(
  File "synapse/synapse/storage/prepare_database.py", line 167, in prepare_database
    raise UpgradeDatabaseException(EMPTY_DATABASE_ON_WORKER_ERROR)
synapse.storage.prepare_database.UpgradeDatabaseException: Uninitialised database: run the main synapse process to prepare the database schema before starting worker processes.

tests.storage.test_rollback_worker.WorkerSchemaTests.test_rolling_back
-------------------------------------------------------------------------------
Ran 1 tests in 0.034s

FAILED (errors=1)
```


### What was the problem before?

[`PREPPED_SQLITE_DB_CONN`](https://github.com/element-hq/synapse/blob/1a1af7b622f219ba0f2501709298caa230ad3912/tests/server.py#L1247-L1262)
is a process global and shared between all tests. Whichever test first
calls `setup_test_homeserver(...)` builds the template database for the
whole trial run.

`prepare_database(...)` has a built-in check to refuse upgrading the
database ["to avoid multiple workers doing it at
once."](https://github.com/element-hq/synapse/blob/1a1af7b622f219ba0f2501709298caa230ad3912/synapse/storage/prepare_database.py#L164-L167)
and throw `UpgradeDatabaseException`.

This means that if we happen to first run a test that uses a worker
(`homeserver_to_use=GenericWorkerServer`), `prepare_database(...)` will
just throw its `UpgradeDatabaseException`. And since
`PREPPED_SQLITE_DB_CONN` is assigned before `prepare_database(...)`, it
will never try to prepare again and the rest of the tests will fail.
2026-08-10 16:34:56 -05:00
..
2026-06-02 11:05:38 +01:00