async_sinatra 0.2.3 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (4) hide show
  1. data/Rakefile +1 -0
  2. data/lib/sinatra/async.rb +22 -13
  3. data/test/test_async.rb +35 -1
  4. metadata +3 -3
data/Rakefile CHANGED
@@ -93,4 +93,5 @@ desc "Release #{gem_task.gem_spec.file_name}"
93
93
  task :release => [:tag, :gem, :publish] do |t|
94
94
  sh "rubyforge add_release #{spec.rubyforge_project} #{spec.name} #{spec.version} #{gem_task.package_dir}/#{gem_task.gem_spec.file_name}"
95
95
  sh "gem push #{gem_task.package_dir}/#{gem_task.gem_spec.file_name}"
96
+ sh "git push --all --tags"
96
97
  end
data/lib/sinatra/async.rb CHANGED
@@ -85,32 +85,41 @@ module Sinatra #:nodoc:
85
85
  end
86
86
  end
87
87
 
88
- # By default async_schedule calls EventMachine#next_tick, if you're using
89
- # threads or some other scheduling mechanism, it must take the block
90
- # passed here.
88
+ # async_schedule is used to schedule work in a future context, the block
89
+ # is wrapped up so that exceptions and halts (redirect, etc) are handled
90
+ # correctly.
91
91
  def async_schedule(&b)
92
92
  if options.environment == :test
93
93
  options.set :async_schedules, [] unless options.respond_to? :async_schedules
94
- options.async_schedules << b
94
+ options.async_schedules << lambda { async_catch_execute(&b) }
95
95
  else
96
- EM.next_tick(&b)
96
+ native_async_schedule { async_catch_execute(&b) }
97
97
  end
98
98
  end
99
99
 
100
+ # By default native_async_schedule calls EventMachine#next_tick, if
101
+ # you're using threads or some other scheduling mechanism, it must take
102
+ # the block passed here and schedule it for running in the future.
103
+ def native_async_schedule(&b)
104
+ EM.next_tick(&b)
105
+ end
106
+
100
107
  # Defaults to throw async as that is most commonly used by servers.
101
108
  def async_response
102
109
  throw :async
103
110
  end
104
111
 
105
112
  def async_runner(method, *bargs)
106
- async_schedule do
107
- @async_running = true
108
- async_handle_exception do
109
- if h = catch(:halt) { __send__(method, *bargs); nil }
110
- invoke { halt h }
111
- invoke { error_block! response.status }
112
- body(response.body)
113
- end
113
+ async_schedule { __send__(method, *bargs); nil }
114
+ end
115
+
116
+ def async_catch_execute(&b)
117
+ @async_running = true
118
+ async_handle_exception do
119
+ if h = catch(:halt, &b)
120
+ invoke { halt h }
121
+ invoke { error_block! response.status }
122
+ body(response.body)
114
123
  end
115
124
  end
116
125
  end
data/test/test_async.rb CHANGED
@@ -43,7 +43,7 @@ class TestSinatraAsync < Test::Unit::TestCase
43
43
  end
44
44
 
45
45
  aget '/em_halt' do
46
- EM.next_tick { ahalt 404 }
46
+ em_async_schedule { ahalt 404 }
47
47
  end
48
48
 
49
49
  aget '/s401' do
@@ -69,6 +69,25 @@ class TestSinatraAsync < Test::Unit::TestCase
69
69
  aget '/redirect' do
70
70
  redirect '/'
71
71
  end
72
+
73
+ aget '/aredirect' do
74
+ async_schedule { redirect '/' }
75
+ end
76
+
77
+ aget '/emredirect' do
78
+ em_async_schedule { redirect '/' }
79
+ end
80
+
81
+ # Defeat the test environment semantics, ensuring we actually follow the
82
+ # non-test branch of async_schedule. You would normally just call
83
+ # async_schedule in user apps, and use test helpers appropriately.
84
+ def em_async_schedule
85
+ o = self.class.environment
86
+ self.class.set :environment, :normal
87
+ async_schedule { yield }
88
+ ensure
89
+ self.class.set :environment, o
90
+ end
72
91
  end
73
92
 
74
93
  def app
@@ -154,4 +173,19 @@ class TestSinatraAsync < Test::Unit::TestCase
154
173
  assert_equal 302, last_response.status
155
174
  assert_equal '/', last_response.location
156
175
  end
176
+
177
+ def test_aredirect
178
+ aget '/aredirect'
179
+ assert last_response.redirect?
180
+ assert_equal 302, last_response.status
181
+ assert_equal '/', last_response.location
182
+ end
183
+
184
+ def test_emredirect
185
+ aget '/emredirect'
186
+ em_async_continue
187
+ assert last_response.redirect?
188
+ assert_equal 302, last_response.status
189
+ assert_equal '/', last_response.location
190
+ end
157
191
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: async_sinatra
3
3
  version: !ruby/object:Gem::Version
4
- hash: 17
4
+ hash: 19
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
- - 2
9
8
  - 3
10
- version: 0.2.3
9
+ - 0
10
+ version: 0.3.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - James Tucker