0.0
No commit activity in last 3 years
No release in over 3 years
Test helper for ordering threaded code (does not depend on gems or stdlib, tested on 1.8.7 - 2.2, rbx, jruby).
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 3.0
 Project Readme

Build Status

ThreadOrder

A tool for testing threaded code. Its purpose is to enable reasoning about thread order.

  • Tested on 1.8.7 - 2.6, JRuby, Rbx
  • It has no external dependencies
  • It does not depend on the stdlib.

Example

# A somewhat contrived class we're going to test.
class MyQueue
  attr_reader :array

  def initialize
    @array, @mutex = [], Mutex.new
  end

  def enqueue
    @mutex.synchronize { @array << yield }
  end
end



require 'rspec/autorun'
require 'thread_order'

RSpec.describe MyQueue do
  let(:queue) { described_class.new }
  let(:order) { ThreadOrder.new }
  after { order.apocalypse! } # ensure everything gets cleaned up (technically redundant for our one example, but it's a good practice)

  it 'is threadsafe on enqueue' do
    # will execute in a thread, can be invoked by name
    order.declare :concurrent_enqueue do
      queue.enqueue { :concurrent }
    end

    # this enqueue will block until the mutex puts the other one to sleep
    queue.enqueue do
      order.pass_to :concurrent_enqueue, resume_on: :sleep
      :main
    end

    order.join_all # concurrent_enqueue may still be asleep
    expect(queue.array).to eq [:main, :concurrent]
  end
end

# >> MyQueue
# >>   is threadsafe on enqueue
# >>
# >> Finished in 0.00131 seconds (files took 0.08687 seconds to load)
# >> 1 example, 0 failures