github.com/ngicks/gokugen@v0.0.5/cron/schedule_state_test.go (about)

     1  package cron_test
     2  
     3  import (
     4  	"testing"
     5  	"time"
     6  
     7  	"github.com/golang/mock/gomock"
     8  	"github.com/ngicks/gokugen/cron"
     9  
    10  	mock_cron "github.com/ngicks/gokugen/cron/__mock"
    11  	"github.com/stretchr/testify/require"
    12  )
    13  
    14  func TestScheduleState(t *testing.T) {
    15  	nextScheduler := cron.Duration{
    16  		Duration: time.Second,
    17  		Command:  []string{"foo"},
    18  	}
    19  	now := time.Now()
    20  	state := cron.NewScheduleState(nextScheduler, now)
    21  
    22  	for i := 0; i < 100; i++ {
    23  		same, count, next := state.Next()
    24  		require.Equal(t, false, same)
    25  		require.Equal(t, i, count)
    26  		require.Equal(t, now.Add(time.Duration(i+1)*time.Second).Equal(next), true)
    27  	}
    28  
    29  	mockNextScheduler := mock_cron.NewMockNextScheduler(gomock.NewController(t))
    30  	mockNextScheduler.
    31  		EXPECT().
    32  		NextSchedule(gomock.Any()).
    33  		DoAndReturn(func(now time.Time) (time.Time, error) {
    34  			return time.Date(2022, 1, 1, 1, 0, 0, 0, time.UTC), nil
    35  		}).
    36  		AnyTimes()
    37  	state = cron.NewScheduleState(mockNextScheduler, now)
    38  
    39  	for i := 0; i < 100; i++ {
    40  		same, count, next := state.Next()
    41  		if i == 0 {
    42  			require.Equal(t, false, same)
    43  		} else {
    44  			require.Equal(t, true, same)
    45  		}
    46  		require.Equal(t, i, count)
    47  		require.Equal(t, time.Date(2022, 1, 1, 1, 0, 0, 0, time.UTC).Equal(next), true)
    48  	}
    49  }