2

在编写我的测试时,我希望能够将连接注入到请求中,以便我可以将整个测试用例包装在一个事务中(即使测试用例中有多个请求)。

我尝试使用BeforeMiddleware可以在我的测试用例中链接以插入连接的方法来执行此操作,例如:

pub type DatabaseConnection = PooledConnection<ConnectionManager<PgConnection>>;

pub struct DatabaseOverride {
    conn: DatabaseConnection,
}

impl BeforeMiddleware for DatabaseOverride {
    fn before(&self, req: &mut Request) -> IronResult<()> {
        req.extensions_mut().entry::<DatabaseOverride>().or_insert(self.conn);
        Ok(())
    }
}

但是,我在尝试执行此操作时遇到编译错误:

error: the trait bound `std::rc::Rc<diesel::pg::connection::raw::RawConnection>: std::marker::Sync` is not satisfied [E0277]
impl BeforeMiddleware for DatabaseOverride {
     ^~~~~~~~~~~~~~~~
help: run `rustc --explain E0277` to see a detailed explanation
note: `std::rc::Rc<diesel::pg::connection::raw::RawConnection>` cannot be shared between threads safely
note: required because it appears within the type `diesel::pg::PgConnection`
note: required because it appears within the type `r2d2::Conn<diesel::pg::PgConnection>`
note: required because it appears within the type `std::option::Option<r2d2::Conn<diesel::pg::PgConnection>>`
note: required because it appears within the type `r2d2::PooledConnection<r2d2_diesel::ConnectionManager<diesel::pg::PgConnection>>`
note: required because it appears within the type `utility::db::DatabaseOverride`
note: required by `iron::BeforeMiddleware`

error: the trait bound `std::cell::Cell<i32>: std::marker::Sync` is not satisfied [E0277]
impl BeforeMiddleware for DatabaseOverride {
     ^~~~~~~~~~~~~~~~
help: run `rustc --explain E0277` to see a detailed explanation
note: `std::cell::Cell<i32>` cannot be shared between threads safely
note: required because it appears within the type `diesel::pg::PgConnection`
note: required because it appears within the type `r2d2::Conn<diesel::pg::PgConnection>`
note: required because it appears within the type `std::option::Option<r2d2::Conn<diesel::pg::PgConnection>>`
note: required because it appears within the type `r2d2::PooledConnection<r2d2_diesel::ConnectionManager<diesel::pg::PgConnection>>`
note: required because it appears within the type `utility::db::DatabaseOverride`
note: required by `iron::BeforeMiddleware`

有没有办法通过柴油机的连接来解决这个问题?我在 Github 上找到了几个使用pg板条箱执行此操作的示例,但我想继续使用柴油。

4

2 回答 2

7

这个答案肯定会解决问题,但它不是最优的。如前所述,您不能共享单个连接,因为它不是线程安全的。但是,虽然将其包装在 aMutex中使其成为线程安全的,但它会强制所有服务器线程使用单个连接。相反,您想使用连接池。

您可以使用r2d2r2d2-diesel板条箱完成此操作。这将根据需要建立多个连接,并在可能的情况下以线程安全的方式重用它们。

于 2016-08-17T10:55:28.890 回答
3

由于没有为我提供足够的代码来重现您的问题,我做了这个:

use std::cell::Cell;

trait Middleware: Sync {}

struct Unsharable(Cell<bool>);

impl Middleware for Unsharable {}

fn main() {}

有同样的错误:

error: the trait bound `std::cell::Cell<bool>: std::marker::Sync` is not satisfied [E0277]
impl Middleware for Unsharable {}
     ^~~~~~~~~~
help: run `rustc --explain E0277` to see a detailed explanation
note: `std::cell::Cell<bool>` cannot be shared between threads safely
note: required because it appears within the type `Unsharable`
note: required by `Middleware`

您可以通过更改类型以使其跨线程兼容来解决问题:

use std::sync::Mutex;

struct Sharable(Mutex<Unsharable>);

impl Middleware for Sharable {}

请注意,Rust为您做了一件非常好的事情:它阻止您使用在多个线程中调用的不安全类型。


在编写我的测试时,我希望能够将连接注入到请求中,以便我可以将整个测试用例包装在一个事务中(即使测试用例中有多个请求)。

我建议架构更改可能会更好。将“Web 框架”的域与“数据库”分开。《Growing Object-Oriented Software, Guided by Tests》(一本强烈推荐的书)的作者提倡这种风格。

拆开你的代码,这样有一个方法可以简单地接受某种可以开始/结束事务的类型,在那里编写有趣的东西,并彻底测试它。然后在 web 层有足够的胶水代码来创建一个事务对象,然后调用下一层。

于 2016-06-24T13:36:34.093 回答