(t *testing.T)
| 18 | var baseTime = time.Date(2020, 1, 1, 0, 0, 0, 0, time.UTC) |
| 19 | |
| 20 | func TestScheduler(t *testing.T) { |
| 21 | ch := make(chan string, 1000) |
| 22 | |
| 23 | // times of upcoming events |
| 24 | it1 := scheduler.Item{"it1", baseTime.Add(100 * time.Millisecond), reportTriggered(t, ch, "it1")} |
| 25 | it2a := scheduler.Item{"it2", baseTime.Add(200 * time.Millisecond), reportTriggered(t, ch, "it2")} |
| 26 | it2b := scheduler.Item{"it2", baseTime.Add(200 * time.Millisecond), reportTriggered(t, ch, "it2")} |
| 27 | it3a := scheduler.Item{"it3", baseTime.Add(300 * time.Millisecond), reportTriggered(t, ch, "it3")} |
| 28 | it3b := scheduler.Item{"it3", baseTime.Add(300 * time.Millisecond), reportTriggered(t, ch, "it3")} |
| 29 | it4 := scheduler.Item{"it4", baseTime.Add(30 * time.Hour), reportTriggered(t, ch, "it4")} |
| 30 | |
| 31 | items := []scheduler.Item{it1, it2a, it2b, it3a, it3b, it4} |
| 32 | |
| 33 | // verify that the item order does not matter by shuffling the items. |
| 34 | rand.Shuffle(len(items), func(i, j int) { items[i], items[j] = items[j], items[i] }) |
| 35 | |
| 36 | ctx := testlogging.Context(t) |
| 37 | |
| 38 | ft := faketime.NewClockTimeWithOffset(baseTime.Sub(clock.Now())) |
| 39 | |
| 40 | refresh := make(chan string) |
| 41 | s := scheduler.Start(ctx, func(ctx context.Context, now time.Time) []scheduler.Item { |
| 42 | var result []scheduler.Item |
| 43 | |
| 44 | for _, it := range items { |
| 45 | if it.NextTime.After(now) { |
| 46 | result = append(result, it) |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | return result |
| 51 | }, scheduler.Options{ |
| 52 | TimeNow: ft.NowFunc(), |
| 53 | Debug: true, |
| 54 | RefreshChannel: refresh, |
| 55 | }) |
| 56 | |
| 57 | defer s.Stop() |
| 58 | |
| 59 | // ensure that the first few items are triggered in order. |
| 60 | require.Equal(t, "it1", <-ch) |
| 61 | require.Equal(t, "it2", <-ch) |
| 62 | require.Equal(t, "it2", <-ch) |
| 63 | require.Equal(t, "it3", <-ch) |
| 64 | require.Equal(t, "it3", <-ch) |
| 65 | |
| 66 | // it4 is far into the future, make sure nothing is triggered immediately |
| 67 | select { |
| 68 | case v := <-ch: |
| 69 | t.Fatalf("unexpected item: %v", v) |
| 70 | |
| 71 | case <-time.After(1 * time.Second): |
| 72 | } |
| 73 | |
| 74 | // now change the set of items returned by adding it5 which comes before it4 |
| 75 | it5 := scheduler.Item{"it5", ft.NowFunc()().Add(time.Second), reportTriggered(t, ch, "it5")} |
| 76 | items = []scheduler.Item{it1, it2a, it2b, it3a, it3b, it4, it5} |
| 77 |
nothing calls this directly
no test coverage detected