spec/lib/flapjack/coordinator_spec.rb in flapjack-0.6.43 vs spec/lib/flapjack/coordinator_spec.rb in flapjack-0.6.44

- old
+ new

@@ -1,54 +1,63 @@ require 'spec_helper' -require 'flapjack/coordinator' -describe Flapjack::Coordinator do +require 'flapjack/configuration' +require 'flapjack/executive' - let(:fiber) { mock('Fiber') } +require 'flapjack/gateways/web' +require 'flapjack/gateways/jabber' +require 'flapjack/gateways/email' - let(:config) { - {'redis' => {}, - 'executive' => {'enabled' => 'yes'}, - 'email_notifier' => {'enabled' => 'yes'}, - 'web' => {'enabled' => 'yes'} - } - } +require 'flapjack/coordinator' - let(:redis_config) { {} } +describe Flapjack::Coordinator do + let(:fiber) { mock(Fiber) } + let(:config) { mock(Flapjack::Configuration) } + # leaving actual testing of daemonisation to that class's tests it "daemonizes properly" do - fc = Flapjack::Coordinator.new(config, redis_config) + config.should_receive(:for_redis).and_return({}) + fc = Flapjack::Coordinator.new(config) + fc.should_receive(:daemonize) fc.should_not_receive(:build_pikelet) fc.start(:daemonize => true, :signals => false) end it "runs undaemonized" do EM.should_receive(:synchrony).and_yield + config.should_receive(:for_redis).and_return({}) + config.should_receive(:pikelets).and_return(Flapjack::Executive => {'enabled' => 'yes'}) + config.should_receive(:gateways).and_return({}) - fc = Flapjack::Coordinator.new(config, redis_config) - fc.should_receive(:build_pikelet).exactly(3).times + fc = Flapjack::Coordinator.new(config) + fc.should_receive(:build_pikelet) fc.start(:daemonize => false, :signals => false) end it "starts after daemonizing" do EM.should_receive(:synchrony).and_yield - fc = Flapjack::Coordinator.new(config, redis_config) - fc.should_receive(:build_pikelet).exactly(3).times + config.should_receive(:for_redis).and_return({}) + config.should_receive(:pikelets).and_return(Flapjack::Executive => {'enabled' => 'yes'}) + config.should_receive(:gateways).and_return({}) + + fc = Flapjack::Coordinator.new(config) + fc.should_receive(:build_pikelet) fc.after_daemonize end it "traps system signals and shuts down" do RbConfig::CONFIG.should_receive(:[]).with('host_os').and_return('darwin12.0.0') Kernel.should_receive(:trap).with('INT').and_yield Kernel.should_receive(:trap).with('TERM').and_yield Kernel.should_receive(:trap).with('QUIT').and_yield - fc = Flapjack::Coordinator.new(config, redis_config) + config.should_receive(:for_redis).and_return({}) + fc = Flapjack::Coordinator.new(config) fc.should_receive(:stop).exactly(3).times fc.send(:setup_signals) end @@ -57,11 +66,12 @@ Kernel.should_receive(:trap).with('INT').and_yield Kernel.should_receive(:trap).with('TERM').and_yield Kernel.should_not_receive(:trap).with('QUIT') - fc = Flapjack::Coordinator.new(config, redis_config) + config.should_receive(:for_redis).and_return({}) + fc = Flapjack::Coordinator.new(config) fc.should_receive(:stop).twice fc.send(:setup_signals) end @@ -76,19 +86,19 @@ exec.should_receive(:cleanup) email = mock('worker') email.should_receive(:is_a?).with(Flapjack::GenericPikelet).twice.and_return(false) email.should_receive(:shutdown) - Flapjack::Notification::Email.should_receive(:cleanup) + Flapjack::Gateways::Email.should_receive(:cleanup) backend = mock('backend') backend.should_receive(:size).twice.and_return(1, 0) web = mock('web') web.should_receive(:is_a?).with(Flapjack::GenericPikelet).twice.and_return(false) web.should_receive(:backend).twice.and_return(backend) web.should_receive(:stop!) - Flapjack::Web.should_receive(:cleanup) + Flapjack::Gateways::Web.should_receive(:cleanup) redis = mock('redis') redis.should_receive(:quit) Redis.should_receive(:new).and_return(redis) @@ -102,14 +112,16 @@ EM::Synchrony.should_receive(:sleep) EM.should_receive(:stop) pikelets = [{:fiber => fiber_exec, :instance => exec, :class => Flapjack::Executive}, - {:fiber => fiber_rsq, :instance => email, :class => Flapjack::Notification::Email}, - {:instance => web, :class => Flapjack::Web}] + {:fiber => fiber_rsq, :instance => email, :class => Flapjack::Gateways::Email}, + {:instance => web, :class => Flapjack::Gateways::Web}] - fc = Flapjack::Coordinator.new(config, redis_config) + config.should_receive(:for_redis).and_return({}) + + fc = Flapjack::Coordinator.new(config) fc.instance_variable_set('@redis_options', {}) fc.instance_variable_set('@pikelets', pikelets) fc.stop end @@ -121,37 +133,41 @@ exec.should_receive(:main) fiber.should_receive(:resume) Fiber.should_receive(:new).and_yield.and_return(fiber) - fc = Flapjack::Coordinator.new(config, redis_config) - fc.send(:build_pikelet, 'executive', {}) + config.should_receive(:for_redis).and_return({}) + + fc = Flapjack::Coordinator.new(config) + fc.send(:build_pikelet, Flapjack::Executive, {'enabled' => 'yes'}) pikelets = fc.instance_variable_get('@pikelets') pikelets.should_not be_nil pikelets.should be_an(Array) pikelets.should have(1).pikelet pikelets.first.should == {:fiber => fiber, :class => Flapjack::Executive, :instance => exec} end it "handles an exception raised by a fiber pikelet" do jabber = mock('jabber') jabber.should_receive(:bootstrap) - Flapjack::Jabber.should_receive(:new).and_return(jabber) + Flapjack::Gateways::Jabber.should_receive(:new).and_return(jabber) jabber.should_receive(:is_a?).with(Flapjack::GenericPikelet).and_return(true) jabber.should_receive(:main).and_raise(RuntimeError) fiber.should_receive(:resume) Fiber.should_receive(:new).and_yield.and_return(fiber) - fc = Flapjack::Coordinator.new(config, redis_config) + config.should_receive(:for_redis).and_return({}) + + fc = Flapjack::Coordinator.new(config) fc.should_receive(:stop) - fc.send(:build_pikelet, 'jabber_gateway', {}) + fc.send(:build_pikelet, Flapjack::Gateways::Jabber, {'enabled' => 'yes'}) pikelets = fc.instance_variable_get('@pikelets') pikelets.should_not be_nil pikelets.should be_an(Array) pikelets.should have(1).pikelet - pikelets.first.should == {:fiber => fiber, :class => Flapjack::Jabber, :instance => jabber} + pikelets.first.should == {:fiber => fiber, :class => Flapjack::Gateways::Jabber, :instance => jabber} end it "creates a resque worker pikelet" do redis = mock('redis') Flapjack::RedisPool.should_receive(:new).and_return(redis) @@ -162,37 +178,41 @@ worker.should_receive(:work) fiber.should_receive(:resume) Fiber.should_receive(:new).and_yield.and_return(fiber) - fc = Flapjack::Coordinator.new(config, redis_config) - fc.send(:build_pikelet, 'email_notifier', {}) + config.should_receive(:for_redis).and_return({}) + + fc = Flapjack::Coordinator.new(config) + fc.send(:build_pikelet, Flapjack::Gateways::Email, {'enabled' => 'yes'}) pikelets = fc.instance_variable_get('@pikelets') pikelets.should_not be_nil pikelets.should be_an(Array) pikelets.should have(1).pikelet - pikelets.first.should == {:fiber => fiber, :class => Flapjack::Notification::Email, + pikelets.first.should == {:fiber => fiber, :class => Flapjack::Gateways::Email, :instance => worker} end it "creates a thin server pikelet" do redis = mock('redis') server = mock('server') server.should_receive(:start) Thin::Server.should_receive(:new). - with(/^(?:\d{1,3}\.){3}\d{1,3}$/, an_instance_of(Fixnum), Flapjack::Web, :signals => false). + with(/^(?:\d{1,3}\.){3}\d{1,3}$/, an_instance_of(Fixnum), Flapjack::Gateways::Web, :signals => false). and_return(server) Flapjack::RedisPool.should_receive(:new) - fc = Flapjack::Coordinator.new(config, redis_config) - fc.send(:build_pikelet, 'web', {}) + config.should_receive(:for_redis).and_return({}) + + fc = Flapjack::Coordinator.new(config) + fc.send(:build_pikelet, Flapjack::Gateways::Web, {'enabled' => 'yes'}) pikelets = fc.instance_variable_get('@pikelets') pikelets.should_not be_nil pikelets.should be_an(Array) pikelets.should have(1).pikelet - pikelets.first.should == {:class => Flapjack::Web, :instance => server} + pikelets.first.should == {:class => Flapjack::Gateways::Web, :instance => server} end # NB: exceptions are handled directly by the Thin pikelets end \ No newline at end of file