Skip to content

Commit d872044

Browse files
committed
fix broken resolver specs
1 parent 50212e3 commit d872044

File tree

5 files changed

+19
-63
lines changed

5 files changed

+19
-63
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: 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/dns.rb

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

0 commit comments

Comments
 (0)