On Sat, May 15, 2021 at 10:48 AM Janko Marohnić <[email protected]>
wrote:

> Hello,
>
> I'm wondering what's the recommended Sequel test setup when using Capybara
> with a JS driver that runs the Rack app server in a background thread.
>
> Ideally, I would like tests to run inside a transaction, in which case the
> DB connection would have to be shared between the test thread and the
> background app. This is what I came up with:
>
> DB = Sequel.connect("...", max_connections: 1)
>
> RSpec.configure do |c|
>   c.around(:each) do |spec|
>     DB.transaction(rollback: :always, auto_savepoint: true) do
>       DB.pool.send(:release, Thread.current) # allow the app to use the
> connection
>       spec.run
>     end
>   end
> end
>
> Does that look ok? If yes, do you think we could add it to the testing
> guide?
>

Releasing a connection from the pool while still using the connection for
the transaction is not acceptable.  There is a reason #release is private,
it should not be called directly.

Instead of max_connections: 1, couldn't you use single_threaded: true, so
the single threaded pool is used and you don't have to worry about
connection checkouts?  Note that that only fixes the API issues, it doesn't
fix the general issue of multiple threads accessing a connection
simultaneously.


> The alternative would be not having a transaction, but rather
> truncating/deleting data after each test run using something like
> DatabaseCleaner, but I guess that would have a too significant performance
> impact (I don't have a large enough app to verify that, though).
>

That's the approach I would recommend for tests that require multiple
threads.

Thanks,
Jeremy

-- 
You received this message because you are subscribed to the Google Groups 
"sequel-talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/sequel-talk/CADGZSSc8CvZgCvW3%2BJ5XQrjdxc0ONPiMdDxMK4Qh4nr1zG5zrA%40mail.gmail.com.

Reply via email to