Skip to content

Commit e0b99dc

Browse files
committed
RUBY-1934 clean up DNS server termination
The ticket originally called for pulling in another dependency to manage the DNS server, but I don't think it's necessary. The async-container code would be doing essentially what we already had, just with a cleaner technique for sending the termination signal.
1 parent abb7765 commit e0b99dc

File tree

5 files changed

+27
-64
lines changed

5 files changed

+27
-64
lines changed

spec/integration/reconnect_spec.rb

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -181,8 +181,6 @@
181181
end
182182

183183
around do |example|
184-
require 'support/dns'
185-
186184
rules = [
187185
['_mongodb._tcp.test-fake.test.build.10gen.cc', :srv,
188186
[0, 0, 2799, 'localhost.test.build.10gen.cc'],

spec/integration/srv_monitoring_spec.rb

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,6 @@
7676
# NotImplementedError: recvmsg_nonblock is not implemented
7777
fails_on_jruby
7878

79-
before(:all) do
80-
require 'support/dns'
81-
end
82-
8379
around do |example|
8480
# Speed up the tests by listening on the fake ports we are using.
8581
done = false

spec/integration/srv_spec.rb

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,6 @@
1212
# NotImplementedError: recvmsg_nonblock is not implemented
1313
fails_on_jruby
1414

15-
before(:all) do
16-
require 'support/dns'
17-
end
18-
1915
let(:uri) do
2016
"mongodb+srv://test-fake.test.build.10gen.cc/?tls=#{SpecConfig.instance.ssl?}&tlsInsecure=true"
2117
end

spec/support/common_shortcuts.rb

Lines changed: 27 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -337,52 +337,41 @@ def stop_monitoring(*clients)
337337
[:tcp, "0.0.0.0", 5300],
338338
]
339339

340-
def mock_dns(config)
341-
semaphore = Mongo::Semaphore.new
342-
343-
thread = Thread.new do
344-
RubyDNS::run_server(DNS_INTERFACES) do
345-
config.each do |(query, type, *answers)|
346-
347-
resource_cls = Resolv::DNS::Resource::IN.const_get(type.to_s.upcase)
348-
resources = answers.map do |answer|
349-
resource_cls.new(*answer)
350-
end
351-
match(query, resource_cls) do |req|
352-
req.add(resources)
353-
end
340+
# A signal class for a DNS server to stop
341+
class TerminateDNSServer < RuntimeError; end
342+
343+
def run_dns_server(config, semaphore)
344+
server = RubyDNS::run_server(DNS_INTERFACES) do
345+
config.each do |(query, type, *answers)|
346+
resource_cls = Resolv::DNS::Resource::IN.const_get(type.to_s.upcase)
347+
resources = answers.map do |answer|
348+
resource_cls.new(*answer)
354349
end
355350

356-
semaphore.signal
351+
match(query, resource_cls) do |req|
352+
req.add(resources)
353+
end
357354
end
355+
356+
semaphore.signal
358357
end
358+
rescue TerminateDNSServer
359+
server&.stop
360+
end
359361

360-
semaphore.wait
362+
def mock_dns(config)
363+
semaphore = Mongo::Semaphore.new
364+
thread = Thread.new { run_dns_server(config, semaphore) }
361365

362-
begin
363-
yield
364-
ensure
365-
10.times do
366-
if $last_async_task
367-
break
368-
end
369-
sleep 0.5
370-
end
366+
# wait for the server to spin up
367+
semaphore.wait
371368

372-
# Hack to stop the server - https://github.com/socketry/rubydns/issues/75
373-
if $last_async_task.nil?
374-
STDERR.puts "No async task - server never started?"
375-
else
376-
begin
377-
$last_async_task.stop
378-
rescue NoMethodError => e
379-
STDERR.puts "Error stopping async task: #{e}"
380-
end
381-
end
369+
yield
370+
ensure
371+
return unless thread
382372

383-
thread.kill
384-
thread.join
385-
end
373+
thread.raise(TerminateDNSServer)
374+
thread.join
386375
end
387376

388377
# Wait for snapshot reads to become available to prevent this error:

spec/support/dns.rb

Lines changed: 0 additions & 16 deletions
This file was deleted.

0 commit comments

Comments
 (0)