Skip to content

Commit

Permalink
Apply feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
GustavoCaso committed Mar 21, 2023
1 parent 98c66ca commit 748ee58
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 24 deletions.
5 changes: 4 additions & 1 deletion lib/datadog/core/remote/worker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,10 @@ def stop

thread = @thr

thread.kill if thread
if thread
thread.kill
thread.join
end

@started = false
@stopping = false
Expand Down
4 changes: 2 additions & 2 deletions sig/datadog/core/remote/worker.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ module Datadog
attr_reader thr: Thread?
attr_reader interval: Integer
attr_reader mutex: ::Thread::Mutex
attr_reader block: (^() -> untyped)
attr_reader block: (^() -> void)

def initialize: (interval: Integer) { () -> untyped } -> void
def initialize: (interval: Integer) { () -> void } -> void

def start: () -> void

Expand Down
42 changes: 23 additions & 19 deletions spec/datadog/core/remote/worker_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
require 'datadog/core/remote/worker'

RSpec.describe Datadog::Core::Remote::Worker do
let(:task) { proc { 1 + 1 } }
subject(:worker) { described_class.new(interval: 1, &task) }

describe '#initialize' do
it 'raises ArgumentError when no block is provided' do
expect do
Expand All @@ -12,37 +15,38 @@
end
end

subject(:worker) do
described_class.new(interval: 1) do
1 + 1
end
end

describe '#start' do
after { worker.stop }

it 'mark worker as started' do
expect(worker).not_to be_started
worker.start
expect(worker).to be_started
worker.stop
end

it 'acquire and release lock' do
expect(worker).to receive(:acquire_lock)
expect(worker).to receive(:release_lock)
expect(worker).to receive(:acquire_lock).at_least(:once)
expect(worker).to receive(:release_lock).at_least(:once)
worker.start
end

it 'execute block when started' do
result = []
queue = Queue.new
queue_worker = described_class.new(interval: 1) do
result << queue.pop
context 'execute block when started' do
let(:result) { [] }
let(:queue) { Queue.new }
let(:task) do
proc do
value = 1
result << value
queue << value
end
end

it 'runs block' do
worker.start
# Wait for the work task to execute once
queue.pop
expect(result).to eq([1])
end
queue_worker.start
# Unblock worker thread
queue << 1
expect(result).to eq([1])
queue_worker.stop
end
end

Expand Down
3 changes: 1 addition & 2 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,7 @@
# suite output unreadable.
if example.file_path.start_with?(
'./spec/datadog/core/workers/',
'./spec/ddtrace/workers/',
'./spec/datadog/core/remote/worker_spec.rb'
'./spec/ddtrace/workers/'
)
puts # Add newline so we get better output when the progress formatter is being used
RSpec.warning("FIXME: #{example.file_path}:#{example.metadata[:line_number]} is leaking threads")
Expand Down

0 comments on commit 748ee58

Please sign in to comment.