aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack
diff options
context:
space:
mode:
authorJeremy Kemper <jeremy@bitsweat.net>2009-03-13 03:16:14 -0700
committerJeremy Kemper <jeremy@bitsweat.net>2009-03-13 03:16:14 -0700
commitb2f98c13a3308e6db618d6f3f9f706cbc19f13fd (patch)
treeae4f0355d8e9150e25b33090e6abbd26bd4165d6 /actionpack
parentd54d97b07c1b258f42210b1a3fc2e8c56e434ee9 (diff)
downloadrails-b2f98c13a3308e6db618d6f3f9f706cbc19f13fd.tar.gz
rails-b2f98c13a3308e6db618d6f3f9f706cbc19f13fd.tar.bz2
rails-b2f98c13a3308e6db618d6f3f9f706cbc19f13fd.zip
Simplify parts and tests
Diffstat (limited to 'actionpack')
-rw-r--r--actionpack/lib/action_view/body_parts/future.rb16
-rw-r--r--actionpack/lib/action_view/body_parts/queued.rb13
-rw-r--r--actionpack/lib/action_view/body_parts/threaded.rb13
-rw-r--r--actionpack/test/template/body_parts_test.rb17
4 files changed, 25 insertions, 34 deletions
diff --git a/actionpack/lib/action_view/body_parts/future.rb b/actionpack/lib/action_view/body_parts/future.rb
index a5291c6e8c..f03c2b395b 100644
--- a/actionpack/lib/action_view/body_parts/future.rb
+++ b/actionpack/lib/action_view/body_parts/future.rb
@@ -1,26 +1,10 @@
module ActionView
module BodyParts
class Future
- def initialize(&block)
- @block = block
- @parts = []
- end
-
def to_s
finish
body
end
-
- protected
- def work
- @block.call(@parts)
- end
-
- def body
- str = ''
- @parts.each { |part| str << part.to_s }
- str
- end
end
end
end
diff --git a/actionpack/lib/action_view/body_parts/queued.rb b/actionpack/lib/action_view/body_parts/queued.rb
index f8501f6a85..618999742b 100644
--- a/actionpack/lib/action_view/body_parts/queued.rb
+++ b/actionpack/lib/action_view/body_parts/queued.rb
@@ -3,18 +3,15 @@ require 'action_view/body_parts/future'
module ActionView
module BodyParts
class Queued < Future
- def initialize(job, &block)
- super(&block)
- enqueue(job)
+ attr_reader :body
+
+ def initialize(job)
+ @receipt = enqueue(job)
end
protected
- def enqueue(job)
- @receipt = submit(job)
- end
-
def finish
- @parts << redeem(@receipt)
+ @body = redeem(@receipt)
end
end
end
diff --git a/actionpack/lib/action_view/body_parts/threaded.rb b/actionpack/lib/action_view/body_parts/threaded.rb
index 34800bf9c7..a2347a2f0e 100644
--- a/actionpack/lib/action_view/body_parts/threaded.rb
+++ b/actionpack/lib/action_view/body_parts/threaded.rb
@@ -4,11 +4,22 @@ module ActionView
module BodyParts
class Threaded < Future
def initialize(concurrent = false, &block)
- super(&block)
+ @block = block
+ @parts = []
concurrent ? start : work
end
protected
+ def work
+ @block.call(@parts)
+ end
+
+ def body
+ str = ''
+ @parts.each { |part| str << part.to_s }
+ str
+ end
+
def start
@worker = Thread.new { work }
end
diff --git a/actionpack/test/template/body_parts_test.rb b/actionpack/test/template/body_parts_test.rb
index 7418f4a716..4555a6b015 100644
--- a/actionpack/test/template/body_parts_test.rb
+++ b/actionpack/test/template/body_parts_test.rb
@@ -47,7 +47,7 @@ class QueuedPartTest < ActionController::TestCase
end
protected
- def submit(job)
+ def enqueue(job)
job.reverse
end
@@ -114,12 +114,11 @@ class ThreadedPartTest < ActionController::TestCase
def test_concurrent_threaded_parts
get :index
- before = Time.now.to_i
- thread_ids = @response.body.split('-').map { |part| part.split('::').first.to_i }
- elapsed = Time.now.to_i - before
-
- assert_equal thread_ids.size, thread_ids.uniq.size
- assert elapsed < 1.1
+ elapsed = Benchmark.ms do
+ thread_ids = @response.body.split('-').map { |part| part.split('::').first.to_i }
+ assert_equal thread_ids.size, thread_ids.uniq.size
+ end
+ assert (elapsed - 1000).abs < 100, elapsed
end
end
@@ -142,7 +141,7 @@ class OpenUriPartTest < ActionController::TestCase
def render_url(url)
url = URI.parse(url)
- def url.read; path end
+ def url.read; sleep 1; path end
response.template.punctuate_body! OpenUriPart.new(url)
end
end
@@ -155,6 +154,6 @@ class OpenUriPartTest < ActionController::TestCase
elapsed = Benchmark.ms do
assert_equal '/foo/bar/baz', @response.body
end
- assert elapsed < 1.1
+ assert (elapsed - 1000).abs < 100, elapsed
end
end