socketry/rubydns

How to stop the server?

Closed this issue · 7 comments

Thank you for RubyDNS, we are using it to mock DNS lookups in the test suite. To this end we need to stop the server at the end of a test using it, as well as have the ability replace the server with another one with different configuration. Looking through the code, I think task.stop for the task created in https://github.com/socketry/async-dns/blob/master/lib/async/dns/server.rb#L105 is going to stop the server, but the task does not appear to be exposed even in that method not to mention RubyDNS.

What is the recommended way of stopping a RubyDNS server?

#!/usr/bin/env ruby
require 'rubydns'

INTERFACES = [
	[:udp, "0.0.0.0", 5300],
	[:tcp, "0.0.0.0", 5300],
]

IN = Resolv::DNS::Resource::IN

# Use upstream DNS for name resolution.
UPSTREAM = RubyDNS::Resolver.new([[:udp, "8.8.8.8", 53], [:tcp, "8.8.8.8", 53]])

Async do |task|
	# Start the RubyDNS server
	server = RubyDNS::run_server(INTERFACES) do
		match(%r{test.local}, IN::A) do |transaction|
			transaction.respond!("10.0.0.80")
		end

		# Default DNS handler
		otherwise do |transaction|
			transaction.passthrough!(UPSTREAM)
		end
	end
	
	task.sleep(2)
	server.stop
end

Just call server.stop. server is returned by run_server. It's a Async::Task.

To make your life easier, you should probably also use async-rspec.

https://github.com/socketry/async-rspec

OK, I gather this puts the application code into the event loop managed by rubydns, and I think I would like the opposite behavior of influencing rubydns's event loop from my application.

I currently have this helper: https://github.com/p-mongo/mongo-ruby-driver/blob/1563/spec/support/common_shortcuts.rb#L198 and this hack to go with it: https://github.com/p-mongo/mongo-ruby-driver/blob/1563/spec/support/dns.rb. The helper creates a background thread which runs the event loop without interfering with the main thread of the application.

For my future reference, async is this: https://github.com/socketry/async#async

Yes your only other option is to run it in a thread or process. Use async-container.

https://github.com/socketry/async-container

If this work is being used in a commercial environment, please consider the sponsorship option under our community funding guidelines: https://github.com/socketry/community#funding