Chewy, RSpec and Threads raising a weird error












0















I'm testing some concurrent behaviours using RSpec and a weird exception is raised.



expect{
threads = 4.times.map do
Thread.new do
WebMock.allow_net_connect!
PaymentForm.new(params).process
end
end

threads.each(&:join)
}.to change{
Payment.count
}.by(1)


After the PaymentForm#process method is called, it creates a Payment (model) record. The Payment model is indexed using Chewy on ElasticSearch, updating the index right after the model is saved.



It turns out that a weird exception is raised:



  1) Checkout::PaymentForm#process when the payment is duplicated blocks repeated transactions
Failure/Error: unless save

NoMethodError:
undefined method `get' for nil:NilClass
Did you mean? gem
# /usr/local/bundle/gems/httpclient-2.8.3/lib/httpclient/session.rb:726:in `set_header'
# /usr/local/bundle/gems/webmock-3.4.2/lib/webmock/http_lib_adapters/httpclient_adapter.rb:206:in `headers_from_session'
# /usr/local/bundle/gems/webmock-3.4.2/lib/webmock/http_lib_adapters/httpclient_adapter.rb:168:in `build_request_signature'
# /usr/local/bundle/gems/webmock-3.4.2/lib/webmock/http_lib_adapters/httpclient_adapter.rb:55:in `do_get'
# /usr/local/bundle/gems/webmock-3.4.2/lib/webmock/http_lib_adapters/httpclient_adapter.rb:47:in `do_get_block'
# /usr/local/bundle/gems/httpclient-2.8.3/lib/httpclient.rb:1019:in `block in do_request'
# /usr/local/bundle/gems/httpclient-2.8.3/lib/httpclient.rb:1133:in `protect_keep_alive_disconnected'
# /usr/local/bundle/gems/httpclient-2.8.3/lib/httpclient.rb:1014:in `do_request'
# /usr/local/bundle/gems/httpclient-2.8.3/lib/httpclient.rb:856:in `request'
# /usr/local/bundle/gems/faraday-0.15.2/lib/faraday/adapter/httpclient.rb:38:in `call'
# /usr/local/bundle/gems/faraday-0.15.2/lib/faraday/rack_builder.rb:143:in `build_response'
# /usr/local/bundle/gems/faraday-0.15.2/lib/faraday/connection.rb:387:in `run_request'
# /usr/local/bundle/gems/elasticsearch-transport-6.1.0/lib/elasticsearch/transport/transport/http/faraday.rb:23:in `block in perform_request'
# /usr/local/bundle/gems/elasticsearch-transport-6.1.0/lib/elasticsearch/transport/transport/base.rb:266:in `perform_request'
# /usr/local/bundle/gems/elasticsearch-transport-6.1.0/lib/elasticsearch/transport/transport/http/faraday.rb:20:in `perform_request'
# /usr/local/bundle/gems/elasticsearch-transport-6.1.0/lib/elasticsearch/transport/client.rb:131:in `perform_request'
# /usr/local/bundle/gems/elasticsearch-api-6.1.0/lib/elasticsearch/api/namespace/common.rb:21:in `perform_request'
# /usr/local/bundle/gems/elasticsearch-api-6.1.0/lib/elasticsearch/api/actions/indices/exists.rb:46:in `block in exists'
# /usr/local/bundle/gems/elasticsearch-api-6.1.0/lib/elasticsearch/api/utils.rb:202:in `__rescue_from_not_found'
# /usr/local/bundle/gems/elasticsearch-api-6.1.0/lib/elasticsearch/api/actions/indices/exists.rb:45:in `exists'
# /usr/local/bundle/gems/chewy-5.0.0/lib/chewy/index/actions.rb:15:in `exists?'
# /usr/local/bundle/gems/chewy-5.0.0/lib/chewy/type/import/routine.rb:69:in `create_indexes!'
# /usr/local/bundle/gems/chewy-5.0.0/lib/chewy/type/import.rb:131:in `import_routine'
# /usr/local/bundle/gems/chewy-5.0.0/lib/chewy/type/import.rb:87:in `import!'
# /usr/local/bundle/gems/chewy-5.0.0/lib/chewy/strategy/urgent.rb:13:in `update'
# /usr/local/bundle/gems/chewy-5.0.0/lib/chewy/type/observe.rb:76:in `update_index'
# /usr/local/bundle/gems/chewy-5.0.0/lib/chewy/type/observe.rb:34:in `block in update_proc'
# /usr/local/bundle/gems/bugsnag-6.7.0/lib/bugsnag/integrations/rails/active_record_rescue.rb:25:in `run_callbacks'
# /usr/local/bundle/gems/airbrake-6.2.1/lib/airbrake/rails/active_record.rb:22:in `run_callbacks'
# ./app/models/payment.rb:176:in `update_with_payment_response_data'
# ./app/models/checkout/payment_form.rb:93:in `block in process'
# ./lib/record_lock.rb:34:in `with_lock'
# ./lib/record_lock.rb:27:in `with_lock'
# ./app/models/checkout/payment_form.rb:72:in `process'
# ./spec/models/checkout/payment_form_spec.rb:532:in `block (7 levels) in <main>'


