Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RUBY-1934 clean up DNS server termination #2903

Merged
merged 5 commits into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.

Loading