Skip to content
This repository
tag: v4.0.5
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 92 lines (79 sloc) 2.321 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
# encoding: utf-8
require 'spec_helper'

describe Picky::Scheduler do

  context 'default params' do
    let(:scheduler) { described_class.new }

    context 'without forking' do
      before(:each) do
        scheduler.stub! :fork? => false
      end
      context 'non-stubbed forking' do
        it 'works correctly' do
          scheduler.schedule { sleep 0.01 }
          scheduler.schedule { sleep 0.01 }
          scheduler.schedule { sleep 0.01 }
          scheduler.schedule { sleep 0.01 }
        end
        it 'works correctly' do
          called = 0

          scheduler.schedule { called += 1 }
          scheduler.schedule { called += 1 }
          scheduler.schedule { called += 1 }
          scheduler.schedule { called += 1 }

          called.should == 4
        end
      end
    end

    describe 'fork?' do
      context 'OS can fork' do
        it 'returns false' do
          scheduler.fork?.should be_false
        end
      end
      context 'OS cannot fork' do
        before(:each) do
          Process.stub! :fork => nil
        end
        it 'returns false' do
          scheduler.fork?.should be_false
        end
      end
    end
  end
  context 'default params' do
    let(:scheduler) { described_class.new parallel: true }

    context 'stubbed forking' do
      it 'works correctly' do
        scheduler.scheduler.should_receive(:schedule).exactly(4).times.and_yield

        scheduler.schedule { sleep 0.01 }
        scheduler.schedule { sleep 0.01 }
        scheduler.schedule { sleep 0.01 }
        scheduler.schedule { sleep 0.01 }
      end
      it 'works correctly' do
        scheduler.scheduler.should_receive(:schedule).any_number_of_times.and_yield

        called = 0

        scheduler.schedule { called += 1 }
        scheduler.schedule { called += 1 }
        scheduler.schedule { called += 1 }
        scheduler.schedule { called += 1 }

        called.should == 4
      end
    end

    describe 'fork?' do
      context 'OS can fork' do
        it 'returns true' do
          scheduler.fork?.should == true
        end
      end
      # context 'OS cannot fork' do
      # before(:each) do
      # Process.send :undef, :fork
      # end
      # it 'returns false' do
      # scheduler.fork?.should == false
      # end
      # end
    end
  end

end
Something went wrong with that request. Please try again.