#pool #parallel #connection-pool #integration-tests #web

db-pool

A thread-safe database pool for running database-tied integration tests in parallel

4 releases

0.2.1 Apr 14, 2024
0.2.0 Apr 13, 2024
0.1.4 Apr 8, 2024
0.1.3 Mar 24, 2024

#767 in Testing

MIT license

375KB
8K SLoC

db-pool

unsafe forbidden Book Status Crates.io

Book - Docs

A thread-safe database pool for running database-tied integration tests in parallel with:

  • Easy setup
  • Proper isolation
  • Automatic creation, reuse, and cleanup
  • Async support

Description

Rather than simply providing a database connection pool that allows multiple connections to the same database, db-pool maintains a pool of separate isolated databases in order to allow running database-tied tests in parallel. It also handles the lifecycles of those databases: whenever you pick a database out of the pool, you can be sure that the database is clean and ready to be used, and that no other tests are connected to the database you are using in any one test.

Motivation

When running tests against a database-tied service, such as a web server, a test database is generally used. However, this comes with its own set of difficulties:

  1. The database has to be either (a) dropped and re-created or (b) cleaned before every test.
  2. Tests have to run serially in order to avoid cross-contamination.

This leads to several issues when running tests serially:

  • Test setup and teardown is now required.
  • Dropping and creating a database from scratch can be expensive.
  • Cleaning a database instead of dropping and re-creating one requires careful execution of dialect-specific statements.

When switching to parallel execution of tests, even more difficulties arise:

  • Creating and dropping a database for each test can be expensive.
  • Sharing temporary databases across tests requires:
    • isolating databases in concurrent use
    • cleaning each database before reuse by a subsequent test
    • restricting user privileges to prevent schema modification by rogue tests
    • dropping temporary databases before or after a test run to reduce clutter

db-pool takes care of all of these concerns while supporting multiple database types, backends, and connection pools.

Databases

  • MySQL (MariaDB)
  • PostgreSQL

Backends & Pools

Sync

Backend Pool Feature
diesel/mysql r2d2 diesel-mysql
diesel/postgres r2d2 diesel-postgres
mysql r2d2 mysql
postgres r2d2 postgres

Async

Backend Pool Features
diesel-async/mysql bb8 diesel-async-mysql, diesel-async-bb8
diesel-async/mysql mobc diesel-async-mysql, diesel-async-mobc
diesel-async/postgres bb8 diesel-async-postgres, diesel-async-bb8
diesel-async/postgres mobc diesel-async-postgres, diesel-async-mobc
sea-orm/sqlx-mysql sqlx sea-orm-mysql
sea-orm/sqlx-postgres sqlx sea-orm-postgres
sqlx/mysql sqlx sqlx-mysql
sqlx/postgres sqlx sqlx-postgres
tokio-postgres bb8 tokio-postgres, tokio-postgres-bb8
tokio-postgres mobc tokio-postgres, tokio-postgres-mobc

Dependencies

~0.6–17MB
~234K SLoC