Comments (9)
I played with the code a little, and it seems to be that the critical bit is being able to inject a connection from the test code to the pool – ensuring that the test uses the one true test connection should be just a matter of setting the pool size to 1, right? And getting that same connection after the code under test has run is just a matter of getting
that one connection from a cloned pool.
The injection can be done at least in two ways – either Pool
could support injecting connections to the pool, or then every 3rd party ManageConnection
implementation could provide a way to do that ad hoc inside the connect
method.
I'd prefer seeing a general injection method, since it would unify the r2d2 ecosystem.
from r2d2.
Another thing besides the #41 would be "snatching" a connection from the pool, which would allow reusing the same test connection over multiple setups and teardowns of r2d2
. @sfackler I'm interested to know what do you think about this stuff?
from r2d2.
The types would differ either way since the pool is parameterized over the manager type, right?
from r2d2.
That's right, I didn't think it through when I was writing that issue. Check out the PR however, it solves the problem without changing any types.
from r2d2.
I'd be a bit worried that the approach in the PR is fragile to future changes - there aren't really any strong guarantees that the connection won't be replaced.
There is a more robust setup here, but it involves some Postgres Deep Magicks. Specifically, you can have multiple connections in the same transaction context! You can use a connection customizer to set up the pool's connections in the right way, and use a separate connection to do the transaction management.
It'd look something like this:
let conn = Connection::new(url).unwrap();
conn.execute("BEGIN ISOLATION LEVEL SERIALIZABLE", &[]).unwrap();
let mut rows = conn.query("SELECT pg_export_snapshot()", &[]).unwrap();
let id: String = rows.get(0).get(0);
struct SharedTransactionCustomizer(String);
impl CustomizeConnection for SharedTransactionCustomizer {
fn on_acquire(&self, conn: &mut Connection) -> Result<(), Error> {
conn.batch_execute(format!("BEGIN ISOLATION LEVEL SERIALIZABLE; SET TRANSACTION SNAPSHOT '{}';", self.0))
}
}
Though you should note that any transaction-based approach like this will fall over if any of your application logic ever wants to start one of its own transactions.
from r2d2.
Oh, thanks for the pointer! And yeah, I get your worries about the non-robustness of the change.
I fear that the discoverability of that solution of yours is low. It would be nice to have an easily accessible and documented way in the library itself to encourage testing – for example, I had to really push myself to write tests because it wasn't obvious for me how to do that in the first place.
Do you have any ideas how to improve the situation? Do you think the idea behind my PR could be incorporated if the implementation was more robust?
from r2d2.
I'd be hesitant to recommend a transaction-based setup in general(whether it be the single-connection pool or the shared snapshot approach) because of the issue with application-level transactions. If your test infrastructure forbids those, you're in a pretty bad place.
Ideally your tests would be designed in a way where they can be run in a "dirty" database in parallel, or you could manually serialize them with a mutex or something like that. Hopefully sometime soon we'll be able to create custom test harnesses so we could make one that supports before test/after test functions and sequential execution to more robustly support these kinds of integration tests.
from r2d2.
This was solved by your suggestion, thanks. I'm going to close this.
from r2d2.
If someone comes across this, also check out: diesel-rs/diesel#2733
Might not be the best solution, and has problems with reusing connections, but it works for me.
I don't know if r2d2 has something better for testing by now. If so, let me know.
from r2d2.
Related Issues (20)
- roadmap to v1.0.0? HOT 3
- Will r2d2 create more connections than max_size when using builder()? HOT 3
- pool.get() returns Error(None) -- best debugging strategy? HOT 1
- Is there a way to not keep idle connections? HOT 8
- `get_timeout` exceeds timeout HOT 11
- Can be used in the future such as tokio, async_std? HOT 1
- Use `compare_exchange` or `compare_exchange_weak` instead
- Is there a way to drop or close an actual connection when I'm using PooldConnection? HOT 4
- "Named" connections HOT 3
- Why example use `let pool = pool.clone()` in a for loop ? HOT 2
- Document relationship between `min_idle`, `idle_timeout` and `max_lifetime` HOT 1
- How to set timezone when linking HOT 1
- Release? HOT 1
- Allow hooking on connection get-from-pool and release-to-pool HOT 8
- Using behind proxychains HOT 1
- Seg fault with Postgres HOT 14
- Remove parking_lot dependency now that std's Mutex is faster than before HOT 3
- [Bug] CI test failure on arm HOT 4
- r2d2::Error should derive PartialEq and Clone HOT 4
- Pool building freezes when connections drop during initial setup HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from r2d2.