Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions lib/segment/analytics/worker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,14 @@ def run
res = Request.new.post @write_key, @batch

@on_error.call res.status, res.error unless res.status == 200

@lock.synchronize { @batch.clear }
end
end

# public: Check whether we have outstanding requests.
#
def is_requesting?
@lock.synchronize { !@batch.empty? }
@lock.synchronize { @batch.any? }
end
end
end
Expand Down
11 changes: 9 additions & 2 deletions spec/segment/analytics/worker_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,17 +84,24 @@ class Analytics
expect(worker.is_requesting?).to eq(false)
end

# Race Condition
# When we call worker.ran it creates new Request object and
# do post request. So we expect to check a batch is running.
# In eventually function we check for 5 seconds that
# all batches finished its process.

it 'returns true if there is a current batch' do
queue = Queue.new
queue << Requested::TRACK
worker = Segment::Analytics::Worker.new(queue, 'testsecret')

Thread.new do
worker.run
expect(worker.is_requesting?).to eq(false)
expect(worker.is_requesting?).to eq(true)
end

eventually { expect(worker.is_requesting?).to eq(true) }
eventually { expect(worker.is_requesting?).to eq(false) }

end
end
end
Expand Down
4 changes: 2 additions & 2 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ module Requested

module AsyncHelper
def eventually(options = {})
timeout = options[:timeout] || 2
interval = options[:interval] || 0.1
timeout = options[:timeout] || 5 #seconds
interval = options[:interval] || 0.25 #seconds
time_limit = Time.now + timeout
loop do
begin
Expand Down