Skip to content

Commit d3c78cd

Browse files
committed
Don't run transaction middleware in tests
But always insert a new transaction and be sure to finish the transaction as part of the request so we can pick up errors and include it in our timing.
1 parent ec66148 commit d3c78cd

File tree

4 files changed

+31
-32
lines changed

4 files changed

+31
-32
lines changed

src/db.rs

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ pub struct Transaction {
3737
// provide this stable address.
3838
tx: LazyCell<pg::Transaction<'static>>,
3939
slot: LazyCell<Box<PooledConnnection>>,
40-
commit: Cell<bool>,
40+
commit: Cell<Option<bool>>,
4141

4242
// Keep a handle to the app which keeps a handle to the database to ensure
4343
// that this `'static` is indeed at least a little more accurate (in that
@@ -51,7 +51,7 @@ impl Transaction {
5151
app: app,
5252
slot: LazyCell::new(),
5353
tx: LazyCell::new(),
54-
commit: Cell::new(false),
54+
commit: Cell::new(None),
5555
}
5656
}
5757

@@ -83,32 +83,37 @@ impl Transaction {
8383
Ok(tx)
8484
}
8585

86-
pub fn rollback(&self) { self.commit.set(false); }
87-
pub fn commit(&self) { self.commit.set(true); }
86+
pub fn rollback(&self) {
87+
self.commit.set(Some(false));
88+
}
89+
90+
pub fn commit(&self) {
91+
if self.commit.get().is_none() {
92+
self.commit.set(Some(true));
93+
}
94+
}
8895
}
8996

9097
impl Middleware for TransactionMiddleware {
9198
fn before(&self, req: &mut Request) -> Result<(), Box<Error+Send>> {
92-
if !req.extensions().contains::<Transaction>() {
93-
let app = req.app().clone();
94-
req.mut_extensions().insert(Transaction::new(app));
95-
}
99+
let app = req.app().clone();
100+
req.mut_extensions().insert(Transaction::new(app));
96101
Ok(())
97102
}
98103

99104
fn after(&self, req: &mut Request, res: Result<Response, Box<Error+Send>>)
100105
-> Result<Response, Box<Error+Send>> {
101-
if res.is_ok() {
102-
let tx = req.extensions().find::<Transaction>()
103-
.expect("Transaction not present in request");
104-
match tx.tx.borrow() {
105-
Some(transaction) if tx.commit.get() => {
106-
transaction.set_commit();
107-
}
108-
_ => {}
106+
let tx = req.mut_extensions().pop::<Transaction>()
107+
.expect("Transaction not present in request");
108+
if let Some(transaction) = tx.tx.into_inner() {
109+
if res.is_ok() && tx.commit.get() == Some(true) {
110+
transaction.set_commit();
109111
}
112+
try!(transaction.finish().map_err(|e| {
113+
Box::new(e) as Box<Error+Send>
114+
}));
110115
}
111-
return res;
116+
return res
112117
}
113118
}
114119

src/lib.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,9 @@ pub fn middleware(app: Arc<App>) -> MiddlewareBuilder {
140140
m.add(conduit_cookie::SessionMiddleware::new("cargo_session",
141141
env == Env::Production));
142142
m.add(app::AppMiddleware::new(app));
143-
m.add(db::TransactionMiddleware);
143+
if env != Env::Test {
144+
m.add(db::TransactionMiddleware);
145+
}
144146
m.add(user::Middleware);
145147
if env != Env::Test {
146148
m.around(dist::Middleware::new());

src/tests/all.rs

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,6 @@ mod version;
7373
mod team;
7474

7575
fn app() -> (record::Bomb, Arc<App>, conduit_middleware::MiddlewareBuilder) {
76-
struct NoCommit;
7776
static INIT: Once = ONCE_INIT;
7877
git::init();
7978

@@ -95,26 +94,14 @@ fn app() -> (record::Bomb, Arc<App>, conduit_middleware::MiddlewareBuilder) {
9594
INIT.call_once(|| db_setup(&config.db_url));
9695
let app = App::new(&config);
9796
let app = Arc::new(app);
98-
let mut middleware = cargo_registry::middleware(app.clone());
99-
middleware.add(NoCommit);
97+
let middleware = cargo_registry::middleware(app.clone());
10098
return (bomb, app, middleware);
10199

102100
fn db_setup(db: &str) {
103101
let migrate = t!(env::current_exe()).parent().unwrap().join("migrate");
104102
assert!(t!(Command::new(&migrate).env("DATABASE_URL", db)
105103
.status()).success());
106104
}
107-
108-
impl conduit_middleware::Middleware for NoCommit {
109-
fn after(&self, req: &mut Request,
110-
res: Result<conduit::Response, Box<StdError+Send>>)
111-
-> Result<conduit::Response, Box<StdError+Send>> {
112-
req.extensions().find::<db::Transaction>()
113-
.expect("Transaction not present in request")
114-
.rollback();
115-
return res;
116-
}
117-
}
118105
}
119106

120107
fn env(s: &str) -> String {

src/util/lazy_cell.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,9 @@ impl<T> LazyCell<T> {
4646
None => None
4747
}
4848
}
49+
50+
/// Consumes this `LazyCell`, returning the underlying value.
51+
pub fn into_inner(self) -> Option<T> {
52+
self.inner.into_inner()
53+
}
4954
}

0 commit comments

Comments
 (0)