module RailsStuff::TestHelpers::Concurrency

Attributes

threads_count[RW]

Default threads count

Public Instance Methods

concurrently(thread_args = nil) { || ... } click to toggle source

Runs block concurrently in separate threads. Pass array of args arrays to run each thread with its own arguments. Or pass Integer to run specified threads count with same arguments. Default is to run Concurrency.threads_count threads.

concurrently { do_something }
concurrently(5) { do_something }
concurrently([[1, opt: true], [2, opt: false]]) do |arg, **options|
  do_something(arg, options)
end
# It'll automatically wrap single args into Array:
concurrently(1, 2, {opt: true}, {opt: false}, [1, opt: false]) { ... }
# File lib/rails_stuff/test_helpers/concurrency.rb, line 27
def concurrently(thread_args = nil)
  thread_args ||= Concurrency.threads_count
  threads =
    case thread_args
    when Integer
      Array.new(thread_args) { Thread.new { yield } }
    else
      thread_args.map { |args| Thread.new { yield(*Array.wrap(args)) } }
    end
  threads.each(&:join)
end