Skip to content
This repository
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 490 lines (422 sloc) 12.916 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 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')

describe 'Navvy::Job' do
  describe '.keep?' do
    after(:each) do
      Navvy::Job.keep = false
      Navvy.configure do |config|
        config.keep_jobs = false
      end
    end

    describe 'when configured using Navvy::Job.keep=' do
      it 'should return false' do
        Navvy::Job.keep = false
        Navvy::Job.keep?.should == false
      end

      it 'should return true' do
        Navvy::Job.keep = true
        Navvy::Job.keep?.should == true
      end
    end

    describe 'when configured with Navvy.configure' do
      it 'should return false' do
        Navvy.configure do |config|
          config.keep_jobs = false
        end
        Navvy::Job.keep?.should == false
      end

      it 'should return true' do
        Navvy.configure do |config|
          config.keep_jobs = true
        end
        Navvy::Job.keep?.should == true
      end
    end
  end

  describe '.enqueue' do
    before(:each) do
      delete_all_jobs
    end

    it 'should enqueue a job' do
      Navvy::Job.enqueue(Cow, :speak)
      job_count.should == 1
    end

    it 'should set the object and the method_name' do
      Navvy::Job.enqueue(Cow, :speak)
      job = first_job
      job.object.should == 'Cow'
      job.method_name.to_s.should == 'speak'
    end

    it 'should turn the method_name into a symbol' do
      Navvy::Job.enqueue(Cow, 'speak')
      job = first_job
      job.method_name.to_s.should == 'speak'
    end

    it 'should set the arguments' do
      Navvy::Job.enqueue(Cow, :speak, true, false)
      job = first_job
      job.args.should == [true, false]
    end

    it 'should set the created_at date' do
      Navvy::Job.enqueue(Cow, :speak, true, false)
      job = first_job
      job.created_at.should be_instance_of Time
      job.created_at.should <= Time.now
      job.created_at.should > Time.now - 10
    end

    it 'should set the run_at date' do
      Navvy::Job.enqueue(Cow, :speak, true, false)
      job = first_job
      job.run_at.should be_instance_of Time
      job.run_at.should <= Time.now
      job.created_at.should > Time.now - 10
    end

    it 'should return the enqueued job' do
      Navvy::Job.enqueue(Cow, :speak, true, false).
        should be_instance_of Navvy::Job
    end
  end

  describe '.next' do
    before(:each) do
      delete_all_jobs
      Navvy::Job.create(
        :object => 'Cow',
        :method_name => :last,
        :created_at => Time.now + (60 * 60),
        :run_at => Time.now
      )
      Navvy::Job.create(
        :object => 'Cow',
        :method_name => :break,
        :completed_at => Time.now,
        :run_at => Time.now
      )
      Navvy::Job.create(
        :object => 'Cow',
        :method_name => :break,
        :failed_at => Time.now,
        :run_at => Time.now
      )
      Navvy::Job.create(
        :object => 'Cow',
        :method_name => :tomorrow,
        :run_at => Time.now + (60 * 60)
      )
      120.times do
        Navvy::Job.enqueue(Cow, :speak)
      end
    end

    it 'should find the next 10 available jobs' do
      jobs = Navvy::Job.next
      jobs.count.should == 100
      jobs.each do |job|
        job.should be_instance_of Navvy::Job
        job.method_name.to_s.should == 'speak'
      end
    end

    it 'should find the next 2 available jobs' do
      Navvy::Job.next(2).count.should == 2
    end

    it 'should find the next 4 available jobs' do
      Navvy::Job.limit = 4
      Navvy::Job.next.count.should == 4
    end
  end

  describe '.cleanup' do
    before(:each) do
      delete_all_jobs
      Navvy::Job.create(
        :object => 'Cow',
        :method_name => :speak,
        :completed_at => Time.now - (2 * 60 * 60)
      )
      Navvy::Job.create(
        :object => 'Cow',
        :method_name => :speak,
        :completed_at => Time.now
      )
      Navvy::Job.create(
        :object => 'Cow',
        :method_name => :speak
      )
    end

    it 'should delete all complete jobs when "keep" is false' do
      Navvy::Job.cleanup
      job_count.should == 1
    end

    it 'should not delete any complete jobs when "keep" is true' do
      Navvy::Job.keep = true
      Navvy::Job.cleanup
      job_count.should == 3
    end

    it 'should delete all complete jobs where "keep" has passed' do
      Navvy::Job.keep = (60 * 60)
      Navvy::Job.cleanup
      job_count.should == 2
    end
  end

  describe '.delete_all' do
    it 'should delete all jobs' do
      3.times do; Navvy::Job.create; end
      Navvy::Job.delete_all
      job_count.should == 0
    end
  end

  describe '#run' do
    it 'should pass the arguments' do
      delete_all_jobs
      job = Navvy::Job.enqueue(Cow, :name, 'Betsy')
      Cow.should_receive(:name).with('Betsy')
      job.run
    end

    describe 'when everything goes well' do
      before(:each) do
        delete_all_jobs
        Navvy::Job.enqueue(Cow, :speak)
        Navvy::Job.keep = false
      end

      it 'should run the job and delete it' do
        jobs = Navvy::Job.next
        jobs.first.run.should == 'moo'
        job_count.should == 0
      end

      describe 'when Navvy::Job.keep is set' do
        it 'should mark the job as complete when keep is true' do
          Navvy::Job.keep = true
          jobs = Navvy::Job.next
          jobs.first.run
          job_count.should == 1
          jobs.first.started_at.should be_instance_of Time
          jobs.first.completed_at.should be_instance_of Time
        end

        it 'should mark the job as complete when keep has not passed yer' do
          Navvy::Job.keep = (60 * 60)
          jobs = Navvy::Job.next
          jobs.first.run
          job_count.should == 1
          jobs.first.started_at.should be_instance_of Time
          jobs.first.completed_at.should be_instance_of Time
        end

        it 'should delete the job when the "keep" flag has passed' do
          Navvy::Job.keep = -(60 * 60)
          jobs = Navvy::Job.next
          jobs.first.run
          job_count.should == 0
        end
      end
    end

    describe 'when a job fails' do
      before(:each) do
        delete_all_jobs
        Navvy::Job.enqueue(Cow, :broken)
      end

      it 'should store the exception and current time' do
        jobs = Navvy::Job.next
        jobs.first.run
        jobs.first.exception.should == 'this method is broken'
        jobs.first.started_at.should be_instance_of Time
        jobs.first.failed_at.should be_instance_of Time
      end
    end
  end

  describe '#started' do
    before(:each) do
      delete_all_jobs
      Navvy::Job.enqueue(Cow, :speak)
    end

    it 'should update the jobs started_at date' do
      jobs = Navvy::Job.next
      jobs.first.started
      jobs.first.started_at.should_not be_nil
    end
  end

  describe '#completed' do
    before(:each) do
      delete_all_jobs
      Navvy::Job.enqueue(Cow, :speak)
    end

    it 'should update the jobs completed_at date' do
      jobs = Navvy::Job.next
      jobs.first.completed
      jobs.first.completed_at.should_not be_nil
    end

    it 'should set the return if provided' do
      jobs = Navvy::Job.next
      jobs.first.completed('woo!')
      jobs.first.return.should == 'woo!'
    end
  end

  describe '#failed' do
    before(:each) do
      delete_all_jobs
      Navvy::Job.enqueue(Cow, :speak)
    end

    it 'should update the jobs failed_at date' do
      jobs = Navvy::Job.next
      jobs.first.failed
      jobs.first.failed_at.should_not be_nil
    end

    it 'should set the exception message if provided' do
      jobs = Navvy::Job.next
      jobs.first.failed('broken')
      jobs.first.exception.should == 'broken'
    end

    it 'should retry' do
      jobs = Navvy::Job.next
      jobs.first.should_receive(:retry)
      jobs.first.failed('broken')
    end

    it 'should not retry when the job has failed 25 times already' do
      jobs = Navvy::Job.next
      jobs.first.stub!(:times_failed).and_return 25
      jobs.first.should_not_receive(:retry)
      jobs.first.failed('broken')
    end

    it 'should not retry when the job has failed 10 times' do
      Navvy::Job.max_attempts = 10
      jobs = Navvy::Job.next
      jobs.first.stub!(:times_failed).and_return 10
      jobs.first.should_not_receive(:retry)
      jobs.first.failed('broken')
    end
  end

  describe '#retry' do
    before(:each) do
      delete_all_jobs
    end

    it 'should enqueue a child for the failed job' do
      failed_job = Navvy::Job.enqueue(Cow, :speak, true, false)
      job = failed_job.retry
      job.object.should == 'Cow'
      job.method_name.to_s.should == 'speak'
      job.args.should == [true, false]
      job.parent_id.should == failed_job.id
    end

    it 'should handle hashes correctly' do
      failed_job = Navvy::Job.enqueue(Cow, :speak, 'name' => 'Betsy')
      job = failed_job.retry
      job.args.should == [{'name' => 'Betsy'}]
      job.parent_id.should == failed_job.id
    end

    it 'should set the priority' do
      failed_job = Navvy::Job.enqueue(
        Cow,
        :speak,
        'name' => 'Betsy',
        :job_options => {
          :priority => 2
        }
      )
      job = failed_job.retry
      job.priority.should == 2
    end

    it 'should set the run_at date to about 16 seconds from now' do
      failed_job = Navvy::Job.enqueue(Cow, :speak, 'name' => 'Betsy')
      failed_job.stub!(:times_failed).and_return 2
      now = Time.now
      job = failed_job.retry
      job.run_at.to_i.should == (now + 16).to_i
    end

    it 'should set the run_at date to about 256 seconds from now' do
      failed_job = Navvy::Job.enqueue(Cow, :speak, 'name' => 'Betsy')
      failed_job.stub!(:times_failed).and_return 4
      now = Time.now
      job = failed_job.retry
      job.run_at.to_i.should == (now + 256).to_i
    end

    it 'should set the run_at date to about 4096 seconds from now' do
      failed_job = Navvy::Job.enqueue(Cow, :speak, 'name' => 'Betsy')
      failed_job.stub!(:times_failed).and_return 8
      now = Time.now
      job = failed_job.retry
      job.run_at.to_i.should == (now + 4096).to_i
    end

    it 'should set the parent_id to the master job id' do
      failed_job = Navvy::Job.enqueue(Cow, :speak, 'name' => 'Betsy')
      failed_child = failed_job.retry
      failed_child.retry.parent_id.should == failed_job.id
    end
  end

  describe '#times_failed' do
    before(:each) do
      delete_all_jobs
      @failed_job = Navvy::Job.create(
        :failed_at => Time.now
      )
    end

    it 'should return 1' do
      @failed_job.times_failed.should == 1
    end

    it 'should return 3 when having 2 failed children' do
      2.times do
        Navvy::Job.create(
          :failed_at => Time.now,
          :parent_id => @failed_job.id
        )
      end

      @failed_job.times_failed.should == 3
    end

    it 'should return 2 when having 1 failed and one pending child' do
      Navvy::Job.create(
        :failed_at => Time.now,
        :parent_id => @failed_job.id
      )

      Navvy::Job.create(
        :parent_id => @failed_job.id
      )

      Navvy::Job.create(
        :parent_id => @failed_job.id
      )

      @failed_job.times_failed.should == 2
    end

    it 'should return 2 when having failed and having a failed parent' do
      failed_child = Navvy::Job.create(
        :failed_at => Time.now,
        :parent_id => @failed_job.id
      )
      failed_child.times_failed.should == 2
    end
  end

  describe '#ran?' do
    it 'should return false when failed_at? and completed_at? are false' do
      job = Navvy::Job.create
      job.ran?.should be_false
    end

    it 'should return true when failed_at? or completed_at? is true' do
      [
        Navvy::Job.create(:failed_at => Time.now),
        Navvy::Job.create(:completed_at => Time.now)
      ].each do |job|
        job.ran?.should be_true
      end
    end
  end

  describe '#duration' do
    it 'should return a duration if started_at and completed_at are set' do
      job = Navvy::Job.create(
        :started_at => Time.now - 2,
        :completed_at => Time.now
      )

      job.duration.should >= 2
    end

    it 'should return a duration if started_at and failed_at are set' do
      job = Navvy::Job.create(
        :started_at => Time.now - 3,
        :failed_at => Time.now
      )

      job.duration.should >= 3
    end

    it 'should return 0 if only started_at is set' do
      job = Navvy::Job.create(
        :started_at => Time.now - 4
      )

      job.duration.should == 0
    end
  end

  describe '#args' do
    it 'should return an array of arguments' do
      job = Navvy::Job.enqueue(Cow, :speak, true, false)
      job.args.should be_instance_of Array
      job.args.count.should == 2
    end
  end
end
Something went wrong with that request. Please try again.