go.charczuk.com@v0.0.0-20240327042549-bc490516bd1a/sdk/cron/daily_schedule_test.go (about)

     1  /*
     2  
     3  Copyright (c) 2023 - Present. Will Charczuk. All rights reserved.
     4  Use of this source code is governed by a MIT license that can be found in the LICENSE file at the root of the repository.
     5  
     6  */
     7  
     8  package cron
     9  
    10  import (
    11  	"testing"
    12  	"time"
    13  
    14  	. "go.charczuk.com/sdk/assert"
    15  )
    16  
    17  func Test_DailyAtUTC(t *testing.T) {
    18  	schedule := DailyAtUTC(12, 0, 0) //noon
    19  	now := time.Now().UTC()
    20  	beforenoon := time.Date(now.Year(), now.Month(), now.Day(), 11, 0, 0, 0, time.UTC)
    21  	afternoon := time.Date(now.Year(), now.Month(), now.Day(), 13, 0, 0, 0, time.UTC)
    22  	todayAtNoon := schedule.Next(beforenoon)
    23  	tomorrowAtNoon := schedule.Next(afternoon)
    24  
    25  	ItsEqual(t, true, todayAtNoon.Before(afternoon))
    26  	ItsEqual(t, true, tomorrowAtNoon.After(afternoon))
    27  }
    28  
    29  func Test_WeeklyAtUTC(t *testing.T) {
    30  	schedule := WeeklyAtUTC(12, 0, 0, time.Monday)               //every monday at noon
    31  	beforenoon := time.Date(2016, 01, 11, 11, 0, 0, 0, time.UTC) //these are both a monday
    32  	afternoon := time.Date(2016, 01, 11, 13, 0, 0, 0, time.UTC)  //these are both a monday
    33  
    34  	sundayBeforeNoon := time.Date(2016, 01, 17, 11, 0, 0, 0, time.UTC) //to gut check that it's monday
    35  
    36  	todayAtNoon := schedule.Next(beforenoon)
    37  	nextWeekAtNoon := schedule.Next(afternoon)
    38  
    39  	ItsEqual(t, true, todayAtNoon.Before(afternoon))
    40  	ItsEqual(t, true, nextWeekAtNoon.After(afternoon))
    41  	ItsEqual(t, true, nextWeekAtNoon.After(sundayBeforeNoon))
    42  	ItsEqual(t, time.Monday, nextWeekAtNoon.Weekday())
    43  }
    44  
    45  func Test_OnTheHourAt(t *testing.T) {
    46  	now := time.Now().UTC()
    47  	schedule := EveryHourAt(40, 00)
    48  
    49  	fromNil := schedule.Next(Zero)
    50  	ItsNotNil(t, fromNil)
    51  
    52  	fromNilExpected := time.Date(now.Year(), now.Month(), now.Day(), now.Hour(), 40, 0, 0, time.UTC)
    53  	if fromNilExpected.Before(now) {
    54  		fromNilExpected = fromNilExpected.Add(time.Hour)
    55  	}
    56  	ItsEqual(t, true, inTimeDelta(fromNilExpected, fromNil, time.Second))
    57  
    58  	fromHalfStart := time.Date(now.Year(), now.Month(), now.Day(), now.Hour(), 45, 0, 0, time.UTC)
    59  	fromHalfExpected := time.Date(now.Year(), now.Month(), now.Day(), now.Hour()+1, 40, 0, 0, time.UTC)
    60  
    61  	fromHalf := schedule.Next(fromHalfStart)
    62  
    63  	ItsNotNil(t, fromHalf)
    64  	ItsEqual(t, true, inTimeDelta(fromHalfExpected, fromHalf, time.Second))
    65  }