From what I could debug, the exception happens when Chewy tried to update the index using Faraday HTTP client.



I'm also using WebMock, but I'm calling WebMock.allow_net_connect! for each thread, making it permissive. Note that this only happens when I use threads. Without threads that runs fine.










share|improve this question



























    0















    I'm testing some concurrent behaviours using RSpec and a weird exception is raised.



    expect{
    threads = 4.times.map do
    Thread.new do
    WebMock.allow_net_connect!
    PaymentForm.new(params).process
    end
    end

    threads.each(&:join)
    }.to change{
    Payment.count
    }.by(1)


    After the PaymentForm#process method is called, it creates a Payment (model) record. The Payment model is indexed using Chewy on ElasticSearch, updating the index right after the model is saved.



    It turns out that a weird exception is raised:



      1) Checkout::PaymentForm#process when the payment is duplicated blocks repeated transactions
    Failure/Error: unless save

    NoMethodError:
    undefined method `get' for nil:NilClass
    Did you mean? gem
    # /usr/local/bundle/gems/httpclient-2.8.3/lib/httpclient/session.rb:726:in `set_header'
    # /usr/local/bundle/gems/webmock-3.4.2/lib/webmock/http_lib_adapters/httpclient_adapter.rb:206:in `headers_from_session'
    # /usr/local/bundle/gems/webmock-3.4.2/lib/webmock/http_lib_adapters/httpclient_adapter.rb:168:in `build_request_signature'
    # /usr/local/bundle/gems/webmock-3.4.2/lib/webmock/http_lib_adapters/httpclient_adapter.rb:55:in `do_get'
    # /usr/local/bundle/gems/webmock-3.4.2/lib/webmock/http_lib_adapters/httpclient_adapter.rb:47:in `do_get_block'
    # /usr/local/bundle/gems/httpclient-2.8.3/lib/httpclient.rb:1019:in `block in do_request'
    # /usr/local/bundle/gems/httpclient-2.8.3/lib/httpclient.rb:1133:in `protect_keep_alive_disconnected'
    # /usr/local/bundle/gems/httpclient-2.8.3/lib/httpclient.rb:1014:in `do_request'
    # /usr/local/bundle/gems/httpclient-2.8.3/lib/httpclient.rb:856:in `request'
    # /usr/local/bundle/gems/faraday-0.15.2/lib/faraday/adapter/httpclient.rb:38:in `call'
    # /usr/local/bundle/gems/faraday-0.15.2/lib/faraday/rack_builder.rb:143:in `build_response'
    # /usr/local/bundle/gems/faraday-0.15.2/lib/faraday/connection.rb:387:in `run_request'
    # /usr/local/bundle/gems/elasticsearch-transport-6.1.0/lib/elasticsearch/transport/transport/http/faraday.rb:23:in `block in perform_request'
    # /usr/local/bundle/gems/elasticsearch-transport-6.1.0/lib/elasticsearch/transport/transport/base.rb:266:in `perform_request'
    # /usr/local/bundle/gems/elasticsearch-transport-6.1.0/lib/elasticsearch/transport/transport/http/faraday.rb:20:in `perform_request'
    # /usr/local/bundle/gems/elasticsearch-transport-6.1.0/lib/elasticsearch/transport/client.rb:131:in `perform_request'
    # /usr/local/bundle/gems/elasticsearch-api-6.1.0/lib/elasticsearch/api/namespace/common.rb:21:in `perform_request'
    # /usr/local/bundle/gems/elasticsearch-api-6.1.0/lib/elasticsearch/api/actions/indices/exists.rb:46:in `block in exists'
    # /usr/local/bundle/gems/elasticsearch-api-6.1.0/lib/elasticsearch/api/utils.rb:202:in `__rescue_from_not_found'
    # /usr/local/bundle/gems/elasticsearch-api-6.1.0/lib/elasticsearch/api/actions/indices/exists.rb:45:in `exists'
    # /usr/local/bundle/gems/chewy-5.0.0/lib/chewy/index/actions.rb:15:in `exists?'
    # /usr/local/bundle/gems/chewy-5.0.0/lib/chewy/type/import/routine.rb:69:in `create_indexes!'
    # /usr/local/bundle/gems/chewy-5.0.0/lib/chewy/type/import.rb:131:in `import_routine'
    # /usr/local/bundle/gems/chewy-5.0.0/lib/chewy/type/import.rb:87:in `import!'
    # /usr/local/bundle/gems/chewy-5.0.0/lib/chewy/strategy/urgent.rb:13:in `update'
    # /usr/local/bundle/gems/chewy-5.0.0/lib/chewy/type/observe.rb:76:in `update_index'
    # /usr/local/bundle/gems/chewy-5.0.0/lib/chewy/type/observe.rb:34:in `block in update_proc'
    # /usr/local/bundle/gems/bugsnag-6.7.0/lib/bugsnag/integrations/rails/active_record_rescue.rb:25:in `run_callbacks'
    # /usr/local/bundle/gems/airbrake-6.2.1/lib/airbrake/rails/active_record.rb:22:in `run_callbacks'
    # ./app/models/payment.rb:176:in `update_with_payment_response_data'
    # ./app/models/checkout/payment_form.rb:93:in `block in process'
    # ./lib/record_lock.rb:34:in `with_lock'
    # ./lib/record_lock.rb:27:in `with_lock'
    # ./app/models/checkout/payment_form.rb:72:in `process'
    # ./spec/models/checkout/payment_form_spec.rb:532:in `block (7 levels) in <main>'


    From what I could debug, the exception happens when Chewy tried to update the index using Faraday HTTP client.



    I'm also using WebMock, but I'm calling WebMock.allow_net_connect! for each thread, making it permissive. Note that this only happens when I use threads. Without threads that runs fine.










    share|improve this question

























      0












      0








      0








      I'm testing some concurrent behaviours using RSpec and a weird exception is raised.



      expect{
      threads = 4.times.map do
      Thread.new do
      WebMock.allow_net_connect!
      PaymentForm.new(params).process
      end
      end

      threads.each(&:join)
      }.to change{
      Payment.count
      }.by(1)


      After the PaymentForm#process method is called, it creates a Payment (model) record. The Payment model is indexed using Chewy on ElasticSearch, updating the index right after the model is saved.



      It turns out that a weird exception is raised:



        1) Checkout::PaymentForm#process when the payment is duplicated blocks repeated transactions
      Failure/Error: unless save

      NoMethodError:
      undefined method `get' for nil:NilClass
      Did you mean? gem
      # /usr/local/bundle/gems/httpclient-2.8.3/lib/httpclient/session.rb:726:in `set_header'
      # /usr/local/bundle/gems/webmock-3.4.2/lib/webmock/http_lib_adapters/httpclient_adapter.rb:206:in `headers_from_session'
      # /usr/local/bundle/gems/webmock-3.4.2/lib/webmock/http_lib_adapters/httpclient_adapter.rb:168:in `build_request_signature'
      # /usr/local/bundle/gems/webmock-3.4.2/lib/webmock/http_lib_adapters/httpclient_adapter.rb:55:in `do_get'
      # /usr/local/bundle/gems/webmock-3.4.2/lib/webmock/http_lib_adapters/httpclient_adapter.rb:47:in `do_get_block'
      # /usr/local/bundle/gems/httpclient-2.8.3/lib/httpclient.rb:1019:in `block in do_request'
      # /usr/local/bundle/gems/httpclient-2.8.3/lib/httpclient.rb:1133:in `protect_keep_alive_disconnected'
      # /usr/local/bundle/gems/httpclient-2.8.3/lib/httpclient.rb:1014:in `do_request'
      # /usr/local/bundle/gems/httpclient-2.8.3/lib/httpclient.rb:856:in `request'
      # /usr/local/bundle/gems/faraday-0.15.2/lib/faraday/adapter/httpclient.rb:38:in `call'
      # /usr/local/bundle/gems/faraday-0.15.2/lib/faraday/rack_builder.rb:143:in `build_response'
      # /usr/local/bundle/gems/faraday-0.15.2/lib/faraday/connection.rb:387:in `run_request'
      # /usr/local/bundle/gems/elasticsearch-transport-6.1.0/lib/elasticsearch/transport/transport/http/faraday.rb:23:in `block in perform_request'
      # /usr/local/bundle/gems/elasticsearch-transport-6.1.0/lib/elasticsearch/transport/transport/base.rb:266:in `perform_request'
      # /usr/local/bundle/gems/elasticsearch-transport-6.1.0/lib/elasticsearch/transport/transport/http/faraday.rb:20:in `perform_request'
      # /usr/local/bundle/gems/elasticsearch-transport-6.1.0/lib/elasticsearch/transport/client.rb:131:in `perform_request'
      # /usr/local/bundle/gems/elasticsearch-api-6.1.0/lib/elasticsearch/api/namespace/common.rb:21:in `perform_request'
      # /usr/local/bundle/gems/elasticsearch-api-6.1.0/lib/elasticsearch/api/actions/indices/exists.rb:46:in `block in exists'
      # /usr/local/bundle/gems/elasticsearch-api-6.1.0/lib/elasticsearch/api/utils.rb:202:in `__rescue_from_not_found'
      # /usr/local/bundle/gems/elasticsearch-api-6.1.0/lib/elasticsearch/api/actions/indices/exists.rb:45:in `exists'
      # /usr/local/bundle/gems/chewy-5.0.0/lib/chewy/index/actions.rb:15:in `exists?'
      # /usr/local/bundle/gems/chewy-5.0.0/lib/chewy/type/import/routine.rb:69:in `create_indexes!'
      # /usr/local/bundle/gems/chewy-5.0.0/lib/chewy/type/import.rb:131:in `import_routine'
      # /usr/local/bundle/gems/chewy-5.0.0/lib/chewy/type/import.rb:87:in `import!'
      # /usr/local/bundle/gems/chewy-5.0.0/lib/chewy/strategy/urgent.rb:13:in `update'
      # /usr/local/bundle/gems/chewy-5.0.0/lib/chewy/type/observe.rb:76:in `update_index'
      # /usr/local/bundle/gems/chewy-5.0.0/lib/chewy/type/observe.rb:34:in `block in update_proc'
      # /usr/local/bundle/gems/bugsnag-6.7.0/lib/bugsnag/integrations/rails/active_record_rescue.rb:25:in `run_callbacks'
      # /usr/local/bundle/gems/airbrake-6.2.1/lib/airbrake/rails/active_record.rb:22:in `run_callbacks'
      # ./app/models/payment.rb:176:in `update_with_payment_response_data'
      # ./app/models/checkout/payment_form.rb:93:in `block in process'
      # ./lib/record_lock.rb:34:in `with_lock'
      # ./lib/record_lock.rb:27:in `with_lock'
      # ./app/models/checkout/payment_form.rb:72:in `process'
      # ./spec/models/checkout/payment_form_spec.rb:532:in `block (7 levels) in <main>'


      From what I could debug, the exception happens when Chewy tried to update the index using Faraday HTTP client.



      I'm also using WebMock, but I'm calling WebMock.allow_net_connect! for each thread, making it permissive. Note that this only happens when I use threads. Without threads that runs fine.










      share|improve this question














      I'm testing some concurrent behaviours using RSpec and a weird exception is raised.



      expect{
      threads = 4.times.map do
      Thread.new do
      WebMock.allow_net_connect!
      PaymentForm.new(params).process
      end
      end

      threads.each(&:join)
      }.to change{
      Payment.count
      }.by(1)


      After the PaymentForm#process method is called, it creates a Payment (model) record. The Payment model is indexed using Chewy on ElasticSearch, updating the index right after the model is saved.



      It turns out that a weird exception is raised:



        1) Checkout::PaymentForm#process when the payment is duplicated blocks repeated transactions
      Failure/Error: unless save

      NoMethodError:
      undefined method `get' for nil:NilClass
      Did you mean? gem
      # /usr/local/bundle/gems/httpclient-2.8.3/lib/httpclient/session.rb:726:in `set_header'
      # /usr/local/bundle/gems/webmock-3.4.2/lib/webmock/http_lib_adapters/httpclient_adapter.rb:206:in `headers_from_session'
      # /usr/local/bundle/gems/webmock-3.4.2/lib/webmock/http_lib_adapters/httpclient_adapter.rb:168:in `build_request_signature'
      # /usr/local/bundle/gems/webmock-3.4.2/lib/webmock/http_lib_adapters/httpclient_adapter.rb:55:in `do_get'
      # /usr/local/bundle/gems/webmock-3.4.2/lib/webmock/http_lib_adapters/httpclient_adapter.rb:47:in `do_get_block'
      # /usr/local/bundle/gems/httpclient-2.8.3/lib/httpclient.rb:1019:in `block in do_request'
      # /usr/local/bundle/gems/httpclient-2.8.3/lib/httpclient.rb:1133:in `protect_keep_alive_disconnected'
      # /usr/local/bundle/gems/httpclient-2.8.3/lib/httpclient.rb:1014:in `do_request'
      # /usr/local/bundle/gems/httpclient-2.8.3/lib/httpclient.rb:856:in `request'
      # /usr/local/bundle/gems/faraday-0.15.2/lib/faraday/adapter/httpclient.rb:38:in `call'
      # /usr/local/bundle/gems/faraday-0.15.2/lib/faraday/rack_builder.rb:143:in `build_response'
      # /usr/local/bundle/gems/faraday-0.15.2/lib/faraday/connection.rb:387:in `run_request'
      # /usr/local/bundle/gems/elasticsearch-transport-6.1.0/lib/elasticsearch/transport/transport/http/faraday.rb:23:in `block in perform_request'
      # /usr/local/bundle/gems/elasticsearch-transport-6.1.0/lib/elasticsearch/transport/transport/base.rb:266:in `perform_request'
      # /usr/local/bundle/gems/elasticsearch-transport-6.1.0/lib/elasticsearch/transport/transport/http/faraday.rb:20:in `perform_request'
      # /usr/local/bundle/gems/elasticsearch-transport-6.1.0/lib/elasticsearch/transport/client.rb:131:in `perform_request'
      # /usr/local/bundle/gems/elasticsearch-api-6.1.0/lib/elasticsearch/api/namespace/common.rb:21:in `perform_request'
      # /usr/local/bundle/gems/elasticsearch-api-6.1.0/lib/elasticsearch/api/actions/indices/exists.rb:46:in `block in exists'
      # /usr/local/bundle/gems/elasticsearch-api-6.1.0/lib/elasticsearch/api/utils.rb:202:in `__rescue_from_not_found'
      # /usr/local/bundle/gems/elasticsearch-api-6.1.0/lib/elasticsearch/api/actions/indices/exists.rb:45:in `exists'
      # /usr/local/bundle/gems/chewy-5.0.0/lib/chewy/index/actions.rb:15:in `exists?'
      # /usr/local/bundle/gems/chewy-5.0.0/lib/chewy/type/import/routine.rb:69:in `create_indexes!'
      # /usr/local/bundle/gems/chewy-5.0.0/lib/chewy/type/import.rb:131:in `import_routine'
      # /usr/local/bundle/gems/chewy-5.0.0/lib/chewy/type/import.rb:87:in `import!'
      # /usr/local/bundle/gems/chewy-5.0.0/lib/chewy/strategy/urgent.rb:13:in `update'
      # /usr/local/bundle/gems/chewy-5.0.0/lib/chewy/type/observe.rb:76:in `update_index'
      # /usr/local/bundle/gems/chewy-5.0.0/lib/chewy/type/observe.rb:34:in `block in update_proc'
      # /usr/local/bundle/gems/bugsnag-6.7.0/lib/bugsnag/integrations/rails/active_record_rescue.rb:25:in `run_callbacks'
      # /usr/local/bundle/gems/airbrake-6.2.1/lib/airbrake/rails/active_record.rb:22:in `run_callbacks'
      # ./app/models/payment.rb:176:in `update_with_payment_response_data'
      # ./app/models/checkout/payment_form.rb:93:in `block in process'
      # ./lib/record_lock.rb:34:in `with_lock'
      # ./lib/record_lock.rb:27:in `with_lock'
      # ./app/models/checkout/payment_form.rb:72:in `process'
      # ./spec/models/checkout/payment_form_spec.rb:532:in `block (7 levels) in <main>'


      From what I could debug, the exception happens when Chewy tried to update the index using Faraday HTTP client.



      I'm also using WebMock, but I'm calling WebMock.allow_net_connect! for each thread, making it permissive. Note that this only happens when I use threads. Without threads that runs fine.







      ruby-on-rails multithreading rspec chewy-gem






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 20 '18 at 11:15









      vnbrsvnbrs

      1,1781722




      1,1781722
























          0






          active

          oldest

          votes











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53391807%2fchewy-rspec-and-threads-raising-a-weird-error%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53391807%2fchewy-rspec-and-threads-raising-a-weird-error%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          MongoDB - Not Authorized To Execute Command

          How to fix TextFormField cause rebuild widget in Flutter

          in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith