开发者

DatabaseCleaner with Resque

开发者 https://www.devze.com 2023-03-27 12:48 出处:网络
Got a weird problem that I can\'t find addressed anywhere. I\'m using Resque in a project along with DatabaseCleaner to clean up between tests. Our test suite needs to actually use regular resque/red

Got a weird problem that I can't find addressed anywhere.

I'm using Resque in a project along with DatabaseCleaner to clean up between tests. Our test suite needs to actually use regular resque/redis (so we can't use the monkeypatching ResqueSpec as it bypassess Resque/Redis usage).

We've been trying to use DBCleaner's :transaction strategy, but because Resque spawns out its own ActiveRecord connection after_fork (see code below), the transactions won't work as mysql only allows transactions to be seen on a single connection.

Resque does this:

Resque.after_fork { ActiveRecord::Base.establish_connection if defined?(ActiveRecord) }

The easy solution is to use DB cleaner with the :truncation strategy. We'd like to be able to switch on :truncation only when using Resque and then back over to :transaction when running unrelated specs.

Part of the trick is that our Resque worker is up and running for the duration of the specs. It starts up a worker when the test specs startup and then simply suspends the worker process after the @resque-marked spec is done running via a set of Before/After hooks.

So the question is how do we get DatabaseCleaner to effectively switch all active ActiveRecord DB connections to use the right cleaning strategy? DatabaseCleaner provides methods of switching strategies for different specs, but it doesn't开发者_C百科 seem to make the switch to all connections, rather only to the main Rails-process AR connection.

Again, solution for now is to use :truncation everywhere. Just wanted to know if anyone had any thoughts on how to work around this.

0

精彩评论

暂无评论...
验证码 换一张
取 消