Sha256: 3016ed728c370485e1da753a67d8ff86930a62a7282e16e7538b26b278678db7

Contents?: true

Size: 1.75 KB

Versions: 4

Compression:

Stored size: 1.75 KB

Contents

require 'spec_helper'
describe Upsert do
  Thread.abort_on_exception = true
  describe "is thread-safe" do
    it "is safe to use one-by-one" do
      upsert = Upsert.new $conn, :pets
      assert_creates(Pet, [{:name => 'Jerry', :gender => 'neutered'}]) do
        ts = []
        10.times do
          ts << Thread.new {
            ActiveRecord::Base.connection_pool.with_connection do |conn|
              sleep 0.2
              upsert.row({name: "Jerry"}, gender: "male")
              upsert.row({name: "Jerry"}, gender: "neutered")
            end
          }
        end
        ts.each { |t| t.join(3) }
      end
    end
    it "is safe to use batch" do
      assert_creates(Pet, [{:name => 'Jerry', :gender => 'neutered'}]) do
        Upsert.batch($conn, :pets) do |upsert|
          ts = []
          10.times do
            ts << Thread.new {
              ActiveRecord::Base.connection_pool.with_connection do |conn|
                sleep 0.2
                upsert.row({name: "Jerry"}, gender: "male")
                upsert.row({name: "Jerry"}, gender: "neutered")
              end
            }
          end
          ts.each { |t| t.join(3) }
        end
      end
    end

    it "is safe to use with the entire block instead the thread" do
      assert_creates(Pet, [{name: "Jerry", gender: "neutered"}]) do
        ts = []
        10.times do
          ts << Thread.new {
            ActiveRecord::Base.connection_pool.with_connection do |conn|
              sleep 0.1
              Upsert.batch(conn, :pets) do |upsert|
                upsert.row({name: "Jerry"}, gender: "male")
                upsert.row({name: "Jerry"}, gender: "neutered")
              end
            end
          }
        end
        ts.each { |t| t.join(3) }
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
upsert-2.9.10-java spec/threaded_spec.rb
upsert-2.9.10 spec/threaded_spec.rb
upsert-2.9.9-universal-java-11 spec/threaded_spec.rb
upsert-2.9.9 spec/threaded_spec.rb