Skip to content

Commit

Permalink
reset changes
Browse files Browse the repository at this point in the history
  • Loading branch information
a-maas committed Apr 2, 2024
1 parent 2a90ff8 commit 91eaa2d
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 56 deletions.
35 changes: 22 additions & 13 deletions lib/vertex_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,12 @@ def configuration
def reconfigure!
@configuration = Configuration.new
yield(@configuration) if block_given?
reconfigure_circuitbox
end

def configure
yield(configuration)
reconfigure_circuitbox
end

def quotation(payload)
Expand All @@ -76,31 +78,38 @@ def tax_area(payload)
end

def circuit
return unless configuration.circuit_config && defined?(Circuitbox)
return unless circuit_configured?

Circuitbox.circuit(
Configuration::CIRCUIT_NAME,
circuit_config_options
configuration.circuit_config
)
end

private

def circuit_config_options
options = {}
circuit_config = configuration.circuit_config
non_proc_options = [:exceptions, :logger, :cache, :circuit_store, :notifier]
def reconfigure_circuitbox
return unless circuitbox_defined?

# Circuitbox requires that configurable options be passed as procs.
circuit_config.keys.each do |key|
options[key] = Proc.new { circuit_config[key] } unless non_proc_options.include?(key.to_sym)
if Circuitbox.respond_to?(:reset)
Circuitbox.reset
else
Circuitbox.configure do |config|
config.default_circuit_store = configured_circuit_store
end
end
end

non_proc_options.each do |key|
options[key] = circuit_config[key] if circuit_config[key]
end
def configured_circuit_store
(configuration.circuit_config && configuration.circuit_config[:circuit_store]) || Circuitbox::MemoryStore.new
end

def circuit_configured?
configuration.circuit_config && circuitbox_defined?
end

options
def circuitbox_defined?
defined?(Circuitbox)
end
end

Expand Down
9 changes: 1 addition & 8 deletions test/configuration_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,11 @@
it 'has a trusted id' do
VertexClient.configuration.trusted_id = 'trusted-id'
assert_equal 'trusted-id', VertexClient.configuration.trusted_id
VertexClient.reconfigure!
end

it 'has a soap_api, and adds a trailing slash to it' do
VertexClient.configuration.soap_api = 'http://service.example.com'
assert_equal 'http://service.example.com/', VertexClient.configuration.soap_api
VertexClient.reconfigure!
end

it 'has a read_timeout default option and can be set' do
Expand Down Expand Up @@ -42,10 +40,6 @@
end

describe 'circuit_config' do
before do
VertexClient.reconfigure!
end

it 'is an accessible attribute' do
VertexClient.configuration.circuit_config = { test: :ok }
assert_equal :ok, VertexClient.configuration.circuit_config[:test]
Expand All @@ -63,8 +57,7 @@
assert_equal VertexClient.circuit.service, 'vertex_client'
config_defaults = VertexClient::Configuration::CIRCUIT_CONFIG.reject{|k,_| k == :logger }
config_defaults.each_pair do |key, value|
option = VertexClient.circuit.circuit_options[key]
assert_equal value, option.is_a?(Proc) ? option.call : option
assert_equal value, VertexClient.circuit.circuit_options[key]
end
end
end
Expand Down
4 changes: 4 additions & 0 deletions test/connection_test.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
require 'test_helper'
describe VertexClient::Connection do
after do
VertexClient.reconfigure!
end

let(:connection) { VertexClient::Connection.new('test', :test) }

it 'initializes with an endpoint and resource_key' do
Expand Down
17 changes: 5 additions & 12 deletions test/integration_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,7 @@
describe 'Integration' do
include TestInput

before do
VertexClient.reconfigure!
end

after do
# Reset Circuitbox
if Circuitbox.respond_to?(:reset)
Circuitbox.reset
else
Circuitbox.configure { |config| config.default_circuit_store = Circuitbox::MemoryStore.new }
end

VertexClient.reconfigure!
end

Expand Down Expand Up @@ -85,7 +74,10 @@
end

it 'uses circuit if it is available' do
VertexClient.configuration.circuit_config = {}
VertexClient.reconfigure! do |config|
config.circuit_config = {}
end

VCR.use_cassette('quotation', :match_requests_on => []) do
VertexClient.quotation(working_quote_params)
end
Expand Down Expand Up @@ -167,6 +159,7 @@
assert_raises VertexClient::ServerError do
VertexClient.invoice(working_quote_params)
end
VertexClient.circuit.send(:close!)
end

it 'raises if theres an error on invoice and the circuit is closed' do
Expand Down
4 changes: 4 additions & 0 deletions test/payload_validator_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
describe 'payload validation' do
include TestInput

after do
VertexClient.reconfigure!
end

describe 'for incomplete location' do
describe 'for US customer' do
let(:payload) { working_quote_params }
Expand Down
29 changes: 6 additions & 23 deletions test/vertex_client_test.rb
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
require 'test_helper'

describe VertexClient do

include TestInput

after do
VertexClient.reconfigure!
end

it 'has a version number' do
refute_nil ::VertexClient::VERSION
end

it 'can be configured with a block' do
VertexClient.configure { |config| config.trusted_id = 'trusted-id' }
assert_equal 'trusted-id', VertexClient.configuration.trusted_id
VertexClient.reconfigure!
end

it 'does a quotation' do
Expand Down Expand Up @@ -43,26 +45,6 @@
end

describe 'circuit' do
before do
if Circuitbox.respond_to?(:reset)
Circuitbox.reset
else
Circuitbox.configure { |config| config.default_circuit_store = Circuitbox::MemoryStore.new }
end

VertexClient.reconfigure!
end

after do
if Circuitbox.respond_to?(:reset)
Circuitbox.reset
else
Circuitbox.configure { |config| config.default_circuit_store = Circuitbox::MemoryStore.new }
end

VertexClient.reconfigure!
end

it 'only exists if circuit_config is provided to configuration' do
VertexClient.configuration.circuit_config = nil
assert_nil VertexClient.circuit
Expand All @@ -76,10 +58,11 @@
c.circuit_config = { sleep_window: 1234 }
end

assert_equal 1234, VertexClient.circuit.circuit_options[:sleep_window].call
assert_equal 1234, VertexClient.circuit.circuit_options[:sleep_window]
end

it 'opens the circuit' do
VertexClient.reconfigure!
# skip "This test is flaky and I can't figure out how it get it to not be flaky. Do we really need to test that the circuit opens?"
VCR.use_cassette('circuit_breaker', allow_playback_repeats: true, match_requests_on: []) do

Expand Down

0 comments on commit 91eaa2d

Please sign in to comment.