From a210c5cd76f9178b5253b01c845076f2ac3f5e0a Mon Sep 17 00:00:00 2001 From: Olivier Bellone Date: Thu, 12 Oct 2017 18:13:23 +0200 Subject: [PATCH] Ensure that each thread has its own client --- .rubocop_todo.yml | 26 ++++++++++++-------------- lib/stripe/stripe_client.rb | 2 +- test/stripe/stripe_client_test.rb | 9 +++++++++ 3 files changed, 22 insertions(+), 15 deletions(-) diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 352328713..a0788feab 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -1,6 +1,6 @@ # This configuration was generated by # `rubocop --auto-gen-config` -# on 2017-09-27 21:19:01 +0200 using RuboCop version 0.50.0. +# on 2017-10-12 18:19:29 +0200 using RuboCop version 0.50.0. # The point is for the user to remove these configuration records # one by one as the offenses are removed from the code base. # Note that changes in the inspected code, or installation of new @@ -10,46 +10,44 @@ Metrics/AbcSize: Max: 49 -# Offense count: 22 +# Offense count: 24 # Configuration parameters: CountComments, ExcludedMethods. Metrics/BlockLength: - Max: 461 + Max: 455 # Offense count: 8 # Configuration parameters: CountComments. Metrics/ClassLength: - Max: 581 + Max: 583 # Offense count: 8 Metrics/CyclomaticComplexity: Max: 13 -# Offense count: 216 +# Offense count: 215 # Configuration parameters: AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, IgnoredPatterns. # URISchemes: http, https Metrics/LineLength: - Max: 312 + Max: 310 -# Offense count: 29 +# Offense count: 31 # Configuration parameters: CountComments. Metrics/MethodLength: Max: 47 -# Offense count: 2 +# Offense count: 1 # Configuration parameters: CountComments. Metrics/ModuleLength: - Max: 423 + Max: 298 -# Offense count: 6 +# Offense count: 5 # Configuration parameters: CountKeywordArgs. Metrics/ParameterLists: Max: 7 -# Offense count: 5 +# Offense count: 4 Metrics/PerceivedComplexity: - Max: 11 - Exclude: - - 'lib/stripe/stripe_object.rb' + Max: 15 # Offense count: 2 Style/ClassVars: diff --git a/lib/stripe/stripe_client.rb b/lib/stripe/stripe_client.rb index 812f5c111..184d25a4d 100644 --- a/lib/stripe/stripe_client.rb +++ b/lib/stripe/stripe_client.rb @@ -17,7 +17,7 @@ def self.active_client end def self.default_client - @default_client ||= StripeClient.new(default_conn) + Thread.current[:stripe_client_default_client] ||= StripeClient.new(default_conn) end # A default Faraday connection to be used when one isn't configured. This diff --git a/test/stripe/stripe_client_test.rb b/test/stripe/stripe_client_test.rb index 173b87c0a..1804b29bd 100644 --- a/test/stripe/stripe_client_test.rb +++ b/test/stripe/stripe_client_test.rb @@ -19,6 +19,15 @@ class StripeClientTest < Test::Unit::TestCase should "be a StripeClient" do assert_kind_of StripeClient, StripeClient.default_client end + + should "be a different client on each thread" do + other_thread_client = nil + thread = Thread.new do + other_thread_client = StripeClient.default_client + end + thread.join + refute_equal StripeClient.default_client, other_thread_client + end end context ".default_conn" do