简体   繁体   English

Ruby on Rails 和 Sidekiq redis 连接池与并行测试?

[英]Ruby on Rails and Sidekiq redis connection pool with parallel tests?

I want to run my Rails tests in parallel.我想并行运行我的 Rails 测试。 I'm currently using Sidekiq own connection pool to communicate with redis.我目前正在使用 Sidekiq 自己的连接池与 redis 进行通信。 The problem here is that if I run parallel tests they collide with each other.这里的问题是,如果我运行并行测试,它们会相互冲突。 Rails has a very nice built in test database system that creates database clones of postgresql for each thread that executes the tests. Rails 有一个非常好的内置测试数据库系统,它为执行测试的每个线程创建 postgresql 的数据库克隆。 How would I do that with Sidekiq redis connection pool?我将如何使用 Sidekiq redis 连接池来做到这一点?

Your tests shouldn't be connecting to redis at all.您的测试根本不应该连接到 redis。 Much like how emails are handled in the test environment, by default Sidekiq will use fake queues for any scheduled jobs in the test environment.就像在测试环境中处理电子邮件的方式一样,默认情况下,Sidekiq 将为测试环境中的任何计划作业使用假队列。

Basically - running your tests in parallel will not cause you any issues with Sidekiq jobs.基本上 - 并行运行测试不会导致 Sidekiq 作业出现任何问题。

You should start by reading the documentation on testing - https://github.com/mperham/sidekiq/wiki/Testing您应该首先阅读有关测试的文档 - https://github.com/mperham/sidekiq/wiki/Testing

Essentially, you can test to see if a job has been scheduled with something like this:本质上,您可以测试以查看是否已使用以下方式安排了作业:

HardWorker.perform_async(1, 2)
HardWorker.perform_async(2, 3)
assert_equal 2, HardWorker.jobs.size

And if you need the jobs to run, just drain the queue:如果您需要运行作业,只需排空队列:

HardWorker.drain
assert_equal 0, HardWorker.jobs.size

More details on the link above.更多详细信息请参见上面的链接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM