Skip to content

Instantly share code, notes, and snippets.

@PatrickLef
Created March 9, 2011 08:32
Show Gist options
  • Save PatrickLef/861886 to your computer and use it in GitHub Desktop.
Save PatrickLef/861886 to your computer and use it in GitHub Desktop.
#!/usr/bin/ruby
require 'rubygems'
gem 'sinatra', :version => "1.2.0"
require 'sinatra'
require 'mysql2/em'
gem "async_sinatra", :version => "0.5.0"
require 'sinatra/async'
require "sinatra/async/test"
require 'test/unit'
require 'rack/test'
class MyApp < Sinatra::Base
register Sinatra::Async
aget ('/') do
# client = Mysql2::EM::Client.new
#client.query("SELECT sleep(1)").callback do |result|
# body "fisk"
# end
body "fisk"
end
end
class MyAppTest < Test::Unit::TestCase
include Rack::Test::Methods
include Sinatra::Async::Test::Methods
def app
MyApp
end
def test_my_default
#client = Mysql2::EM::Client.new
#client.query("SELECT sleep(1)").callback do |result|
# EventMachine.stop
#end
get '/'
async_continue
p last_response.body
assert_equal 'Hello World!', last_response.body
end
end
#MyApp.run!
# include EM::Deferrable
# set_deferred_status :succeeded
@PatrickLef
Copy link
Author

# Patch sinatra async test to support sinatra 1.2.0
class Sinatra::Async::Test
 module Methods
   # Executes the pending asynchronous blocks, required for the
   # aget/apost/etc blocks to run.
   def async_continue

     # Changed from app.options.async_schedules.shift since sinatra 1.2.0 doesn't support it
     while b = app.async_schedules.shift
       b.call
     end
   end
 end
end

@PatrickLef
Copy link
Author

PatrickLef commented Aug 28, 2012 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment