github.com/Aoi-hosizora/ahlib-more@v1.5.1-0.20230404072844-256112befaf6/xtask/xtask_test.go (about)

     1  package xtask
     2  
     3  import (
     4  	"fmt"
     5  	"github.com/Aoi-hosizora/ahlib/xtesting"
     6  	"github.com/robfig/cron/v3"
     7  	"log"
     8  	"testing"
     9  	"time"
    10  )
    11  
    12  func TestCronTask(t *testing.T) {
    13  	t.Run("basic", func(t *testing.T) {
    14  		c := cron.New(cron.WithSeconds())
    15  		// c := cron.New(cron.WithSeconds(), cron.WithLogger(cron.DiscardLogger))
    16  		task := NewCronTask(c)
    17  		xtesting.Equal(t, task.Cron(), c)
    18  		xtesting.Equal(t, len(task.Jobs()), 0)
    19  		schedule, err := task.ScheduleParser().Parse("30 12 * * * *")
    20  		xtesting.Nil(t, err)
    21  		parser2 := cron.NewParser(cron.Second | cron.Minute | cron.Hour | cron.Dom | cron.Month | cron.Dow | cron.Descriptor)
    22  		schedule2, err := parser2.Parse("30 12 * * * *")
    23  		xtesting.Nil(t, err)
    24  		xtesting.Equal(t, schedule, schedule2)
    25  	})
    26  
    27  	t.Run("add and remove", func(t *testing.T) {
    28  		c := cron.New(cron.WithSeconds())
    29  		task := NewCronTask(c)
    30  
    31  		xtesting.Panic(t, func() {
    32  			task.AddJobByCronSpec("", "", nil)
    33  		})
    34  		xtesting.Panic(t, func() {
    35  			task.AddJobBySchedule("", nil, func() { return })
    36  		})
    37  		xtesting.Panic(t, func() {
    38  			task.AddJobBySchedule("", cron.Every(time.Second), nil)
    39  		})
    40  
    41  		// 1
    42  		id1, err := task.AddJobByCronSpec("job1", "0/1 * * * * *", func() { return })
    43  		xtesting.Nil(t, err)
    44  		xtesting.Equal(t, id1, cron.EntryID(1))
    45  		// 2
    46  		_, err = task.AddJobByCronSpec("job2", "@", func() { return })
    47  		xtesting.NotNil(t, err)
    48  		id2, err := task.AddJobByCronSpec("job2", "0/2 * * * * *", func() { return })
    49  		xtesting.Equal(t, id2, cron.EntryID(2))
    50  		sch2, _ := task.ScheduleParser().Parse("0/2 * * * * *")
    51  		// 3
    52  		sch3 := cron.Every(time.Second * 3)
    53  		id3 := task.AddJobBySchedule("job3", sch3, func() { return })
    54  		xtesting.Equal(t, id3, cron.EntryID(3))
    55  		// remove 3
    56  		xtesting.Equal(t, task.Jobs()[2].ScheduleExpr(), "every 3s")
    57  		task.RemoveJob(id3)
    58  		// 4
    59  		sch4, err := task.ScheduleParser().Parse("0/4 * * * * *")
    60  		xtesting.Nil(t, err)
    61  		id4 := task.AddJobBySchedule("job4", sch4, func() { return })
    62  		xtesting.Equal(t, id4, cron.EntryID(4))
    63  
    64  		xtesting.Equal(t, task.Jobs()[0].Title(), "job1")
    65  		xtesting.Equal(t, task.Jobs()[0].EntryID(), cron.EntryID(1))
    66  		xtesting.Equal(t, task.Jobs()[1].CronSpec(), "0/2 * * * * *")
    67  		xtesting.Equal(t, task.Jobs()[1].Schedule(), sch2)
    68  		xtesting.Equal(t, task.Jobs()[2].Schedule(), sch4)
    69  		xtesting.Equal(t, task.Jobs()[2].CronSpec(), "")
    70  		xtesting.Equal(t, task.Jobs()[2].Entry().ID, cron.EntryID(4))
    71  		xtesting.Equal(t, task.Jobs()[0].ScheduleExpr(), "0/1 * * * * *")
    72  		xtesting.Equal(t, task.Jobs()[1].ScheduleExpr(), "0/2 * * * * *")
    73  		xtesting.Equal(t, task.Jobs()[2].ScheduleExpr(), "<parsed SpecSchedule>")
    74  		f := &FuncJob{schedule: nil}
    75  		xtesting.Equal(t, f.ScheduleExpr(), "<unknown Schedule>") // fake
    76  	})
    77  }
    78  
    79  func TestFuncJob(t *testing.T) {
    80  	t.Run("start", func(t *testing.T) {
    81  		c := cron.New(cron.WithSeconds())
    82  		task := NewCronTask(c)
    83  
    84  		task.AddJobByCronSpec("every1s", "0/1 * * * * *", func() {
    85  			log.Printf("every1s_1_%s", time.Now().Format(time.RFC3339Nano))
    86  		})
    87  		task.SetAddedCallback(DefaultColorizedAddedCallback)
    88  		task.AddJobByCronSpec("every2s", "0/2 * * * * *", func() {
    89  			log.Printf("every2s_2_%s", time.Now().Format(time.RFC3339Nano))
    90  		})
    91  		task.SetRemovedCallback(func(j *FuncJob) {
    92  			log.Printf("[Task] Remove job: %s | EntryID: %d", j.Title(), j.EntryID())
    93  		})
    94  		task.RemoveJob(2)
    95  		task.SetAddedCallback(func(j *FuncJob) {
    96  			log.Printf("[Task] %-29s | %s (EntryID: %d)", fmt.Sprintf("%s, %s", j.Title(), j.ScheduleExpr()), j.Funcname(), j.EntryID())
    97  		})
    98  		task.AddJobByCronSpec("every2s", "0/2 * * * * *", func() {
    99  			log.Printf("every2s_3_%s", time.Now().Format(time.RFC3339Nano))
   100  		})
   101  		task.AddJobByCronSpec("every1s", "0/1 * * * * *", func() {
   102  			log.Printf("every1s_4_%s", time.Now().Format(time.RFC3339Nano))
   103  		})
   104  		task.SetScheduledCallback(func(j *FuncJob) {
   105  			log.Printf("[Task] Executing job: %s", j.Title())
   106  		})
   107  		xtesting.Equal(t, len(task.Jobs()), 3)
   108  		xtesting.Equal(t, len(task.Cron().Entries()), 3)
   109  
   110  		task.Cron().Start()
   111  		time.Sleep(time.Second * 3)
   112  		<-task.Cron().Stop().Done()
   113  	})
   114  
   115  	t.Run("panic", func(t *testing.T) {
   116  		c := cron.New(cron.WithSeconds())
   117  		task := NewCronTask(c)
   118  		task.AddJobBySchedule("panic", cron.Every(time.Second), func() {
   119  			panic("test")
   120  		})
   121  
   122  		task.Cron().Start()
   123  		time.Sleep(time.Second + time.Millisecond*200)
   124  		panicV := (interface{})(nil)
   125  		task.SetPanicHandler(func(job *FuncJob, v interface{}) { log.Printf("panic: %v | %s", v, job.Title()); panicV = v })
   126  		time.Sleep(time.Second + time.Millisecond*200)
   127  		xtesting.Equal(t, panicV, "test")
   128  		<-task.Cron().Stop().Done()
   129  	})
   130  }