Sha256: 7b184cb24be62e3eb7f43c7f4e8c556839a21cf32991fbf33df1360bd71ddea3

Contents?: true

Size: 1.29 KB

Versions: 2

Compression:

Stored size: 1.29 KB

Contents

require 'spec_helper'
require 'securerandom'

describe "threadhot" do

  before :each do
    @iterations = 5
    @workers = 8
  end

  def prepend_sibling_at_even_second(run_at)
    ActiveRecord::Base.connection.reconnect!
    sibling = Label.new(:name => SecureRandom.hex(10))
    target = Label.find(@target.id)
    sleep(run_at - Time.now.to_f)
    target.prepend_sibling sibling
  end

  def run_workers
    start_time = Time.now.to_i + 2
    @times = @iterations.times.collect { |ea| start_time + (ea * 2) }
    @names = @times.collect { |ea| ea.to_s }
    @threads = @workers.times.collect do
      Thread.new do
        @times.each { |ea| prepend_sibling_at_even_second(ea) }
      end
    end
    @threads.each { |ea| ea.join }
  end

  it "prepend_sibling on a non-root node doesn't cause deadlocks" do
    @target = Label.find_or_create_by_path %w(root parent)
    run_workers
    children = Label.roots
    uniq_sort_orders = children.collect { |ea| ea.sort_order }.uniq
    children.size.should == uniq_sort_orders.size

    # The only non-root node should be "root":
    Label.all.select { |ea| ea.root? }.should == [@target.parent]
  end

# SQLite doesn't like parallelism, and Rails 3.0 and 3.1 have known threading issues. SKIP.
end if ((ENV["DB"] != "sqlite") && (ActiveRecord::VERSION::STRING =~ /^3.2/))

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
closure_tree-4.4.0 spec/parallel_prepend_sibling_spec.rb
closure_tree-4.3.0 spec/parallel_prepend_sibling_spec.rb