@@ -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
0 commit comments