Last active
April 20, 2020 19:03
-
-
Save thbar/c68d671276c390b2aed3bed75b8cf0e0 to your computer and use it in GitHub Desktop.
How to start and stop Puma programmatically (e.g. from a test)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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