diff options
author | John Keiser <jkeiser@opscode.com> | 2014-05-19 10:36:26 -0700 |
---|---|---|
committer | John Keiser <jkeiser@opscode.com> | 2014-05-19 10:36:26 -0700 |
commit | 15d4ff065806478d1c20bf78ce7c2a4c2fb74022 (patch) | |
tree | cafcc3d38d2b68b10f4a52094d395373acf35c99 | |
parent | 9f0bb81b3c8f873456a8e18eff3bee24c573ac8c (diff) | |
download | chef-15d4ff065806478d1c20bf78ce7c2a4c2fb74022.tar.gz |
Do not provide "lazy" unless it is available (Ruby 2.0+)
-rw-r--r-- | lib/chef/chef_fs/parallelizer/parallel_enumerable.rb | 40 | ||||
-rw-r--r-- | spec/unit/chef_fs/parallelizer.rb | 62 |
2 files changed, 65 insertions, 37 deletions
diff --git a/lib/chef/chef_fs/parallelizer/parallel_enumerable.rb b/lib/chef/chef_fs/parallelizer/parallel_enumerable.rb index 46a01cbb4c..7354bc5c82 100644 --- a/lib/chef/chef_fs/parallelizer/parallel_enumerable.rb +++ b/lib/chef/chef_fs/parallelizer/parallel_enumerable.rb @@ -112,31 +112,33 @@ class Chef restricted_copy(@input_enumerable.take(n)).to_a end - class RestrictedLazy - def initialize(parallel_enumerable, actual_lazy) - @parallel_enumerable = parallel_enumerable - @actual_lazy = actual_lazy - end + if Enumerable.method_defined?(:lazy) + class RestrictedLazy + def initialize(parallel_enumerable, actual_lazy) + @parallel_enumerable = parallel_enumerable + @actual_lazy = actual_lazy + end - def drop(*args, &block) - input = @parallel_enumerable.input_enumerable.lazy.drop(*args, &block) - @parallel_enumerable.restricted_copy(input) - end + def drop(*args, &block) + input = @parallel_enumerable.input_enumerable.lazy.drop(*args, &block) + @parallel_enumerable.restricted_copy(input) + end - def take(*args, &block) - input = @parallel_enumerable.input_enumerable.lazy.take(*args, &block) - @parallel_enumerable.restricted_copy(input) - end + def take(*args, &block) + input = @parallel_enumerable.input_enumerable.lazy.take(*args, &block) + @parallel_enumerable.restricted_copy(input) + end - def method_missing(method, *args, &block) - @actual_lazy.send(:method, *args, &block) + def method_missing(method, *args, &block) + @actual_lazy.send(:method, *args, &block) + end end - end - alias :original_lazy :lazy + alias :original_lazy :lazy - def lazy - RestrictedLazy.new(self, original_lazy) + def lazy + RestrictedLazy.new(self, original_lazy) + end end private diff --git a/spec/unit/chef_fs/parallelizer.rb b/spec/unit/chef_fs/parallelizer.rb index fa13d917a7..5bc2ecfd17 100644 --- a/spec/unit/chef_fs/parallelizer.rb +++ b/spec/unit/chef_fs/parallelizer.rb @@ -361,9 +361,8 @@ describe Chef::ChefFS::Parallelizer do x end enum.take(2).should == [1,2] - enum.lazy.take(2).to_a.should == [1,2] - outputs_processed.should == 4 - input_mapper.num_processed.should == 4 + outputs_processed.should == 2 + input_mapper.num_processed.should == 2 end it ".drop does not process anything other than the last result(s)" do @@ -375,24 +374,51 @@ describe Chef::ChefFS::Parallelizer do x end enum.drop(2).should == [3,4,5,6] - enum.lazy.drop(2).to_a.should == [3,4,5,6] - outputs_processed.should == 8 - input_mapper.num_processed.should == 12 + outputs_processed.should == 4 + input_mapper.num_processed.should == 6 end - it "lazy enumerable is actually lazy" do - outputs_processed = 0 - input_mapper = InputMapper.new(1,2,3,4,5,6) - enum = parallelizer.parallelize(input_mapper) do |x| - outputs_processed += 1 - sleep(0.05) # Just enough to yield and get other inputs in the queue - x + if Enumerable.method_defined?(:lazy) + it ".lazy.take does not enumerate anything other than the first result(s)" do + outputs_processed = 0 + input_mapper = InputMapper.new(1,2,3,4,5,6) + enum = parallelizer.parallelize(input_mapper) do |x| + outputs_processed += 1 + sleep(0.05) # Just enough to yield and get other inputs in the queue + x + end + enum.lazy.take(2).to_a.should == [1,2] + outputs_processed.should == 2 + input_mapper.num_processed.should == 2 + end + + it ".drop does not process anything other than the last result(s)" do + outputs_processed = 0 + input_mapper = InputMapper.new(1,2,3,4,5,6) + enum = parallelizer.parallelize(input_mapper) do |x| + outputs_processed += 1 + sleep(0.05) # Just enough to yield and get other inputs in the queue + x + end + enum.lazy.drop(2).to_a.should == [3,4,5,6] + outputs_processed.should == 4 + input_mapper.num_processed.should == 6 + end + + it "lazy enumerable is actually lazy" do + outputs_processed = 0 + input_mapper = InputMapper.new(1,2,3,4,5,6) + enum = parallelizer.parallelize(input_mapper) do |x| + outputs_processed += 1 + sleep(0.05) # Just enough to yield and get other inputs in the queue + x + end + enum.lazy.take(2) + enum.lazy.drop(2) + sleep(0.1) + outputs_processed.should == 0 + input_mapper.num_processed.should == 0 end - enum.lazy.take(2) - enum.lazy.drop(2) - sleep(0.1) - outputs_processed.should == 0 - input_mapper.num_processed.should == 0 end end |