diff --git a/lib/vertex_client.rb b/lib/vertex_client.rb index b156d19..82ef774 100644 --- a/lib/vertex_client.rb +++ b/lib/vertex_client.rb @@ -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) @@ -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 diff --git a/test/configuration_test.rb b/test/configuration_test.rb index cffe562..2d9a484 100644 --- a/test/configuration_test.rb +++ b/test/configuration_test.rb @@ -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 @@ -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] @@ -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 diff --git a/test/connection_test.rb b/test/connection_test.rb index 645c129..65e0c6a 100644 --- a/test/connection_test.rb +++ b/test/connection_test.rb @@ -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 diff --git a/test/integration_test.rb b/test/integration_test.rb index 435a2fd..a779ca9 100644 --- a/test/integration_test.rb +++ b/test/integration_test.rb @@ -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 @@ -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 @@ -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 diff --git a/test/payload_validator_test.rb b/test/payload_validator_test.rb index 0d9f913..8a93abf 100644 --- a/test/payload_validator_test.rb +++ b/test/payload_validator_test.rb @@ -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 } diff --git a/test/vertex_client_test.rb b/test/vertex_client_test.rb index 3769348..4587a8a 100644 --- a/test/vertex_client_test.rb +++ b/test/vertex_client_test.rb @@ -1,9 +1,12 @@ require 'test_helper' describe VertexClient do - include TestInput + after do + VertexClient.reconfigure! + end + it 'has a version number' do refute_nil ::VertexClient::VERSION end @@ -11,7 +14,6 @@ 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 @@ -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 @@ -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