Skip to content

Commit

Permalink
Added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ankane committed May 2, 2015
1 parent 247d68b commit 53d4c91
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 2 deletions.
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@ source "https://rubygems.org"

# Specify your gem's dependencies in slowpoke.gemspec
gemspec

gem "makara", github: "taskrabbit/makara"
7 changes: 7 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1 +1,8 @@
require "bundler/gem_tasks"
require "rake/testtask"

task default: :test
Rake::TestTask.new do |t|
t.libs << "test"
t.pattern = "test/**/*_test.rb"
end
5 changes: 3 additions & 2 deletions lib/slowpoke/postgres.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ module Postgres
end

def configure_connection_with_statement_timeout
configure_connection_without_statement_timeout
value = configure_connection_without_statement_timeout
safely do
timeout = Slowpoke.database_timeout || Slowpoke.timeout
if timeout && !timeout.respond_to?(:call)
if ActiveRecord::Base.logger
if ActiveRecord::Base.logger && ActiveRecord::Base.logger.respond_to?(:silence)
ActiveRecord::Base.logger.silence do
execute("SET statement_timeout = #{timeout * 1000}")
end
Expand All @@ -20,6 +20,7 @@ def configure_connection_with_statement_timeout
end
end
end
value
end
end
end
3 changes: 3 additions & 0 deletions slowpoke.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,7 @@ Gem::Specification.new do |spec|

spec.add_development_dependency "bundler", "~> 1.7"
spec.add_development_dependency "rake", "~> 10.0"
spec.add_development_dependency "minitest", ">= 5"
spec.add_development_dependency "rails"
spec.add_development_dependency "pg"
end
13 changes: 13 additions & 0 deletions test/slowpoke_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
require_relative "test_helper"

class TestSlowpoke < Minitest::Test
def test_database_timeout
ActiveRecord::Base.establish_connection adapter: "postgresql", database: "slowpoke_test"
assert_raises(ActiveRecord::StatementInvalid, /canceling statement due to statement timeout/) { ActiveRecord::Base.connection.execute("SELECT pg_sleep(2)") }
end

def test_database_timeout_makara
ActiveRecord::Base.establish_connection adapter: "postgresql_makara", database: "slowpoke_test", makara: {connections: [{role: "master", host: "localhost"}, {host: "localhost"}]}
assert_raises(ActiveRecord::StatementInvalid, /canceling statement due to statement timeout/) { ActiveRecord::Base.connection.execute("SELECT pg_sleep(2)") }
end
end
10 changes: 10 additions & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
require "bundler/setup"
Bundler.require(:default)
require "minitest/autorun"
require "minitest/pride"
require "rails/all"
require "pg"
require "makara"
require "slowpoke"

ENV["DATABASE_TIMEOUT"] = "1"

0 comments on commit 53d4c91

Please sign in to comment.