-
-
Save deepfryed/429572 to your computer and use it in GitHub Desktop.
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
require 'rack' | |
require 'thin' | |
module Rack | |
module Test | |
class App | |
# Attempt to run a Rack app server instance in the same process as the a | |
# 'work' block. Came about because you can't see an unsaved DB transaction | |
# inside the Rack app if it happens in another process. | |
# | |
# ==== Notes | |
# Doesn't quite work because minitest failed assertions cause it to time | |
# out and I don't know why. | |
# | |
# ==== Example | |
# require 'minitest/spec' | |
# require 'rack/test/app' | |
# | |
# describe 'MyApp' | |
# Rack::Test::App.run lambda{|env| [200, {'Content-Type' => 'text/plain'}, ['ok']]} do | |
# it 'must respond ok' do | |
# assert open('http://localhost:3000').read =~ /ok/ | |
# end | |
# end | |
# end | |
# | |
# ==== Paramaters | |
# app<Object>:: Rack 'app' responding to call. | |
# options<Hash>:: Options include host: and port:. | |
# work<Proc>:: Connect and work with the 'app'. When this block exits the app will stop. | |
def self.run app, options = {}, &work | |
host = options[:host] ||= '0.0.0.0' | |
port = options[:port] ||= '3000' | |
EM.run do | |
Thin::Server.start(app, host, port) | |
EM.defer( | |
proc{ TCPSocket.new(host, port)}, | |
# TODO: Failed assertions do not return. Why? | |
proc{ EM.defer(proc{ work.call; true}, proc{ EM.stop})} | |
) | |
EM.add_timer(5){ $stderr.puts 'Timed out completing work.'; EM.stop} | |
end | |
end | |
end # App | |
end # Test | |
end # Rack |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment