test_helper_test.rb 27.9 KB
Newer Older
1
# frozen_string_literal: true
2

3 4 5 6 7 8 9
require "helper"
require "active_support/core_ext/time"
require "active_support/core_ext/date"
require "jobs/hello_job"
require "jobs/logging_job"
require "jobs/nested_job"
require "jobs/rescue_job"
10
require "jobs/inherited_job"
11
require "models/person"
A
Abdelkader Boudih 已提交
12 13 14 15 16

class EnqueuedJobsTest < ActiveJob::TestCase
  def test_assert_enqueued_jobs
    assert_nothing_raised do
      assert_enqueued_jobs 1 do
17
        HelloJob.perform_later("david")
A
Abdelkader Boudih 已提交
18 19 20 21 22 23 24
      end
    end
  end

  def test_repeated_enqueued_jobs_calls
    assert_nothing_raised do
      assert_enqueued_jobs 1 do
25
        HelloJob.perform_later("abdelkader")
A
Abdelkader Boudih 已提交
26 27 28 29 30
      end
    end

    assert_nothing_raised do
      assert_enqueued_jobs 2 do
31 32
        HelloJob.perform_later("sean")
        HelloJob.perform_later("yves")
A
Abdelkader Boudih 已提交
33 34 35 36
      end
    end
  end

37
  def test_assert_enqueued_jobs_message
38
    HelloJob.perform_later("sean")
39 40
    e = assert_raises Minitest::Assertion do
      assert_enqueued_jobs 2 do
41
        HelloJob.perform_later("sean")
42 43 44 45 46 47
      end
    end
    assert_match "Expected: 2", e.message
    assert_match "Actual: 1", e.message
  end

A
Abdelkader Boudih 已提交
48 49
  def test_assert_enqueued_jobs_with_no_block
    assert_nothing_raised do
50
      HelloJob.perform_later("rafael")
A
Abdelkader Boudih 已提交
51 52 53 54
      assert_enqueued_jobs 1
    end

    assert_nothing_raised do
55 56
      HelloJob.perform_later("aaron")
      HelloJob.perform_later("matthew")
A
Abdelkader Boudih 已提交
57 58 59 60
      assert_enqueued_jobs 3
    end
  end

61 62 63 64 65 66
  def test_assert_no_enqueued_jobs_with_no_block
    assert_nothing_raised do
      assert_no_enqueued_jobs
    end
  end

A
Abdelkader Boudih 已提交
67 68 69
  def test_assert_no_enqueued_jobs
    assert_nothing_raised do
      assert_no_enqueued_jobs do
70
        HelloJob.perform_now
A
Abdelkader Boudih 已提交
71 72 73 74 75 76 77
      end
    end
  end

  def test_assert_enqueued_jobs_too_few_sent
    error = assert_raise ActiveSupport::TestCase::Assertion do
      assert_enqueued_jobs 2 do
78
        HelloJob.perform_later("xavier")
A
Abdelkader Boudih 已提交
79 80 81 82 83 84 85 86 87
      end
    end

    assert_match(/2 .* but 1/, error.message)
  end

  def test_assert_enqueued_jobs_too_many_sent
    error = assert_raise ActiveSupport::TestCase::Assertion do
      assert_enqueued_jobs 1 do
88 89
        HelloJob.perform_later("cristian")
        HelloJob.perform_later("guillermo")
A
Abdelkader Boudih 已提交
90 91 92 93 94
      end
    end

    assert_match(/1 .* but 2/, error.message)
  end
95

A
Abdelkader Boudih 已提交
96 97 98
  def test_assert_no_enqueued_jobs_failure
    error = assert_raise ActiveSupport::TestCase::Assertion do
      assert_no_enqueued_jobs do
99
        HelloJob.perform_later("jeremy")
A
Abdelkader Boudih 已提交
100 101 102 103 104 105
      end
    end

    assert_match(/0 .* but 1/, error.message)
  end

106 107 108
  def test_assert_enqueued_jobs_with_only_option
    assert_nothing_raised do
      assert_enqueued_jobs 1, only: HelloJob do
109
        HelloJob.perform_later("jeremy")
110
        LoggingJob.perform_later
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
        LoggingJob.perform_later
      end
    end
  end

  def test_assert_enqueued_jobs_with_except_option
    assert_nothing_raised do
      assert_enqueued_jobs 1, except: LoggingJob do
        HelloJob.perform_later("jeremy")
        LoggingJob.perform_later
        LoggingJob.perform_later
      end
    end
  end

  def test_assert_enqueued_jobs_with_only_and_except_option
127
    error = assert_raise ArgumentError do
