Simple test using Runnables.
(t *testing.T)
| 434 | |
| 435 | // Simple test using Runnables. |
| 436 | func TestJob(t *testing.T) { |
| 437 | wg := &sync.WaitGroup{} |
| 438 | wg.Add(1) |
| 439 | |
| 440 | cron := newWithSeconds() |
| 441 | cron.AddJob("0 0 0 30 Feb ?", testJob{wg, "job0"}) |
| 442 | cron.AddJob("0 0 0 1 1 ?", testJob{wg, "job1"}) |
| 443 | job2, _ := cron.AddJob("* * * * * ?", testJob{wg, "job2"}) |
| 444 | cron.AddJob("1 0 0 1 1 ?", testJob{wg, "job3"}) |
| 445 | cron.Schedule(Every(5*time.Second+5*time.Nanosecond), testJob{wg, "job4"}) |
| 446 | job5 := cron.Schedule(Every(5*time.Minute), testJob{wg, "job5"}) |
| 447 | |
| 448 | // Test getting an Entry pre-Start. |
| 449 | if actualName := cron.Entry(job2).Job.(testJob).name; actualName != "job2" { |
| 450 | t.Error("wrong job retrieved:", actualName) |
| 451 | } |
| 452 | if actualName := cron.Entry(job5).Job.(testJob).name; actualName != "job5" { |
| 453 | t.Error("wrong job retrieved:", actualName) |
| 454 | } |
| 455 | |
| 456 | cron.Start() |
| 457 | defer cron.Stop() |
| 458 | |
| 459 | select { |
| 460 | case <-time.After(OneSecond): |
| 461 | t.FailNow() |
| 462 | case <-wait(wg): |
| 463 | } |
| 464 | |
| 465 | // Ensure the entries are in the right order. |
| 466 | expecteds := []string{"job2", "job4", "job5", "job1", "job3", "job0"} |
| 467 | |
| 468 | var actuals []string |
| 469 | for _, entry := range cron.Entries() { |
| 470 | actuals = append(actuals, entry.Job.(testJob).name) |
| 471 | } |
| 472 | |
| 473 | for i, expected := range expecteds { |
| 474 | if actuals[i] != expected { |
| 475 | t.Fatalf("Jobs not in the right order. (expected) %s != %s (actual)", expecteds, actuals) |
| 476 | } |
| 477 | } |
| 478 | |
| 479 | // Test getting Entries. |
| 480 | if actualName := cron.Entry(job2).Job.(testJob).name; actualName != "job2" { |
| 481 | t.Error("wrong job retrieved:", actualName) |
| 482 | } |
| 483 | if actualName := cron.Entry(job5).Job.(testJob).name; actualName != "job5" { |
| 484 | t.Error("wrong job retrieved:", actualName) |
| 485 | } |
| 486 | } |
| 487 | |
| 488 | // Issue #206 |
| 489 | // Ensure that the next run of a job after removing an entry is accurate. |