Sha256: c07b2edef6a1c8187c679ed1bcc8961fd17fb2b1605da9a7de984ba434e12c5e

Contents?: true

Size: 1 KB

Versions: 14

Compression:

Stored size: 1 KB

Contents

require 'abstract_unit'
require 'fixtures/topic'

class ThreadSafetyTest < Test::Unit::TestCase
  def setup
    @topics  = create_fixtures "topics"
    @threads = []
  end
  
  def test_threading_on_transactions
    # SQLite breaks down under thread banging
    # Jamis Buck (author of SQLite-ruby): "I know that sqlite itself is not designed for concurrent access"
    if ActiveRecord::ConnectionAdapters.const_defined? :SQLiteAdapter
      return true if ActiveRecord::Base.connection.instance_of?(ActiveRecord::ConnectionAdapters::SQLiteAdapter)
    end

    5.times do |thread_number|
      @threads << Thread.new(thread_number) do |thread_number|
        first, second = Topic.find(1, 2)
        Topic.transaction(first, second) do
          Topic.logger.info "started #{thread_number}"
          first.approved  = 1
          second.approved = 0
          first.save
          second.save
          Topic.logger.info "ended #{thread_number}"
        end
      end
    end
    
    @threads.each { |t| t.join }
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
activerecord-1.0.0 test/thread_safety_test.rb
activerecord-1.10.0 test/thread_safety_test.rb
activerecord-1.1.0 test/thread_safety_test.rb
activerecord-1.10.1 test/thread_safety_test.rb
activerecord-1.3.0 test/thread_safety_test.rb
activerecord-1.2.0 test/thread_safety_test.rb
activerecord-1.4.0 test/thread_safety_test.rb
activerecord-1.5.1 test/thread_safety_test.rb
activerecord-1.8.0 test/thread_safety_test.rb
activerecord-1.6.0 test/thread_safety_test.rb
activerecord-1.5.0 test/thread_safety_test.rb
activerecord-1.7.0 test/thread_safety_test.rb
activerecord-1.9.0 test/thread_safety_test.rb
activerecord-1.9.1 test/thread_safety_test.rb