128 129 130 131
      assert_enqueued_jobs 1, only: HelloJob, except: HelloJob do
        HelloJob.perform_later("jeremy")
        LoggingJob.perform_later
        LoggingJob.perform_later
132 133
      end
    end
134 135

    assert_match(/`:only` and `:except`/, error.message)
136 137
  end

138 139 140 141 142 143 144 145 146 147
  def test_assert_enqueued_jobs_with_only_and_queue_option
    assert_nothing_raised do
      assert_enqueued_jobs 1, only: HelloJob, queue: :some_queue do
        HelloJob.set(queue: :some_queue).perform_later
        HelloJob.set(queue: :other_queue).perform_later
        LoggingJob.perform_later
      end
    end
  end

148 149 150 151 152 153 154 155 156 157 158
  def test_assert_enqueued_jobs_with_except_and_queue_option
    assert_nothing_raised do
      assert_enqueued_jobs 1, except: LoggingJob, queue: :some_queue do
        HelloJob.set(queue: :some_queue).perform_later
        HelloJob.set(queue: :other_queue).perform_later
        LoggingJob.perform_later
      end
    end
  end

  def test_assert_enqueued_jobs_with_only_and_except_and_queue_option
159
    error = assert_raise ArgumentError do
160 161 162 163 164 165 166 167 168 169
      assert_enqueued_jobs 1, only: HelloJob, except: HelloJob, queue: :some_queue do
        HelloJob.set(queue: :some_queue).perform_later
        HelloJob.set(queue: :other_queue).perform_later
        LoggingJob.perform_later
      end
    end

    assert_match(/`:only` and `:except`/, error.message)
  end

170 171 172 173 174 175 176 177 178 179 180
  def test_assert_enqueued_jobs_with_queue_option
    assert_nothing_raised do
      assert_enqueued_jobs 2, queue: :default do
        HelloJob.perform_later
        LoggingJob.perform_later
        HelloJob.set(queue: :other_queue).perform_later
        LoggingJob.set(queue: :other_queue).perform_later
      end
    end
  end

181 182 183 184 185 186 187 188 189 190
  def test_assert_enqueued_jobs_with_only_option_and_none_sent
    error = assert_raise ActiveSupport::TestCase::Assertion do
      assert_enqueued_jobs 1, only: HelloJob do
        LoggingJob.perform_later
      end
    end

    assert_match(/1 .* but 0/, error.message)
  end

191 192 193 194 195 196 197 198 199 200 201
  def test_assert_enqueued_jobs_with_except_option_and_none_sent
    error = assert_raise ActiveSupport::TestCase::Assertion do
      assert_enqueued_jobs 1, except: LoggingJob do
        LoggingJob.perform_later
      end
    end

    assert_match(/1 .* but 0/, error.message)
  end

  def test_assert_enqueued_jobs_with_only_and_except_option_and_none_sent
202
    error = assert_raise ArgumentError do
203 204 205 206 207 208 209 210
      assert_enqueued_jobs 1, only: HelloJob, except: HelloJob do
        LoggingJob.perform_later
      end
    end

    assert_match(/`:only` and `:except`/, error.message)
  end

211 212 213
  def test_assert_enqueued_jobs_with_only_option_and_too_few_sent
    error = assert_raise ActiveSupport::TestCase::Assertion do
      assert_enqueued_jobs 5, only: HelloJob do
214
        HelloJob.perform_later("jeremy")
215 216 217 218 219 220 221
        4.times { LoggingJob.perform_later }
      end
    end

    assert_match(/5 .* but 1/, error.message)
  end

222 223 224 225 226 227 228 229 230 231 232 233
  def test_assert_enqueued_jobs_with_except_option_and_too_few_sent
    error = assert_raise ActiveSupport::TestCase::Assertion do
      assert_enqueued_jobs 5, except: LoggingJob do
        HelloJob.perform_later("jeremy")
        4.times { LoggingJob.perform_later }
      end
    end

    assert_match(/5 .* but 1/, error.message)
  end

  def test_assert_enqueued_jobs_with_only_and_except_option_and_too_few_sent
234
    error = assert_raise ArgumentError do
235 236 237 238 239 240 241 242 243
      assert_enqueued_jobs 5, only: HelloJob, except: HelloJob do
        HelloJob.perform_later("jeremy")
        4.times { LoggingJob.perform_later }
      end
    end

    assert_match(/`:only` and `:except`/, error.message)
  end

244 245 246
  def test_assert_enqueued_jobs_with_only_option_and_too_many_sent
    error = assert_raise ActiveSupport::TestCase::Assertion do
      assert_enqueued_jobs 1, only: HelloJob do
247
        2.times { HelloJob.perform_later("jeremy") }
248 249 250 251 252 253
      end
    end

    assert_match(/1 .* but 2/, error.message)
  end

