-
Notifications
You must be signed in to change notification settings - Fork 92
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow River client to be created with nil database pool
I realized as I was working on test helpers and thinking about transactions that having to send a River client a database pool in a test suite would be _very_ inconvenient. We make a database pool on a test database relatively easily available in River (I say "relatively" because the test suite still cannot support `-p > 1`), but for many applications that won't be the case. At my work for example, sending River a database pool in tests would create the risk of tainting the test database which would be very undesirable. Here, we add the capacity for a River client/driver to be initialized with a `nil` database pool. It works like this: * `Start` is not available and errors if call. * The non-transactional variants of `Insert`/`InsertMany` are not available and error if called. * `InsertTx`/`InsertManyTx` will continue to work happily and insert jobs as they always would. So when testing something like an API endpoint, users would create and inject a test transaction, make sure to always use `InsertTx` and `InsertManyTx`, then reuse the same test transaction with the test helpers to verify the inserts. They assert only that the jobs are inserted and not that the worker work is performed. That's enough assuming that River works (and we hope it does) and will help keep their test suite much faster because not every job needs to actually round trip through its worker. There could even be cases where this would be useful in prod. On the API side you might make sure to initialize the client with a `nil` database pool so that an accidental job insertion outside of a transaction (i.e. accidental call to `Insert` instead of `InsertTx`) will fail, and hopefully be caught in the test suite. If I was using River in a project, that's how I'd do it.
- Loading branch information
Showing
9 changed files
with
149 additions
and
11 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package riverpgxv5 | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/jackc/pgx/v5/pgxpool" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestNew(t *testing.T) { | ||
t.Run("AllowsNilDatabasePool", func(t *testing.T) { | ||
dbPool := &pgxpool.Pool{} | ||
driver := New(dbPool) | ||
require.Equal(t, dbPool, driver.dbPool) | ||
}) | ||
|
||
t.Run("AllowsNilDatabasePool", func(t *testing.T) { | ||
driver := New(nil) | ||
require.Nil(t, driver.dbPool) | ||
}) | ||
} |