ruby - 使用 rspec 测试成功返回的线程数

标签 ruby multithreading unit-testing testing 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

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

关于ruby - 使用 rspec 测试成功返回的线程数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45469849/

相关文章:

unit-testing - 如何在Groovy脚本中设置退出状态

unit-testing - 使用单例进行单元测试

windows - Ruby 键盘事件处理

ruby - 寻找 Rack 应用程序结构和 config.ru 规范?

ruby - 结合使用 Ruby 和 Node 加密库

ruby - gsub 在整个代码片段中不工作

multithreading - 线程应用程序中的unsafePerformIO无法正常工作

Java多线程

c# - 我如何使用带有计时器刻度的 BackgroundWorker?

unit-testing - 如何在 TFS 托管上查看测试错误消息