254 255 256 257 258 259 260 261 262 263 264
  def test_assert_enqueued_jobs_with_except_option_and_too_many_sent
    error = assert_raise ActiveSupport::TestCase::Assertion do
      assert_enqueued_jobs 1, except: LoggingJob do
        2.times { HelloJob.perform_later("jeremy") }
      end
    end

    assert_match(/1 .* but 2/, error.message)
  end

  def test_assert_enqueued_jobs_with_only_and_except_option_and_too_many_sent
265
    error = assert_raise ArgumentError do
266 267 268 269 270 271 272 273
      assert_enqueued_jobs 1, only: HelloJob, except: HelloJob do
        2.times { HelloJob.perform_later("jeremy") }
      end
    end

    assert_match(/`:only` and `:except`/, error.message)
  end

274 275 276
  def test_assert_enqueued_jobs_with_only_option_as_array
    assert_nothing_raised do
      assert_enqueued_jobs 2, only: [HelloJob, LoggingJob] do
277 278 279
        HelloJob.perform_later("jeremy")
        LoggingJob.perform_later("stewie")
        RescueJob.perform_later("david")
280 281 282 283
      end
    end
  end

284 285 286 287 288 289 290 291 292 293 294
  def test_assert_enqueued_jobs_with_except_option_as_array
    assert_nothing_raised do
      assert_enqueued_jobs 1, except: [HelloJob, LoggingJob] do
        HelloJob.perform_later("jeremy")
        LoggingJob.perform_later("stewie")
        RescueJob.perform_later("david")
      end
    end
  end

  def test_assert_enqueued_jobs_with_only_and_except_option_as_array
295
    error = assert_raise ArgumentError do
296 297 298 299 300 301 302 303 304 305
      assert_enqueued_jobs 2, only: [HelloJob, LoggingJob], except: [HelloJob, LoggingJob] do
        HelloJob.perform_later("jeremy")
        LoggingJob.perform_later("stewie")
        RescueJob.perform_later("david")
      end
    end

    assert_match(/`:only` and `:except`/, error.message)
  end

306 307 308 309 310 311 312 313
  def test_assert_no_enqueued_jobs_with_only_option
    assert_nothing_raised do
      assert_no_enqueued_jobs only: HelloJob do
        LoggingJob.perform_later
      end
    end
  end

314 315 316 317 318 319 320 321 322
  def test_assert_no_enqueued_jobs_with_except_option
    assert_nothing_raised do
      assert_no_enqueued_jobs except: LoggingJob do
        LoggingJob.perform_later
      end
    end
  end

  def test_assert_no_enqueued_jobs_with_only_and_except_option
323
    error = assert_raise ArgumentError do
324 325 326 327 328 329 330 331
      assert_no_enqueued_jobs only: HelloJob, except: HelloJob do
        LoggingJob.perform_later
      end
    end

    assert_match(/`:only` and `:except`/, error.message)
  end

332 333 334
  def test_assert_no_enqueued_jobs_with_only_option_failure
    error = assert_raise ActiveSupport::TestCase::Assertion do
      assert_no_enqueued_jobs only: HelloJob do
335
        HelloJob.perform_later("jeremy")
336 337 338 339 340 341 342
        LoggingJob.perform_later
      end
    end

    assert_match(/0 .* but 1/, error.message)
  end

343 344 345 346 347 348 349 350 351 352 353 354
  def test_assert_no_enqueued_jobs_with_except_option_failure
    error = assert_raise ActiveSupport::TestCase::Assertion do
      assert_no_enqueued_jobs except: LoggingJob do
        HelloJob.perform_later("jeremy")
        LoggingJob.perform_later
      end
    end

    assert_match(/0 .* but 1/, error.message)
  end

  def test_assert_no_enqueued_jobs_with_only_and_except_option_failure
355
    error = assert_raise ArgumentError do
356 357 358 359 360 361 362 363 364
      assert_no_enqueued_jobs only: HelloJob, except: HelloJob do
        HelloJob.perform_later("jeremy")
        LoggingJob.perform_later
      end
    end

    assert_match(/`:only` and `:except`/, error.message)
  end

365 366 367 368 369 370 371 372
  def test_assert_no_enqueued_jobs_with_only_option_as_array
    assert_nothing_raised do
      assert_no_enqueued_jobs only: [HelloJob, RescueJob] do
        LoggingJob.perform_later
      end
    end
  end

373 374 375 376 377 378 379 380 381 382
  def test_assert_no_enqueued_jobs_with_except_option_as_array
    assert_nothing_raised do
      assert_no_enqueued_jobs except: [HelloJob, RescueJob] do
        HelloJob.perform_later
        RescueJob.perform_later
      end
    end
  end

  def test_assert_no_enqueued_jobs_with_only_and_except_option_as_array
