Module: RailsStuff::RSpecHelpers::Concurrency::ClassMethods

Defined in:
lib/rails_stuff/rspec_helpers/concurrency.rb

Instance Method Summary collapse

Instance Method Details

#check_concurrent(&block) ⇒ Object

Runs given block in current context and nested context with concurrent subject.

subject { -> { increment_value_once } }
# This will create 2 examples. One for current contex, and one
# for current context where subject will run multiple times concurrently.
check_concurrent do
  it { should change { value }.by(1) }
end


33
34
35
36
37
38
39
# File 'lib/rails_stuff/rspec_helpers/concurrency.rb', line 33

def check_concurrent(&block)
  instance_eval(&block)
  context 'running multiple times concurrently' do
    concurrent_subject!
    instance_eval(&block)
  end
end

#concurrent_subject!Object

Defines subject which runs parent’s value in multiple threads concurrently. Define ‘thread_args` or `threads_count` with `let` to configure it.

Sets metadata ‘concurrent: true` so database cleaner uses right strategy.



15
16
17
18
19
20
21
22
23
# File 'lib/rails_stuff/rspec_helpers/concurrency.rb', line 15

def concurrent_subject!
  [:concurrent] = true
  subject do
    super_proc = super()
    args = defined?(thread_args) && thread_args
    args ||= defined?(threads_count) && threads_count
    -> { concurrently(args, &super_proc) }
  end
end