gitee.com/h79/goutils@v1.22.10/common/scheduler/scheduler_test.go (about)

     1  package scheduler
     2  
     3  import (
     4  	"fmt"
     5  	"gitee.com/h79/goutils/common/option"
     6  	"gitee.com/h79/goutils/common/system"
     7  	"gitee.com/h79/goutils/common/timer"
     8  	"testing"
     9  	"time"
    10  )
    11  
    12  func TestScheduler(t *testing.T) {
    13  	sched := NewScheduler(4)
    14  	sched.Run()
    15  
    16  	sched.AddTask(NewShellJob("ls -ltf").WithMustFlag(QuitExecMustFlag))
    17  	for i := 0; i < 4; i++ {
    18  		_ = sched.AddTriggerTask(NewShellJob(fmt.Sprintf("cp job.go xxx-%d.txt", i)).WithHandlerFunc(func(job *Job, opts ...option.Option) (interface{}, error) {
    19  			t.Logf("jobId= '%s',quited= %v, at: %s", job.GetId(), IsQuited(opts...), timer.CurrentTime())
    20  			return nil, nil
    21  		}).WithMustFlag(QuitExecMustFlag), NewSimpleTrigger(time.Second*10))
    22  	}
    23  	idx := 0
    24  	cron, _ := NewCronTrigger("*/5 * * * * ?")
    25  	_ = sched.AddTriggerTask(NewJob("test", "1", 0).WithHandlerFunc(func(job *Job, opts ...option.Option) (interface{}, error) {
    26  		idx++
    27  		t.Logf("jobId= %s,quited= %v", job.GetId(), IsQuited(opts...))
    28  		var s = system.Shell{}
    29  		res := s.Run(fmt.Sprintf("cp job.go xxx-w-%d.txt", idx))
    30  		if res.Err != nil {
    31  			job.State = ErrorState
    32  			return res, res.Err
    33  		}
    34  		job.State = CompletedState
    35  		return res, nil
    36  	}), cron)
    37  
    38  	_ = sched.AddTriggerTask(NewJob("test", "2", 0).WithHandlerFunc(func(job *Job, opts ...option.Option) (interface{}, error) {
    39  		idx++
    40  		t.Logf("jobId= %s,quited= %v,at: %s", job.GetId(), IsQuited(opts...), timer.CurrentTime())
    41  		return nil, nil
    42  	}).WithMustFlag(QuitExecMustFlag), NewSimpleTrigger(time.Second*2))
    43  
    44  	system.ChildRunning(func() {
    45  		time.Sleep(time.Second * 10)
    46  
    47  		sched.Foreach(func(group int, task Task, tk time.Duration, now, trigger int64) bool {
    48  			t.Logf("jobId= '%s' in group=%d,time= %v,trigger= %v", task.GetId(), group, tk, time.Duration(trigger))
    49  			return false
    50  		})
    51  		//system.Close()
    52  	})
    53  
    54  	system.MainRunning(func() {
    55  		for {
    56  			select {
    57  			case <-system.Closed():
    58  				return
    59  			}
    60  		}
    61  	})
    62  	system.Go().Wait()
    63  }
    64  
    65  func TestNewShellJob(t *testing.T) {
    66  	job := NewShellJob("cp job.go xxx.txt")
    67  	res, err := job.Execute()
    68  	if err != nil {
    69  		return
    70  	}
    71  
    72  	t.Log(res)
    73  
    74  	job = NewShellJob("ls -ltf")
    75  	res, err = job.Execute()
    76  	if err != nil {
    77  		return
    78  	}
    79  	t.Log(res)
    80  }
    81  
    82  func TestNewPool(t *testing.T) {
    83  	pool := NewPool(1, 10)
    84  
    85  	pool.AddJob(
    86  		NewEmptyJob(func(job *Job, opts ...option.Option) (interface{}, error) {
    87  			t.Log("run 0")
    88  			return nil, nil
    89  		}).WithDelay(time.Second * 3))
    90  
    91  	pool.AddJob(
    92  		NewEmptyJob(func(job *Job, opts ...option.Option) (interface{}, error) {
    93  			t.Logf("run 1, quited= %v", IsQuited(opts...))
    94  			return nil, nil
    95  		}).WithDelay(time.Second * 15))
    96  
    97  	pool.AddJob(
    98  		NewEmptyJob(func(job *Job, opts ...option.Option) (interface{}, error) {
    99  			t.Logf("run 2, quited= %v", IsQuited(opts...))
   100  			return nil, nil
   101  		}).WithDelay(time.Second * 0))
   102  
   103  	system.ChildRunning(func() {
   104  		time.Sleep(time.Second * 5)
   105  		for i := 0; i < 5; i++ {
   106  			pool.AddJob(
   107  				NewJob("", fmt.Sprintf("%d", i+10), 0).
   108  					WithHandlerFunc(func(job *Job, opts ...option.Option) (interface{}, error) {
   109  						t.Logf("run %s, quited= %v", job.GetId(), IsQuited(opts...))
   110  						return nil, nil
   111  					}).
   112  					WithDelay(time.Second * 0).WithMustFlag(QuitExecMustFlag))
   113  		}
   114  		system.Close()
   115  	})
   116  	system.MainRunning(func() {
   117  		for {
   118  			select {
   119  			case <-system.Closed():
   120  				return
   121  			}
   122  		}
   123  	})
   124  	system.Go().Wait()
   125  }