Skip to content

Commit

Permalink
Add spec for ActiveRecord shared connection.
Browse files Browse the repository at this point in the history
  • Loading branch information
jarmo committed Sep 20, 2013
1 parent 76e2683 commit c1ed876
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 6 deletions.
6 changes: 6 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
require "bundler/gem_tasks"

require 'rspec/core/rake_task'
RSpec::Core::RakeTask.new(:spec)

task :default => :spec
task :release => :spec

require "yard"
YARD::Rake::YardocTask.new
12 changes: 6 additions & 6 deletions lib/watir/rspec/active_record_shared_connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
#
# Tip from http://blog.plataformatec.com.br/2011/12/three-tips-to-improve-the-performance-of-your-test-suite/
class ::ActiveRecord::Base
@@shared_connection_semaphore = Mutex.new
@@shared_connection = nil
@shared_connection_semaphore = Mutex.new

def self.connection
@@shared_connection_semaphore.synchronize do
@@shared_connection ||= retrieve_connection
class << self
def connection
@shared_connection_semaphore.synchronize do
@shared_connection ||= retrieve_connection
end
end
end
end

end
9 changes: 9 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
RSpec.configure do |c|
c.color = true
end

# Make sure constants are defined
module ActiveRecord
class Base
end
end
25 changes: 25 additions & 0 deletions spec/watir/rspec/active_record_shared_connection_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
require "spec_helper"
require "watir/rspec/active_record_shared_connection"

describe ::ActiveRecord::Base do
before { described_class.instance_variable_set :@shared_connection, nil }

context ".connection" do
it "reuses the connection" do
described_class.should_receive(:retrieve_connection).once.and_return(:established_connection)

2.times { described_class.connection.should == :established_connection }
end

it "uses mutex" do
described_class.should_receive(:retrieve_connection).once { sleep 0.1; :established_connection }

thr = Thread.new { described_class.connection }

t = Time.now
described_class.connection
(Time.now - t).should be >= 0.1
thr.join
end
end
end

0 comments on commit c1ed876

Please sign in to comment.