简体   繁体   中英

Testing number of successfully returned threads using rspec

如何测试成功返回主线程的线程数等于在rspec中创建的线程数?

describe '#update_tokens' do
  it 'is threadsafe', db_strategy: :truncation do
    u = create(:user)
    thread_injected = false
    time = Time.now
    allow(Time).to receive(:now) do
      unless thread_injected
        thread_injected = true
        Thread.new { u.reload.update_tokens('c2', 't2') }.join
      end
      time
    end
    u.update_tokens('c1', 't1')
    expect(u.tokens['c1']).not_to be_nil
    expect(u.tokens['c2']).not_to be_nil
  end
end

Original: http://blog.paulrugelhiatt.com/rails/rspec/2015/09/28/testing-thread-safety-concurrency-in-rspec.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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