383
    error = assert_raise ArgumentError do
384 385 386 387 388 389 390 391
      assert_no_enqueued_jobs only: [HelloJob, RescueJob], except: [HelloJob, RescueJob] do
        LoggingJob.perform_later
      end
    end

    assert_match(/`:only` and `:except`/, error.message)
  end

392
  def test_assert_enqueued_with
393
    assert_enqueued_with(job: LoggingJob, queue: "default") do
394
      LoggingJob.set(wait_until: Date.tomorrow.noon).perform_later
A
Abdelkader Boudih 已提交
395 396 397
    end
  end

398 399 400 401 402 403
  def test_assert_enqueued_with_with_no_block
    LoggingJob.set(wait_until: Date.tomorrow.noon).perform_later
    assert_enqueued_with(job: LoggingJob, queue: "default")
  end

  def test_assert_enqueued_with_returns
404 405 406 407 408 409
    job = assert_enqueued_with(job: LoggingJob) do
      LoggingJob.set(wait_until: 5.minutes.from_now).perform_later(1, 2, 3)
    end

    assert_instance_of LoggingJob, job
    assert_in_delta 5.minutes.from_now, job.scheduled_at, 1
410
    assert_equal "default", job.queue_name
411 412 413
    assert_equal [1, 2, 3], job.arguments
  end

414 415 416 417 418 419 420 421 422 423 424
  def test_assert_enqueued_with_with_no_block_returns
    LoggingJob.set(wait_until: 5.minutes.from_now).perform_later(1, 2, 3)
    job = assert_enqueued_with(job: LoggingJob)

    assert_instance_of LoggingJob, job
    assert_in_delta 5.minutes.from_now, job.scheduled_at, 1
    assert_equal "default", job.queue_name
    assert_equal [1, 2, 3], job.arguments
  end

  def test_assert_enqueued_with_failure
A
Abdelkader Boudih 已提交
425
    assert_raise ActiveSupport::TestCase::Assertion do
426
      assert_enqueued_with(job: LoggingJob, queue: "default") do
C
Cristian Bica 已提交
427
        NestedJob.perform_later
A
Abdelkader Boudih 已提交
428 429 430
      end
    end

431 432 433 434 435
    assert_raise ActiveSupport::TestCase::Assertion do
      LoggingJob.perform_later
      assert_enqueued_with(job: LoggingJob) {}
    end

436
    error = assert_raise ActiveSupport::TestCase::Assertion do
437
      assert_enqueued_with(job: NestedJob, queue: "low") do
C
Cristian Bica 已提交
438
        NestedJob.perform_later
A
Abdelkader Boudih 已提交
439 440
      end
    end
441

442
    assert_equal 'No enqueued job found with {:job=>NestedJob, :queue=>"low"}', error.message
A
Abdelkader Boudih 已提交
443 444
  end

445 446 447 448 449 450 451 452 453 454 455 456 457 458 459
  def test_assert_enqueued_with_with_no_block_failure
    assert_raise ActiveSupport::TestCase::Assertion do
      NestedJob.perform_later
      assert_enqueued_with(job: LoggingJob, queue: "default")
    end

    error = assert_raise ActiveSupport::TestCase::Assertion do
      NestedJob.perform_later
      assert_enqueued_with(job: NestedJob, queue: "low")
    end

    assert_equal 'No enqueued job found with {:job=>NestedJob, :queue=>"low"}', error.message
  end

  def test_assert_enqueued_with_args
A
Abdelkader Boudih 已提交
460
    assert_raise ArgumentError do
A
Abdelkader Boudih 已提交
461
      assert_enqueued_with(class: LoggingJob) do
C
Cristian Bica 已提交
462
        NestedJob.set(wait_until: Date.tomorrow.noon).perform_later
A
Abdelkader Boudih 已提交
463 464 465
      end
    end
  end
466

467 468 469 470
  def test_assert_enqueued_with_with_no_block_args
    assert_raise ArgumentError do
      NestedJob.set(wait_until: Date.tomorrow.noon).perform_later
      assert_enqueued_with(class: LoggingJob)
471 472 473
    end
  end

474 475 476 477 478 479
  def test_assert_enqueued_with_with_no_block_with_at_option
    HelloJob.set(wait_until: Date.tomorrow.noon).perform_later
    assert_enqueued_with(job: HelloJob, at: Date.tomorrow.noon)
  end

  def test_assert_enqueued_with_with_global_id_args
480 481 482 483 484 485
    ricardo = Person.new(9)
    assert_enqueued_with(job: HelloJob, args: [ricardo]) do
      HelloJob.perform_later(ricardo)
    end
  end

486 487 488 489 490 491 492
  def test_assert_enqueued_with_with_no_block_with_global_id_args
    ricardo = Person.new(9)
    HelloJob.perform_later(ricardo)
    assert_enqueued_with(job: HelloJob, args: [ricardo])
  end

  def test_assert_enqueued_with_failure_with_global_id_args
493 494 495 496 497 498 499 500 501 502
    ricardo = Person.new(9)
    wilma = Person.new(11)
    error = assert_raise ActiveSupport::TestCase::Assertion do
      assert_enqueued_with(job: HelloJob, args: [wilma]) do
        HelloJob.perform_later(ricardo)
      end
    end

    assert_equal "No enqueued job found with {:job=>HelloJob, :args=>[#{wilma.inspect}]}", error.message
  end
503

504 505 506 507 508 509 510 511 512 513 514
  def test_assert_enqueued_with_with_failure_with_no_block_with_global_id_args
    ricardo = Person.new(9)
    wilma = Person.new(11)
    error = assert_raise ActiveSupport::TestCase::Assertion do
      HelloJob.perform_later(ricardo)
      assert_enqueued_with(job: HelloJob, args: [wilma])
    end

    assert_equal "No enqueued job found with {:job=>HelloJob, :args=>[#{wilma.inspect}]}", error.message
  end

515 516 517 518 519 520
  def test_assert_enqueued_job_does_not_change_jobs_count
    HelloJob.perform_later
    assert_enqueued_with(job: HelloJob) do
      HelloJob.perform_later
    end

521
    assert_equal 2, queue_adapter.enqueued_jobs.count
522
  end
523 524 525 526 527 528 529 530

  def test_assert_enqueued_with_with_no_block_does_not_change_jobs_count
    HelloJob.perform_later
    HelloJob.perform_later
    assert_enqueued_with(job: HelloJob)

    assert_equal 2, queue_adapter.enqueued_jobs.count
  end
A
Abdelkader Boudih 已提交
531 532 533
end

class PerformedJobsTest < ActiveJob::TestCase
534
  def test_performed_enqueue_jobs_with_only_option_doesnt_leak_outside_the_block
535
    assert_nil queue_adapter.filter
536 537 538
    perform_enqueued_jobs only: HelloJob do
      assert_equal HelloJob, queue_adapter.filter
    end
539
    assert_nil queue_adapter.filter
540 541
  end

542 543 544 545 546 547 548 549
  def test_performed_enqueue_jobs_with_except_option_doesnt_leak_outside_the_block
    assert_nil queue_adapter.reject
    perform_enqueued_jobs except: HelloJob do
      assert_equal HelloJob, queue_adapter.reject
    end
    assert_nil queue_adapter.reject
  end

A
Abdelkader Boudih 已提交
550 551 552
  def test_assert_performed_jobs
    assert_nothing_raised do
      assert_performed_jobs 1 do
553
        HelloJob.perform_later("david")
A
Abdelkader Boudih 已提交
554 555 556 557 558 559 560
      end
    end
  end

  def test_repeated_performed_jobs_calls
    assert_nothing_raised do
      assert_performed_jobs 1 do
561
        HelloJob.perform_later("abdelkader")
A
Abdelkader Boudih 已提交
562 563 564 565 566
      end
    end

    assert_nothing_raised do
      assert_performed_jobs 2 do
567 568
        HelloJob.perform_later("sean")
        HelloJob.perform_later("yves")
A
Abdelkader Boudih 已提交
569 570 571 572
      end
    end
  end

573
  def test_assert_performed_jobs_message
574
    HelloJob.perform_later("sean")
575 576
    e = assert_raises Minitest::Assertion do
      assert_performed_jobs 2 do
577
        HelloJob.perform_later("sean")
578 579 580 581 582 583
      end
    end
    assert_match "Expected: 2", e.message
    assert_match "Actual: 1", e.message
  end

A
Abdelkader Boudih 已提交
584 585
  def test_assert_performed_jobs_with_no_block
    assert_nothing_raised do
586
      perform_enqueued_jobs do
587
        HelloJob.perform_later("rafael")
588
      end
A
Abdelkader Boudih 已提交
589 590 591 592
      assert_performed_jobs 1
    end

    assert_nothing_raised do
593
      perform_enqueued_jobs do
594 595
        HelloJob.perform_later("aaron")
        HelloJob.perform_later("matthew")
596 597 598 599 600 601 602 603
        assert_performed_jobs 3
      end
    end
  end

  def test_assert_no_performed_jobs_with_no_block
    assert_nothing_raised do
      assert_no_performed_jobs
A
Abdelkader Boudih 已提交
604 605 606 607 608 609
    end
  end

  def test_assert_no_performed_jobs
    assert_nothing_raised do
      assert_no_performed_jobs do
610
        # empty block won't perform jobs
A
Abdelkader Boudih 已提交
611 612 613 614 615 616 617
      end
    end
  end

  def test_assert_performed_jobs_too_few_sent
    error = assert_raise ActiveSupport::TestCase::Assertion do
      assert_performed_jobs 2 do
618
        HelloJob.perform_later("xavier")
A
Abdelkader Boudih 已提交
619 620 621 622 623 624 625 626 627
      end
    end

    assert_match(/2 .* but 1/, error.message)
  end

  def test_assert_performed_jobs_too_many_sent
    error = assert_raise ActiveSupport::TestCase::Assertion do
      assert_performed_jobs 1 do
628 629
        HelloJob.perform_later("cristian")
        HelloJob.perform_later("guillermo")
A
Abdelkader Boudih 已提交
630 631 632 633 634
      end
    end

    assert_match(/1 .* but 2/, error.message)
  end
635

A
Abdelkader Boudih 已提交
636 637 638
  def test_assert_no_performed_jobs_failure
    error = assert_raise ActiveSupport::TestCase::Assertion do
      assert_no_performed_jobs do
639
        HelloJob.perform_later("jeremy")
A
Abdelkader Boudih 已提交
640 641 642 643 644 645
      end
    end

    assert_match(/0 .* but 1/, error.message)
  end

646 647 648
  def test_assert_performed_jobs_with_only_option
    assert_nothing_raised do
      assert_performed_jobs 1, only: HelloJob do
649
        HelloJob.perform_later("jeremy")
650 651 652 653 654
        LoggingJob.perform_later
      end
    end
  end

655 656 657 658 659 660 661 662 663 664
  def test_assert_performed_jobs_with_except_option
    assert_nothing_raised do
      assert_performed_jobs 1, except: LoggingJob do
        HelloJob.perform_later("jeremy")
        LoggingJob.perform_later
      end
    end
  end

  def test_assert_performed_jobs_with_only_and_except_option
665
    error = assert_raise ArgumentError do
666 667 668 669 670 671 672 673 674
      assert_performed_jobs 1, only: HelloJob, except: HelloJob do
        HelloJob.perform_later("jeremy")
        LoggingJob.perform_later
      end
    end

    assert_match(/`:only` and `:except`/, error.message)
  end

675 676 677
  def test_assert_performed_jobs_with_only_option_as_array
    assert_nothing_raised do
      assert_performed_jobs 2, only: [HelloJob, LoggingJob] do
678 679 680
        HelloJob.perform_later("jeremy")
        LoggingJob.perform_later("stewie")
        RescueJob.perform_later("david")
681 682 683 684
      end
    end
  end

685 686 687 688 689 690 691 692 693 694 695
  def test_assert_performed_jobs_with_except_option_as_array
    assert_nothing_raised do
      assert_performed_jobs 1, except: [LoggingJob, RescueJob] do
        HelloJob.perform_later("jeremy")
        LoggingJob.perform_later("stewie")
        RescueJob.perform_later("david")
      end
    end
  end

  def test_assert_performed_jobs_with_only_and_except_option_as_array
696
    error = assert_raise ArgumentError do
697 698 699 700 701 702 703 704 705 706
      assert_performed_jobs 2, only: [HelloJob, LoggingJob], except: [HelloJob, LoggingJob] do
        HelloJob.perform_later("jeremy")
        LoggingJob.perform_later("stewie")
        RescueJob.perform_later("david")
      end
    end

    assert_match(/`:only` and `:except`/, error.message)
  end

707 708 709 710 711 712 713 714 715 716
  def test_assert_performed_jobs_with_only_option_and_none_sent
    error = assert_raise ActiveSupport::TestCase::Assertion do
      assert_performed_jobs 1, only: HelloJob do
        LoggingJob.perform_later
      end
    end

    assert_match(/1 .* but 0/, error.message)
  end

717 718 719 720 721 722 723 724 725 726 727
  def test_assert_performed_jobs_with_except_option_and_none_sent
    error = assert_raise ActiveSupport::TestCase::Assertion do
      assert_performed_jobs 1, except: LoggingJob do
        LoggingJob.perform_later
      end
    end

    assert_match(/1 .* but 0/, error.message)
  end

  def test_assert_performed_jobs_with_only_and_except_option_and_none_sent
728
    error = assert_raise ArgumentError do
729 730 731 732 733 734 735 736
      assert_performed_jobs 1, only: HelloJob, except: HelloJob do
        LoggingJob.perform_later
      end
    end

    assert_match(/`:only` and `:except`/, error.message)
  end

737 738 739
  def test_assert_performed_jobs_with_only_option_and_too_few_sent
    error = assert_raise ActiveSupport::TestCase::Assertion do
      assert_performed_jobs 5, only: HelloJob do
740
        HelloJob.perform_later("jeremy")
741 742 743 744 745 746 747
        4.times { LoggingJob.perform_later }
      end
    end

    assert_match(/5 .* but 1/, error.message)
  end

748 749 750 751 752 753 754 755 756 757 758 759
  def test_assert_performed_jobs_with_except_option_and_too_few_sent
    error = assert_raise ActiveSupport::TestCase::Assertion do
      assert_performed_jobs 5, except: LoggingJob do
        HelloJob.perform_later("jeremy")
        4.times { LoggingJob.perform_later }
      end
    end

    assert_match(/5 .* but 1/, error.message)
  end

  def test_assert_performed_jobs_with_only_and_except_option_and_too_few_sent
760
    error = assert_raise ArgumentError do
761 762 763 764 765 766 767 768 769
      assert_performed_jobs 5, only: HelloJob, except: HelloJob do
        HelloJob.perform_later("jeremy")
        4.times { LoggingJob.perform_later }
      end
    end

    assert_match(/`:only` and `:except`/, error.message)
  end

770 771 772
  def test_assert_performed_jobs_with_only_option_and_too_many_sent
    error = assert_raise ActiveSupport::TestCase::Assertion do
      assert_performed_jobs 1, only: HelloJob do
773
        2.times { HelloJob.perform_later("jeremy") }
774 775 776 777 778 779
      end
    end

    assert_match(/1 .* but 2/, error.message)
  end

780 781 782 783 784 785 786 787 788 789 790
  def test_assert_performed_jobs_with_except_option_and_too_many_sent
    error = assert_raise ActiveSupport::TestCase::Assertion do
      assert_performed_jobs 1, except: LoggingJob do
        2.times { HelloJob.perform_later("jeremy") }
      end
    end

    assert_match(/1 .* but 2/, error.message)
  end

  def test_assert_performed_jobs_with_only_and_except_option_and_too_many_sent
791
    error = assert_raise ArgumentError do
792 793 794 795 796 797 798 799
      assert_performed_jobs 1, only: HelloJob, except: HelloJob do
        2.times { HelloJob.perform_later("jeremy") }
      end
    end

    assert_match(/`:only` and `:except`/, error.message)
  end

800 801 802 803 804 805 806 807
  def test_assert_no_performed_jobs_with_only_option
    assert_nothing_raised do
      assert_no_performed_jobs only: HelloJob do
        LoggingJob.perform_later
      end
    end
  end

808 809 810 811 812 813 814 815 816
  def test_assert_no_performed_jobs_with_except_option
    assert_nothing_raised do
      assert_no_performed_jobs except: LoggingJob do
        LoggingJob.perform_later
      end
    end
  end

  def test_assert_no_performed_jobs_with_only_and_except_option
817
    error = assert_raise ArgumentError do
818 819 820 821 822 823 824 825
      assert_no_performed_jobs only: HelloJob, except: HelloJob do
        LoggingJob.perform_later
      end
    end

    assert_match(/`:only` and `:except`/, error.message)
  end

826 827 828 829 830 831 832 833
  def test_assert_no_performed_jobs_with_only_option_as_array
    assert_nothing_raised do
      assert_no_performed_jobs only: [HelloJob, RescueJob] do
        LoggingJob.perform_later
      end
    end
  end

834 835 836 837 838 839 840 841 842 843
  def test_assert_no_performed_jobs_with_except_option_as_array
    assert_nothing_raised do
      assert_no_performed_jobs except: [HelloJob, RescueJob] do
        HelloJob.perform_later
        RescueJob.perform_later
      end
    end
  end

  def test_assert_no_performed_jobs_with_only_and_except_option_as_array
844
    error = assert_raise ArgumentError do
845 846 847 848 849 850 851 852
      assert_no_performed_jobs only: [HelloJob, RescueJob], except: [HelloJob, RescueJob] do
        LoggingJob.perform_later
      end
    end

    assert_match(/`:only` and `:except`/, error.message)
  end

853 854 855
  def test_assert_no_performed_jobs_with_only_option_failure
    error = assert_raise ActiveSupport::TestCase::Assertion do
      assert_no_performed_jobs only: HelloJob do
856
        HelloJob.perform_later("jeremy")
857 858 859 860 861 862 863
        LoggingJob.perform_later
      end
    end

    assert_match(/0 .* but 1/, error.message)
  end

