Skip to content

Instantly share code, notes, and snippets.

@thbar
Last active April 20, 2020 19:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thbar/c68d671276c390b2aed3bed75b8cf0e0 to your computer and use it in GitHub Desktop.
Save thbar/c68d671276c390b2aed3bed75b8cf0e0 to your computer and use it in GitHub Desktop.
How to start and stop Puma programmatically (e.g. from a test)
# inspiration -> the puma tests
require 'puma'
require 'puma/server'
def with_puma_running(app)
server = Puma::Server.new(app, Puma::Events.strings)
server.add_tcp_listener "127.0.0.1", 0
server.run
port = server.connected_port
yield(port: port)
ensure
server&.stop(true)
end
app = proc do |env|
[ 200, {"Content-Type" => "text/html"}, ["OK"] ]
end
with_puma_running(app) do |port:|
# SNIP
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment