diff --git a/lib/ddtrace/workers.rb b/lib/ddtrace/workers.rb index fb3fd6fab0..55db957c41 100644 --- a/lib/ddtrace/workers.rb +++ b/lib/ddtrace/workers.rb @@ -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 @@ -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. diff --git a/test/integration_test.rb b/test/integration_test.rb index 4a09f01bbe..1fcbae058a 100644 --- a/test/integration_test.rb +++ b/test/integration_test.rb @@ -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') @@ -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')