Skip to content

Instantly share code, notes, and snippets.

@johnbintz
Created July 8, 2013 16:07
Show Gist options
  • Save johnbintz/5950149 to your computer and use it in GitHub Desktop.
Save johnbintz/5950149 to your computer and use it in GitHub Desktop.
Totally untested way to run Rack and a LiveReload listener for rack-livereload testing
LIVERELOAD_PORT = 50000
app = Rack::LiveReload.new(
lambda { |env|
[
200,
{ 'Content-Type' => 'text/html' },
[ <<-HTML ]
<html>
<head>
<title>A page to livereload</title>
</head>
<body>
Here I am
</body>
</html>
HTML
]
}, :port => LIVERELOAD_PORT
)
rack = Thread.new do
Rack::Handler::WEBrick.run(app, :Port => 3001) do |server|
Thread.current[:running] = true
while !Thread.current[:stop]
sleep 1
end
server.stop
end
end
require 'socket'
livereload = Thread.new do
server = TCPServer.new 'localhost', LIVERELOAD_PORT
Thread.current[:connected] = false
server.accept
Thread.current[:connected] = true
while !Thread.current[:stop]
sleep 1
end
end
while !rack[:running]
sleep 1
end
# interact with the rack server via capybara or something else
# stop the rack thread
rack[:stop] = true
livereload[:connected].should be_true
# stop the lievreload thread
livereload[:stop] = true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment