Permalink
Browse files

First integration tests

  • Loading branch information...
1 parent 0568fe5 commit 911c812fff116646bc91f107fac77d38bb0c2db4 @mloughran committed Aug 4, 2010
Showing with 79 additions and 1 deletion.
  1. +78 −0 spec/integration/juggler_spec.rb
  2. +1 −1 spec/unit/job_runner_spec.rb
@@ -0,0 +1,78 @@
+require File.expand_path('../../spec_helper', __FILE__)
+
+describe "Juggler" do
+ include EM::SpecHelper
+
+ before :each do
+ # Reset state
+ Juggler.instance_variable_set(:@connection, nil)
+ Juggler::Runner.instance_variable_set(:@runners, nil)
+
+ # Start clean beanstalk instance for each test
+ Juggler.server = "beanstalk://localhost:10001"
+ system "beanstalkd -p 10001 &"
+ sleep 0.1
+ end
+
+ after :each do
+ # TODO: Use pid
+ system "killall beanstalkd"
+ end
+
+ it "should successfully excecute one job" do
+ em(1) do
+ params_for_jobs_received = []
+ Juggler.juggle(:some_task, 1) { |params|
+ params_for_jobs_received << params
+ stub_deferrable(nil)
+ }
+ Juggler.throw(:some_task, {:some => "params"})
+
+ EM.add_timer(0.1) {
+ params_for_jobs_received.should == [{:some => "params"}]
+ done
+ }
+ end
+ end
+
+ it "should run correct number of jobs concurrently" do
+ em(1) do
+ params_for_jobs_received = []
+ Juggler.juggle(:some_task, 2) { |params|
+ params_for_jobs_received << params
+ stub_deferrable(nil, 0.2)
+ }
+
+ 10.times { |i| Juggler.throw(:some_task, i) }
+
+ EM.add_timer(0.3) {
+ # After 0.3 seconds, 2 jobs should have completed, and 2 more started
+ params_for_jobs_received.should == [0, 1, 2, 3]
+ done
+ }
+ end
+ end
+
+ it "should stop em after jobs completed when signaled to QUIT" do
+ job_finished = false
+ job_started = false
+ em(1) do
+ Juggler.juggle(:some_task, 1) { |params|
+ dd = EM::DefaultDeferrable.new
+ job_started = true
+ EM.add_timer(0.2) {
+ job_finished = true
+ dd.succeed
+ }
+ dd
+ }
+ Juggler.throw(:some_task, 'foo')
+ EM.add_timer(0.1) {
+ job_started.should == true
+ job_finished.should == false
+ Juggler::Runner.send(:stop_all_runners_with_grace)
+ }
+ end
+ job_finished.should == true
+ end
+end
@@ -16,7 +16,7 @@
job.should_receive(:delete).and_return(stub_deferrable(nil))
strategy = lambda {
- stub_deferrable(nil)
+ stub_deferrable(nil, 0.2)
}
jobrunner = Juggler::JobRunner.new(job, {}, strategy)

0 comments on commit 911c812

Please sign in to comment.