Skip to content

Commit

Permalink
RUBY-1934 clean up DNS server termination (#2903)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
jamis authored Nov 5, 2024
1 parent 45bfc81 commit 4b9b347
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 62 deletions.
4 changes: 2 additions & 2 deletions spec/integration/reconnect_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,8 @@
# thread.kill should've similarly failed, but it doesn't.
fails_on_jruby

minimum_mri_version '3.0.0'

it 'recreates SRV monitor' do
wait_for_discovery

Expand Down Expand Up @@ -181,8 +183,6 @@
end

around do |example|
require 'support/dns'

rules = [
['_mongodb._tcp.test-fake.test.build.10gen.cc', :srv,
[0, 0, 2799, 'localhost.test.build.10gen.cc'],
Expand Down
4 changes: 1 addition & 3 deletions spec/integration/srv_monitoring_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,7 @@
# NotImplementedError: recvmsg_nonblock is not implemented
fails_on_jruby

before(:all) do
require 'support/dns'
end
minimum_mri_version '3.0.0'

around do |example|
# Speed up the tests by listening on the fake ports we are using.
Expand Down
4 changes: 0 additions & 4 deletions spec/integration/srv_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,6 @@
# NotImplementedError: recvmsg_nonblock is not implemented
fails_on_jruby

before(:all) do
require 'support/dns'
end

let(:uri) do
"mongodb+srv://test-fake.test.build.10gen.cc/?tls=#{SpecConfig.instance.ssl?}&tlsInsecure=true"
end
Expand Down
56 changes: 19 additions & 37 deletions spec/support/common_shortcuts.rb
Original file line number Diff line number Diff line change
Expand Up @@ -337,51 +337,33 @@ def stop_monitoring(*clients)
[:tcp, "0.0.0.0", 5300],
]

def mock_dns(config)
semaphore = Mongo::Semaphore.new

thread = Thread.new do
RubyDNS::run_server(DNS_INTERFACES) do
config.each do |(query, type, *answers)|

resource_cls = Resolv::DNS::Resource::IN.const_get(type.to_s.upcase)
resources = answers.map do |answer|
resource_cls.new(*answer)
end
match(query, resource_cls) do |req|
req.add(resources)
end
# Starts the DNS server and returns it; should be run from within an
# Async block. Prefer #mock_dns instead, which does the setup for you.
def start_dns_server(config)
RubyDNS::run_server(DNS_INTERFACES) do
config.each do |(query, type, *answers)|
resource_cls = Resolv::DNS::Resource::IN.const_get(type.to_s.upcase)
resources = answers.map do |answer|
resource_cls.new(*answer)
end

semaphore.signal
match(query, resource_cls) do |req|
req.add(resources)
end
end
end
end

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

begin
Async do |task|
server = start_dns_server(config)
yield
ensure
10.times do
if $last_async_task
break
end
sleep 0.5
end

# Hack to stop the server - https://github.com/socketry/rubydns/issues/75
if $last_async_task.nil?
STDERR.puts "No async task - server never started?"
else
begin
$last_async_task.stop
rescue NoMethodError => e
STDERR.puts "Error stopping async task: #{e}"
end
end

thread.kill
thread.join
server.stop
end
end

Expand Down
10 changes: 10 additions & 0 deletions spec/support/constraints.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,16 @@ def require_local_tls
end
end

def minimum_mri_version(version)
require_mri

before(:all) do
if RUBY_VERSION < version
skip "Ruby #{version} or greater is required"
end
end
end

def forbid_x509_auth
before(:all) do
skip 'X.509 auth not allowed' if SpecConfig.instance.x509_auth?
Expand Down
16 changes: 0 additions & 16 deletions spec/support/dns.rb

This file was deleted.

0 comments on commit 4b9b347

Please sign in to comment.