-
Notifications
You must be signed in to change notification settings - Fork 555
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
webmock failures inside sidekiq workers not bubbled up to rspec #894
Comments
I tried adding a
So, I think I really only want to webmock specific requests -- this doesn't seem like the right path. |
Continuing my troubleshooting, I tried the following in the test: WebMock.disable_net_connect!(allow_localhost: true) This allowed the ElasticSearch things to work, but I still had the problem of Sidekiq's call of Faraday not being intercepted:
So this appears to me to definitely be some kind of issue with Sidekiq's test environment not being affected by webmock. |
This might be related to #578 because I'm using a connection object. I'm currently trying to figure out how to NOT use a connection object (to validate the problem) but still make the same configuration options. |
I tried switching to Excon because of #578 and I am seeing the same behavior. Worker code: class TokenizeAndVectorizeWorker
include Sidekiq::Worker
sidekiq_options queue: 'tokenizer_vectorizer', retry: true, backtrace: true
def perform(article_id)
article = Article.find(article_id)
tokenizer_url = ENV['TOKENIZER_URL']
if article.content.nil?
send_content = article.abstract
else
send_content = article.content
end
response = Excon.post("#{tokenizer_url}/tokenize",
:body => URI.encode_www_form(:content => send_content),
:headers => { "Content-Type" => "application/x-www-form-urlencoded" },
:expects => [200])
# the response's body contains the JSON for the tokenized and vectorized article content
article.token_vector = response.body
article.save
end
end Test: it "should fetch token vector for article content when content is not nil" do
# don't wait for async sidekiq job
Sidekiq::Testing.inline!
require 'webmock/rspec'
request_url = "#{ENV['TOKENIZER_URL']'}tokenize"
# webmock the expected request and response
stub = stub_request(:post, request_url)
.with(body: 'content=y')
.to_return(body: 'y')
# create an attrs to hand to ingest
attrs = {
data_source: @data_source,
title: Faker::Book.title,
url: Faker::Internet.url,
content: "y",
abstract: Faker::Book.genre,
published_on: DateTime.now,
created_at: DateTime.now
}
# ingest an article from the attrs
status = Article.ingest(attrs)
# the ingest occurs roughly simultaneously to the submission to the
# worker so we need to re-fetch the article by the id because at that
# point it will have gotten the vector saved to the DB
@token_vector_article = Article.find(status[1].id)
# we should have sent a request with content=y
expect(stub).to have_been_requested
# we should've saved "y" as the token_vector
expect(@token_vector_article.token_vector).not_to eq(nil)
expect(@token_vector_article.token_vector).to eq("y")
end Result:
|
How could I use |
I switched to using excon and simplified things with a service class, so closing this. |
I'm reopening this because I ran into it again in a different place. I have several workers that interact with the internet. I don't think I should have to use service classes for webmock to intercept traffic happening inside a Sidekiq worker. Or there's something I don't understand about how to make it do so. Simply put, if you have a model that does |
This is related to #897 now. When I enable and then disable, I see the webmock failures in the logs, but they are not exposed up to rspec. In other words, the request fails properly:
But the test is considered passed because the failure inside the worker doesn't bubble up to rspec:
I'm not sure if this is expected or not. |
Using webmock 3.8.3 with ruby 2.6.6 and rails 5.2.4
This is related to #897 now.
When I enable and then disable, I see the webmock failures in the logs, but they are not exposed up to rspec.
In other words, the request fails properly:
But the test is considered passed because the failure inside the worker doesn't bubble up to rspec:
I'm not sure if this is expected or not.
I also added the above as the last comment. The rest of this post and my other comments is mostly background details that aren't totally relevant at this point. I also updated the title of the issue to reflect the "real" problem, as it was mostly user error the first threve-teen go throughs.
Test scenario (rspec):
Result:
You can see that the Sidekiq worker is being executed because its log messages are showing (before connection / after connection)
Here's the code in the worker:
You can see where I am making the Faraday call and it is definitely reaching out to the remote system because I get back an expected output from that remote system:
If I put
require 'webmock/rspec'
at the top of this particular test file or even in myspec_helper
, it causes other things in my test to blow up (namely the integration with Elasticsearch and setting up the tests):Result:
It seems to "work" with
webmock/rspec
loaded in the specific test in the sense that it's clearly showing what webmock expects. However it's not detecting any requests being made, let alone the wrong request.The text was updated successfully, but these errors were encountered: