Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I test a code with 'Thread.new' in Rails?

In my rails app, Signup Class has below functions,

def register_email
  # Something...
  add_to_other_thread do
    send_verification_email
  end
end

def add_to_other_thread(&block)
  Thread.new do
    yield
    ActiveRecord::Base.connection.close
  end
end

And I want to do 3 tests with these.

  1. Test about add_to_other_thread(&blcok):
    After add_to_other_thread being called with some block, Check whether or not it called Thread.new with proper block.
  2. Test about register_email:
    After register_email being called, Check whether add_to_other_thread(&block) got proper block or not.
  3. In Integral Test:
    After User signing up, Check whether or not proper email was sent(with ActionMailer::Base.deliveries) via other Thread.
like image 465
cancue Avatar asked Sep 20 '15 06:09

cancue


People also ask

How do you perform a thread test?

How to Perform Thread Testing? Thread testing is conducted at the initial phase of the system integration testing process. For this, you need to accurately plan how to put modules in the correct order to integrate them into a complete system. Big-bang approach − All modules are integrated into a single step.

How do you run a test in rails?

We can run all of our tests at once by using the bin/rails test command. Or we can run a single test file by passing the bin/rails test command the filename containing the test cases. This will run all test methods from the test case.

What is Rails_max_threads?

RAILS_MAX_THREADS is a way to set the number of threads each of your workers will use under the hood. In the example above, the min_threads_count is equal to the max_threads_count , so the number of threads is constant.

Can rails handle concurrent requests?

1 Automatic ConcurrencyRails automatically allows various operations to be performed at the same time. When using a threaded web server, such as the default Puma, multiple HTTP requests will be served simultaneously, with each request provided its own controller instance.


1 Answers

Make Thread.new just execute the block instead of doing Thready things. Either stub Thread.new or Mock Thread or replace it.

expect(Thread).to receive(:new).and_yield

See also testing threaded code in ruby

like image 103
kwerle Avatar answered Oct 03 '22 15:10

kwerle