-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
bug: only use one db connection at any point in time
I've fixed the implementation of `create()` to only use a single database connection at any point in time — previously, as @ebabani pointed out in #5, pgtestdb used 2 simultaneous connections. I was able to verify the behavior by adding some `time.Sleep` calls in the library and then connecting to the test database while running some tests: <details> <summary> Now, when you call `pgtestdb.New()` or `pgtestdb.Custom(0`, pgtestdb connects, ensures the template exists, creates the instance db, then cleans everything up like before, but only uses a single connection to the server at any point in time.</summary> * Here, you see a single active connection to the `postgres` database to start (that's me connected in `psql`.) ```psql postgres=# SELECT datname, sum(numbackends) FROM pg_stat_database WHERE datname is not null group by 1; datname | sum ---------------------------------------------+----- postgres | 1 template0 | 0 template1 | 0 ``` * I start running a test. `pgtestdb` connects to the `postgres` database as well, this is the `baseDB` in the code. It uses this connection to create the template database, run migrations, and then create the instance database for the specific test. ```psql postgres=# SELECT datname, sum(numbackends) FROM pg_stat_database WHERE datname is not null group by 1; datname | sum ---------------------------------------------+----- postgres | 2 template0 | 0 template1 | 0 ``` * `pgtestdb` connects to the instance database, then hands it to the test. this is the `db := pgtestdb.New(...)` connection. ```psql postgres=# SELECT datname, sum(numbackends) FROM pg_stat_database WHERE datname is not null group by 1; datname | sum -----------------------------------------------------------------+----- template1 | 0 testdb_tpl_ed8ae75db1176559951eadb85d6be0db_inst_8ed74b29d41d8c | 1 postgres | 1 testdb_tpl_ed8ae75db1176559951eadb85d6be0db | 0 template0 | 0 ``` * The test case finishes, and `pgtestdb` closes the connection to the instance, connects back to the `baseDB`, and deletes the instance. ```psql postgres=# SELECT datname, sum(numbackends) FROM pg_stat_database WHERE datname is not null group by 1; datname | sum ---------------------------------------------+----- postgres | 2 testdb_tpl_ed8ae75db1176559951eadb85d6be0db | 0 template0 | 0 template1 | 0 ``` * `pgtestdb` closes its connection, the test is entirely finished running. ```psql postgres=# SELECT datname, sum(numbackends) FROM pg_stat_database WHERE datname is not null group by 1; datname | sum ---------------------------------------------+----- postgres | 1 testdb_tpl_ed8ae75db1176559951eadb85d6be0db | 0 template0 | 0 template1 | 0 ``` </details> In my testing, re-connecting to the database adds some slight overhead per test, but it's fundamentally more correct, and the database connection time should be dominated by the time taken during each test so overall this is worth the performance impact because it allows you to double the amount of tests you are running at once! I also updated the FAQ to explain the problem, show some example error messages, and provide some ideas for how to work around it. Co-authored-by: Peter Downs <github@peterdowns.com>
- Loading branch information
1 parent
7374056
commit 15a8a8a
Showing
9 changed files
with
80 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
v0.0.13 | ||
v0.0.14 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters