New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Shared Test Transactions #900

Open
mcasper opened this Issue May 11, 2017 · 2 comments

Comments

Projects
None yet
3 participants
@mcasper
Collaborator

mcasper commented May 11, 2017

When running tests in a multithreaded environment (like running tests against a webserver), currently the only way to ensure that your test database is clean across threads is to drop/truncate for every test. This works fine, but is tedious, and it would be nicer if connections and their test transactions could be shared, so that data is all localized and automatically cleaned up.

One way to accomplish this that I've been thinking about is to implement Sync for connections and Connection for Arc<T: Connection>, allowing pointers to a shared connection to be passed around and data within a test transaction to be visible. I know we've previously talked about not wanting to implement Sync for connections because they're not meant to be passed around, which is why I'd want to hide this away behind a tests or testing feature, included only in [dev-dependencies].

Some changes would be necessary in other supporting libraries (like r2d2/r2d2-diesel for example), but could lead to code like the following for testing (example using Rocket):

#[test]
fn test_index() {
  let pool = database::pool();
  let conn = pool.get().unwrap();
  let rocket_app = app::launch(pool);

  conn.begin_test_transaction().unwrap();

  // insert data into connection
  // build and send request to app
  // assert on response
}

If we like this, I'm happy to take on the feature work

@sgrif

This comment has been minimized.

Member

sgrif commented May 13, 2017

The solution I've been using in crates.io is to set the connection pool size to 1, but I agree that it'd be nice to make it easier. I do not want to implement Sync on connections, as they are explicitly not thread safe and several of them rely on being guaranteed to not be concurrently used across threads. We'd need a mutex for sure.

@sgrif sgrif added the enhancement label May 13, 2017

@fluffysquirrels

This comment has been minimized.

fluffysquirrels commented Aug 28, 2017

Arc<Mutex<T: Send>> already implements Sync automagically (Mutex docs) and Connection requires Send. So can you just wrap transactions in Arc<Mutex<T>> in your app?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment