2017-08-02 34 views

回答

0
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