864 865 866 867 868 869 870 871 872 873 874 875
  def test_assert_no_performed_jobs_with_except_option_failure
    error = assert_raise ActiveSupport::TestCase::Assertion do
      assert_no_performed_jobs except: LoggingJob do
        HelloJob.perform_later("jeremy")
        LoggingJob.perform_later
      end
    end

    assert_match(/0 .* but 1/, error.message)
  end

  def test_assert_no_performed_jobs_with_only_and_except_option_failure
876
    error = assert_raise ArgumentError do
877 878 879 880 881 882 883 884 885
      assert_no_performed_jobs only: HelloJob, except: HelloJob do
        HelloJob.perform_later("jeremy")
        LoggingJob.perform_later
      end
    end

    assert_match(/`:only` and `:except`/, error.message)
  end

A
Abdelkader Boudih 已提交
886
  def test_assert_performed_job
887
    assert_performed_with(job: NestedJob, queue: "default") do
C
Cristian Bica 已提交
888
      NestedJob.perform_later
A
Abdelkader Boudih 已提交
889 890 891
    end
  end

892
  def test_assert_performed_job_returns
893
    job = assert_performed_with(job: NestedJob, queue: "default") do
894 895 896 897 898 899
      NestedJob.perform_later
    end

    assert_instance_of NestedJob, job
    assert_nil job.scheduled_at
    assert_equal [], job.arguments
900
    assert_equal "default", job.queue_name
901 902
  end

A
Abdelkader Boudih 已提交
903 904
  def test_assert_performed_job_failure
    assert_raise ActiveSupport::TestCase::Assertion do
905 906
      assert_performed_with(job: LoggingJob) do
        HelloJob.perform_later
A
Abdelkader Boudih 已提交
907 908 909 910
      end
    end

    assert_raise ActiveSupport::TestCase::Assertion do
911 912
      assert_performed_with(job: HelloJob, queue: "low") do
        HelloJob.set(queue: "important").perform_later
A
Abdelkader Boudih 已提交
913 914 915
      end
    end
  end
916

917 918 919 920
  def test_assert_performed_job_with_at_option
    assert_performed_with(job: HelloJob, at: Date.tomorrow.noon) do
      HelloJob.set(wait_until: Date.tomorrow.noon).perform_later
    end
921 922 923 924

    assert_raise ActiveSupport::TestCase::Assertion do
      assert_performed_with(job: HelloJob, at: Date.today.noon) do
        HelloJob.set(wait_until: Date.tomorrow.noon).perform_later
A
Abdelkader Boudih 已提交
925 926 927
      end
    end
  end
928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946

  def test_assert_performed_job_with_global_id_args
    ricardo = Person.new(9)
    assert_performed_with(job: HelloJob, args: [ricardo]) do
      HelloJob.perform_later(ricardo)
    end
  end

  def test_assert_performed_job_failure_with_global_id_args
    ricardo = Person.new(9)
    wilma = Person.new(11)
    error = assert_raise ActiveSupport::TestCase::Assertion do
      assert_performed_with(job: HelloJob, args: [wilma]) do
        HelloJob.perform_later(ricardo)
      end
    end

    assert_equal "No performed job found with {:job=>HelloJob, :args=>[#{wilma.inspect}]}", error.message
  end
947 948 949 950 951 952 953 954 955 956

  def test_assert_performed_job_does_not_change_jobs_count
    assert_performed_with(job: HelloJob) do
      HelloJob.perform_later
    end

    assert_performed_with(job: HelloJob) do
      HelloJob.perform_later
    end

957
    assert_equal 2, queue_adapter.performed_jobs.count
958
  end
A
Abdelkader Boudih 已提交
959
end
960 961 962 963 964 965 966 967 968 969 970 971

class OverrideQueueAdapterTest < ActiveJob::TestCase
  class CustomQueueAdapter < ActiveJob::QueueAdapters::TestAdapter; end

  def queue_adapter_for_test
    CustomQueueAdapter.new
  end

  def test_assert_job_has_custom_queue_adapter_set
    assert_instance_of CustomQueueAdapter, HelloJob.queue_adapter
  end
end
972 973 974 975 976 977

class InheritedJobTest < ActiveJob::TestCase
  def test_queue_adapter_is_test_adapter
    assert_instance_of ActiveJob::QueueAdapters::TestAdapter, InheritedJob.queue_adapter
  end
end
978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994

class QueueAdapterJobTest < ActiveJob::TestCase
  def before_setup
    @original_autoload_paths = ActiveSupport::Dependencies.autoload_paths
    ActiveSupport::Dependencies.autoload_paths = %w(test/jobs)
    super
  end

  def after_teardown
    ActiveSupport::Dependencies.autoload_paths = @original_autoload_paths
    super
  end

  def test_queue_adapter_is_test_adapter
    assert_instance_of ActiveJob::QueueAdapters::TestAdapter, QueueAdapterJob.queue_adapter
  end
end