<commit_before>require "spec_helper"

# FIXME: These tests are buggy on Rails 3.0
# Sqlite is not concurrent database
if ActiveRecord::VERSION::STRING >= "3.1" && ENV['DB'] != 'sqlite3'
  describe ActsAsOrderedTree, :non_transactional do
    module Concurrency
      # run block in its own thread, create +size+ threads
      def pool(size)
        body = proc do |x|
          ActiveRecord::Base.connection_pool.with_connection do
            yield x
          end
        end
        threads = size.times.map { |x| Thread.new { body.call(x) } }

        threads.each(&:join)
      end
    end
    include Concurrency

    let!(:root) { create :default }

    it "should not create nodes with same position" do
      pool(3) do
        create :default, :parent => root
      end

      root.children.map(&:position).should eq [1, 2, 3]
    end

    it "should not move nodes to same position when moving to child of certain node" do
      nodes = create_list :default, 3

      pool(3) do |x|
        nodes[x].move_to_child_of(root)
      end

      root.children.map(&:position).should eq [1, 2, 3]
    end
<commit_msg>concurency support spec: simplify pool method<commit_after>require "spec_helper"

# FIXME: These tests are buggy on Rails 3.0
# Sqlite is not concurrent database
if ActiveRecord::VERSION::STRING >= "3.1" && ENV['DB'] != 'sqlite3'
  describe ActsAsOrderedTree, :non_transactional do
    module Concurrency
      # run block in its own thread, create +size+ threads
      def pool(size)
        size.times.map { |x|
          Thread.new do
            ActiveRecord::Base.connection_pool.with_connection { yield x }
          end
        }.each(&:join)
      end
    end
    include Concurrency

    let!(:root) { create :default }

    it "should not create nodes with same position" do
      pool(3) do
        create :default, :parent => root
      end

      root.children.map(&:position).should eq [1, 2, 3]
    end

    it "should not move nodes to same position when moving to child of certain node" do
      nodes = create_list :default, 3

      pool(3) do |x|
        nodes[x].move_to_child_of(root)
      end

      root.children.map(&:position).should eq [1, 2, 3]
    end

{'fraction_non_alphanumeric': 0.09986382206082614, 'fraction_numerical': 0.01270994098955969, 'mean_word_length': 2.4873417721518987, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4532168', 'n_tokens_mistral': 692, 'n_tokens_neox': 660, 'n_words': 224}