Module: Sinatra::Async::Test::Methods

Includes:
Rack::Test::Methods
Defined in:
lib/sinatra/async/test.rb

Instance Method Summary collapse

Instance Method Details

#assert_asyncObject



73
74
75
# File 'lib/sinatra/async/test.rb', line 73

def assert_async
  assert last_response.async?, "response not asynchronous. expected a status of -1 got #{last_response.status}"
end

#async_closeObject

Simulate a user closing the connection before a response is sent.

Raises:

  • (ArgumentError)


78
79
80
81
# File 'lib/sinatra/async/test.rb', line 78

def async_close
  raise ArgumentError, 'please make a request first' unless last_request
  current_session.last_request.env['async.close'].succeed
end

#async_continueObject

Executes the pending asynchronous blocks, required for the aget/apost/etc blocks to run.



85
86
87
88
89
# File 'lib/sinatra/async/test.rb', line 85

def async_continue
  while b = settings.async_schedules.shift
    b.call
  end
end

#build_rack_mock_sessionObject

XXX move me



69
70
71
# File 'lib/sinatra/async/test.rb', line 69

def build_rack_mock_session # XXX move me
  Sinatra::Async::Test::AsyncSession.new(app)
end

#em_async_continue(timeout = 10) ⇒ Object

Crank the eventmachine loop until a response is made, or timeout after a particular period, by default 10s. If the timeout is nil, no timeout will occur.



107
108
109
110
111
112
113
114
115
# File 'lib/sinatra/async/test.rb', line 107

def em_async_continue(timeout = 10)
  timed = false
  EM.run do
    async_continue
    em_hard_loop { EM.stop unless last_response.async? }
    EM.add_timer(timeout) { timed = true; EM.stop } if timeout
  end
  assert !timed, "asynchronous timeout after #{timeout} seconds"
end

#em_hard_loopObject

Uses EM.tick_loop or a periodic timer to check for changes



118
119
120
121
122
123
124
# File 'lib/sinatra/async/test.rb', line 118

def em_hard_loop
  if EM.respond_to?(:tick_loop)
    EM.tick_loop { yield }
  else
    EM.add_periodic_timer(0.0001) { yield }
  end
end

#settingsObject



91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/sinatra/async/test.rb', line 91

def settings
  # This hack exists because sinatra is now returning a proper rack stack.
  # We might need to consider alternative approaches in future.
  app = app()
  app = app.app if app.is_a?(Sinatra::ExtendedRack)

  until app.nil? || app.is_a?(Sinatra::Base) || app.is_a?(Sinatra::Wrapper)
    app = app.instance_variable_get(:@app)
  end
  raise "Cannot determine sinatra application from #{app()}" unless app
  app.settings
end