Skip to content

Commit

Permalink
Fix rspec output and warnings
Browse files Browse the repository at this point in the history
The tests were previously outputing a ton of stuff from the logger. I've silenced the logger. Additionally, I've fixed deprecation warnings.
  • Loading branch information
jclusso committed Feb 7, 2024
1 parent 6a6b18c commit 3686bbb
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 20 deletions.
31 changes: 11 additions & 20 deletions spec/posthog/send_worker_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,32 +23,29 @@ class PostHog
end

after :all do
PostHog::Defaults::Request.send(:remove_const, :BACKOFF)
PostHog::Defaults::Request::BACKOFF = 30.0
end

it 'does not error if the request fails' do
expect do
PostHog::Transport
.any_instance
.stub(:send)
.and_return(PostHog::Response.new(-1, 'Unknown error'))
allow_any_instance_of(PostHog::Transport).to(
receive(:send).and_return(PostHog::Response.new(-1, 'Unknown error'))
)

queue = Queue.new
queue << {}
worker = described_class.new(queue, 'secret')
worker.run

expect(queue).to be_empty

PostHog::Transport.any_instance.unstub(:send)
end.to_not raise_error
end

it 'executes the error handler if the request is invalid' do
PostHog::Transport
.any_instance
.stub(:send)
.and_return(PostHog::Response.new(400, 'Some error'))
allow_any_instance_of(PostHog::Transport).to(
receive(:send).and_return(PostHog::Response.new(400, 'Some error'))
)

status = error = nil
on_error =
Expand All @@ -67,8 +64,6 @@ class PostHog
sleep 0.1 # First give thread time to spin-up.
sleep 0.01 while worker.is_requesting?

PostHog::Transport.any_instance.unstub(:send)

expect(queue).to be_empty
expect(status).to eq(400)
expect(error).to eq('Some error')
Expand Down Expand Up @@ -118,12 +113,10 @@ def bad_obj.to_json(*_args)
end

it 'returns true if there is a current batch' do
PostHog::Transport
.any_instance
.stub(:send) do
sleep(0.2)
PostHog::Response.new(200, 'Success')
end
allow_any_instance_of(PostHog::Transport).to receive(:send) do
sleep(0.2)
PostHog::Response.new(200, 'Success')
end

queue = Queue.new
queue << Requested::CAPTURE
Expand All @@ -134,8 +127,6 @@ def bad_obj.to_json(*_args)

worker_thread.join
expect(worker.is_requesting?).to eq(false)

PostHog::Transport.any_instance.unstub(:send)
end
end
end
Expand Down
6 changes: 6 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@
require 'active_support/all'
require 'webmock/rspec'

RSpec.configure do |config|
config.before(:each) do
PostHog::Logging.logger = Logger.new('/dev/null') # Suppress all logging
end
end

# Setting timezone for ActiveSupport::TimeWithZone to UTC
Time.zone = 'UTC'

Expand Down

0 comments on commit 3686bbb

Please sign in to comment.