-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implement reset on query executor (#30)
* implement reset on query executor * make methods smaller
- Loading branch information
1 parent
bfa5462
commit 8799b99
Showing
4 changed files
with
79 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# frozen_string_literal: true | ||
|
||
# Entry point for DatabaseConnection | ||
module PgDice | ||
# Wrapper class around pg_connection to reset connection on PG errors | ||
class QueryExecutor | ||
attr_reader :logger | ||
|
||
def initialize(logger:, connection_supplier:) | ||
@logger = logger | ||
@connection_supplier = connection_supplier | ||
end | ||
|
||
def call(query) | ||
@connection_supplier.call.exec(query) | ||
rescue PG::Error => error | ||
logger.error { "Caught error: #{error}. Going to reset connection and try again" } | ||
@connection_supplier.call.reset | ||
@connection_supplier.call.exec(query) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'test_helper' | ||
|
||
class QueryExecutorTest < Minitest::Test | ||
def setup | ||
@should_raise = true | ||
@resetter_call_count = 0 | ||
@runner_call_count = 0 | ||
|
||
@resetter = resetter | ||
@runner = runner | ||
end | ||
|
||
def test_retry_on_pg_error | ||
PgDice::QueryExecutor.new(logger: logger, connection_supplier: -> { MockPgConnection.new(@runner, @resetter) }) | ||
.call('blah') | ||
|
||
assert_equal 2, @runner_call_count, 'Runner should be called twice when we catch a PG error' | ||
assert_equal 1, @resetter_call_count, 'Resetter should be called once when we catch a PG error' | ||
end | ||
|
||
private | ||
|
||
def resetter | ||
proc do | ||
@should_raise = false | ||
@resetter_call_count += 1 | ||
end | ||
end | ||
|
||
def runner | ||
proc do | ||
@runner_call_count += 1 | ||
raise PG::Error, 'Something bad' if @should_raise | ||
end | ||
end | ||
end | ||
|
||
class MockPgConnection | ||
def initialize(runner, resetter) | ||
@runner = runner | ||
@resetter = resetter | ||
end | ||
|
||
def exec(query) | ||
@runner.call(query) | ||
end | ||
|
||
def reset | ||
@resetter.call | ||
end | ||
end |