Skip to content

Commit

Permalink
Fixes check for shutdown method so that traces send when buffer is cl…
Browse files Browse the repository at this point in the history
…eared but the traces are still being sent. Updates test to use a thread to make sure shutdown is not called more than once
  • Loading branch information
Rachel Lo committed Sep 29, 2017
1 parent 98a826c commit 47b0dd2
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 11 deletions.
3 changes: 2 additions & 1 deletion lib/ddtrace/workers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ def stop

# Closes all available queues and waits for the trace and service buffer to flush
def shutdown!
return if @shutting_down || (@trace_buffer.empty? && @service_buffer.empty?)
return false if @shutting_down
@shutting_down = true
@trace_buffer.close
@service_buffer.close
Expand All @@ -91,6 +91,7 @@ def shutdown!
stop
join
@shutting_down = false
true
end

# Block until executor shutdown is complete or until timeout seconds have passed.
Expand Down
29 changes: 19 additions & 10 deletions test/integration_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,10 @@ def agent_receives_short_span(tracer)
span.service = 'my.service'
span.finish()

tracer.shutdown!
first_shutdown = tracer.shutdown!

stats = tracer.writer.stats
assert(first_shutdown, 'should have run through the shutdown method')
assert(span.finished?, 'span did not finish')
assert_equal(1, stats[:traces_flushed], 'wrong number of traces flushed')
assert_equal(1, stats[:services_flushed], 'wrong number of services flushed')
Expand All @@ -88,19 +89,27 @@ def shutdown_exec_only_once(tracer)
span = tracer.start_span('my.short.op')
span.service = 'my.service'
span.finish()
first_shutdown = nil
second_shutdown = nil
worker = Thread.new() do
first_shutdown = tracer.shutdown!
end

100.times do
break if tracer.writer.worker.shutting_down # wait until first shutdown is halfway through execution
sleep(0.01)
end

first_point = Time.now.utc
tracer.shutdown!
second_point = Time.now.utc
tracer.shutdown!
third_point = Time.now.utc
second_worker = Thread.new() do
second_shutdown = tracer.shutdown!
end

first_shutdown = second_point - first_point
second_shutdown = third_point - second_point
worker.join(2)
second_worker.join(2)

stats = tracer.writer.stats
assert(first_shutdown >= 0.1, 'should have executed shutdown')
assert_equal(true, second_shutdown < 0.1, 'should not have executed second shutdown')
assert(first_shutdown, 'should have run through the shutdown method')
assert_equal(false, second_shutdown, 'should not have executed shutdown')
assert_equal(true, span.finished?, 'span did not finish')
assert_equal(1, stats[:traces_flushed], 'wrong number of traces flushed')
assert_equal(1, stats[:services_flushed], 'wrong number of services flushed')
Expand Down

0 comments on commit 47b0dd2

Please sign in to comment.