Skip to content

Instantly share code, notes, and snippets.

@audy
Last active June 20, 2017 08:16
Show Gist options
  • Save audy/4bf963ee473dc2c113ee30c325291bb4 to your computer and use it in GitHub Desktop.
Save audy/4bf963ee473dc2c113ee30c325291bb4 to your computer and use it in GitHub Desktop.
source 'https://rubygems.org'
gem 'activerecord'
gem 'blamer'
gem 'pg'
gem 'pry'
gem 'parallel'
require 'bundler'
Bundler.require
class Foo < ActiveRecord::Base
establish_connection adapter: 'postgresql', database: 'test_blamer'
connection.create_table table_name, force: true do |t|
t.string :name
t.userstamps
end
end
class Foo < ActiveRecord::Base
end
class User
cattr_accessor :current_user
attr_accessor :id
def initialize kwargs
@id = kwargs.fetch(:id)
end
def id
@id
end
end
puts 'testing single threaded'
Parallel.each((0..100), in_threads: 1) do |i|
expected_id = rand(0..1)
User.current_user = User.new id: expected_id
foo = Foo.create!
foo.reload
fail "#{i} -> expected #{foo.created_by} to equal #{expected_id}" unless foo.created_by == expected_id
end
puts 'testing 2-threaded'
Parallel.each((0..100), in_threads: 2) do |i|
expected_id = rand(0..1)
User.current_user = User.new id: expected_id
foo = Foo.create!
foo.reload
fail "#{i} -> expected #{foo.created_by} to equal #{expected_id}" unless foo.created_by == expected_id
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment