mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Simplify parts and tests
This commit is contained in:
parent
d54d97b07c
commit
b2f98c13a3
4 changed files with 25 additions and 34 deletions
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue