Sha256: 3f04c4844a1f77658c60f1eaf16519e853e281df18b2c466b3000a7eabead200

Contents?: true

Size: 1.34 KB

Versions: 50

Compression:

Stored size: 1.34 KB

Contents

require 'spec_helper'
require 'securerandom'

describe "threadhot" do

  before :each do
    LabelHierarchy.delete_all
    Label.delete_all
    @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"] != "sqlite3") && (ActiveRecord::VERSION::STRING =~ /^3.2/))

Version data entries

50 entries across 50 versions & 2 rubygems

Version Path
lalala-4.0.0.dev.92 vendor/deps/closure_tree/spec/parallel_prepend_sibling_spec.rb
lalala-4.0.0.dev.90 vendor/deps/closure_tree/spec/parallel_prepend_sibling_spec.rb
lalala-4.0.0.dev.84 vendor/deps/closure_tree/spec/parallel_prepend_sibling_spec.rb
lalala-4.0.0.dev.80 vendor/deps/closure_tree/spec/parallel_prepend_sibling_spec.rb
lalala-4.0.0.dev.79 vendor/deps/closure_tree/spec/parallel_prepend_sibling_spec.rb
lalala-4.0.0.dev.78 vendor/deps/closure_tree/spec/parallel_prepend_sibling_spec.rb
lalala-4.0.0.dev.77 vendor/deps/closure_tree/spec/parallel_prepend_sibling_spec.rb
lalala-4.0.0.dev.74 vendor/deps/closure_tree/spec/parallel_prepend_sibling_spec.rb
lalala-4.0.0.dev.73 vendor/deps/closure_tree/spec/parallel_prepend_sibling_spec.rb
lalala-4.0.0.dev.72 vendor/deps/closure_tree/spec/parallel_prepend_sibling_spec.rb
lalala-4.0.0.dev.71 vendor/deps/closure_tree/spec/parallel_prepend_sibling_spec.rb
lalala-4.0.0.dev.66 vendor/deps/closure_tree/spec/parallel_prepend_sibling_spec.rb
lalala-4.0.0.dev.65 vendor/deps/closure_tree/spec/parallel_prepend_sibling_spec.rb
lalala-4.0.0.dev.64 vendor/deps/closure_tree/spec/parallel_prepend_sibling_spec.rb
lalala-4.0.0.dev.63 vendor/deps/closure_tree/spec/parallel_prepend_sibling_spec.rb
lalala-4.0.0.dev.62 vendor/deps/closure_tree/spec/parallel_prepend_sibling_spec.rb
lalala-4.0.0.dev.61 vendor/deps/closure_tree/spec/parallel_prepend_sibling_spec.rb
lalala-4.0.0.dev.60 vendor/deps/closure_tree/spec/parallel_prepend_sibling_spec.rb
lalala-4.0.0.dev.59 vendor/deps/closure_tree/spec/parallel_prepend_sibling_spec.rb
lalala-4.0.0.dev.58 vendor/deps/closure_tree/spec/parallel_prepend_sibling_spec.rb