Skip to content

Commit 4b9b347

Browse files
authored
RUBY-1934 clean up DNS server termination (#2903)
* 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. * need to require rubydns * even cleaner * RubyDNS is failing oddly with Ruby 2.7, so let's just skip it
1 parent 45bfc81 commit 4b9b347

File tree

6 files changed

+32
-62
lines changed

6 files changed

+32
-62
lines changed

spec/integration/reconnect_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,8 @@
111111
# thread.kill should've similarly failed, but it doesn't.
112112
fails_on_jruby
113113

114+
minimum_mri_version '3.0.0'
115+
114116
it 'recreates SRV monitor' do
115117
wait_for_discovery
116118

@@ -181,8 +183,6 @@
181183
end
182184

183185
around do |example|
184-
require 'support/dns'
185-
186186
rules = [
187187
['_mongodb._tcp.test-fake.test.build.10gen.cc', :srv,
188188
[0, 0, 2799, 'localhost.test.build.10gen.cc'],

spec/integration/srv_monitoring_spec.rb

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,7 @@
7676
# NotImplementedError: recvmsg_nonblock is not implemented
7777
fails_on_jruby
7878

79-
before(:all) do
80-
require 'support/dns'
81-
end
79+
minimum_mri_version '3.0.0'
8280

8381
around do |example|
8482
# Speed up the tests by listening on the fake ports we are using.

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: 19 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -337,51 +337,33 @@ 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+
# Starts the DNS server and returns it; should be run from within an
341+
# Async block. Prefer #mock_dns instead, which does the setup for you.
342+
def start_dns_server(config)
343+
RubyDNS::run_server(DNS_INTERFACES) do
344+
config.each do |(query, type, *answers)|
345+
resource_cls = Resolv::DNS::Resource::IN.const_get(type.to_s.upcase)
346+
resources = answers.map do |answer|
347+
resource_cls.new(*answer)
354348
end
355349

356-
semaphore.signal
350+
match(query, resource_cls) do |req|
351+
req.add(resources)
352+
end
357353
end
358354
end
355+
end
359356

360-
semaphore.wait
357+
# Starts and runs a DNS server, then yields to the attached block.
358+
def mock_dns(config)
359+
# only require rubydns when we need it; it's MRI-only.
360+
require 'rubydns'
361361

362-
begin
362+
Async do |task|
363+
server = start_dns_server(config)
363364
yield
364365
ensure
365-
10.times do
366-
if $last_async_task
367-
break
368-
end
369-
sleep 0.5
370-
end
371-
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
382-
383-
thread.kill
384-
thread.join
366+
server.stop
385367
end
386368
end
387369

spec/support/constraints.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,16 @@ def require_local_tls
1717
end
1818
end
1919

20+
def minimum_mri_version(version)
21+
require_mri
22+
23+
before(:all) do
24+
if RUBY_VERSION < version
25+
skip "Ruby #{version} or greater is required"
26+
end
27+
end
28+
end
29+
2030
def forbid_x509_auth
2131
before(:all) do
2232
skip 'X.509 auth not allowed' if SpecConfig.instance.x509_auth?

spec/support/dns.rb

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

0 commit comments

Comments
 (0)