Skip to content

Commit

Permalink
Simplify parts and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremy committed Mar 13, 2009
1 parent d54d97b commit b2f98c1
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 34 deletions.
16 changes: 0 additions & 16 deletions 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
13 changes: 5 additions & 8 deletions actionpack/lib/action_view/body_parts/queued.rb
Expand Up @@ -3,18 +3,15 @@
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
Expand Down
13 changes: 12 additions & 1 deletion actionpack/lib/action_view/body_parts/threaded.rb
Expand Up @@ -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
Expand Down
17 changes: 8 additions & 9 deletions actionpack/test/template/body_parts_test.rb
Expand Up @@ -47,7 +47,7 @@ def self.redemption_tag(receipt)
end

protected
def submit(job)
def enqueue(job)
job.reverse
end

Expand Down Expand Up @@ -114,12 +114,11 @@ def future_render(&block)
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

Expand All @@ -142,7 +141,7 @@ def index

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
Expand All @@ -155,6 +154,6 @@ def test_concurrent_open_uri_parts
elapsed = Benchmark.ms do
assert_equal '/foo/bar/baz', @response.body
end
assert elapsed < 1.1
assert (elapsed - 1000).abs < 100, elapsed
end
end

0 comments on commit b2f98c1

Please sign in to comment.