github.com/blend/go-sdk@v1.20220411.3/cron/daily_schedule_test.go (about)

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