github.com/zhongdalu/gf@v1.0.0/g/os/gcron/gcron_unit_1_test.go (about)

     1  // Copyright 2018 gf Author(https://github.com/zhongdalu/gf). All Rights Reserved.
     2  //
     3  // This Source Code Form is subject to the terms of the MIT License.
     4  // If a copy of the MIT was not distributed with this file,
     5  // You can obtain one at https://github.com/zhongdalu/gf.
     6  
     7  package gcron_test
     8  
     9  import (
    10  	"testing"
    11  	"time"
    12  
    13  	"github.com/zhongdalu/gf/g/container/garray"
    14  	"github.com/zhongdalu/gf/g/os/gcron"
    15  	"github.com/zhongdalu/gf/g/test/gtest"
    16  )
    17  
    18  func TestCron_Add_Close(t *testing.T) {
    19  	gtest.Case(t, func() {
    20  		cron := gcron.New()
    21  		array := garray.New()
    22  		_, err1 := cron.Add("* * * * * *", func() {
    23  			//glog.Println("cron1")
    24  			array.Append(1)
    25  		})
    26  		_, err2 := cron.Add("* * * * * *", func() {
    27  			//glog.Println("cron2")
    28  			array.Append(1)
    29  		}, "test")
    30  		_, err3 := cron.Add("* * * * * *", func() {
    31  			array.Append(1)
    32  		}, "test")
    33  		_, err4 := cron.Add("@every 2s", func() {
    34  			//glog.Println("cron3")
    35  			array.Append(1)
    36  		})
    37  		gtest.Assert(err1, nil)
    38  		gtest.Assert(err2, nil)
    39  		gtest.AssertNE(err3, nil)
    40  		gtest.Assert(err4, nil)
    41  		gtest.Assert(cron.Size(), 3)
    42  		time.Sleep(1200 * time.Millisecond)
    43  		gtest.Assert(array.Len(), 2)
    44  		time.Sleep(1200 * time.Millisecond)
    45  		gtest.Assert(array.Len(), 5)
    46  		cron.Close()
    47  		time.Sleep(1200 * time.Millisecond)
    48  		fixedLength := array.Len()
    49  		time.Sleep(1200 * time.Millisecond)
    50  		gtest.Assert(array.Len(), fixedLength)
    51  	})
    52  }
    53  
    54  func TestCron_Basic(t *testing.T) {
    55  	gtest.Case(t, func() {
    56  		cron := gcron.New()
    57  		cron.Add("* * * * * *", func() {}, "add")
    58  		//fmt.Println("start", time.Now())
    59  		cron.DelayAdd(time.Second, "* * * * * *", func() {}, "delay_add")
    60  		gtest.Assert(cron.Size(), 1)
    61  		time.Sleep(1200 * time.Millisecond)
    62  		gtest.Assert(cron.Size(), 2)
    63  
    64  		cron.Remove("delay_add")
    65  		gtest.Assert(cron.Size(), 1)
    66  
    67  		entry1 := cron.Search("add")
    68  		entry2 := cron.Search("test-none")
    69  		gtest.AssertNE(entry1, nil)
    70  		gtest.Assert(entry2, nil)
    71  	})
    72  }
    73  
    74  func TestCron_Remove(t *testing.T) {
    75  	gtest.Case(t, func() {
    76  		cron := gcron.New()
    77  		array := garray.New()
    78  		cron.Add("* * * * * *", func() {
    79  			array.Append(1)
    80  		}, "add")
    81  		gtest.Assert(array.Len(), 0)
    82  		time.Sleep(1200 * time.Millisecond)
    83  		gtest.Assert(array.Len(), 1)
    84  
    85  		cron.Remove("add")
    86  		gtest.Assert(array.Len(), 1)
    87  		time.Sleep(1200 * time.Millisecond)
    88  		gtest.Assert(array.Len(), 1)
    89  	})
    90  }
    91  
    92  func TestCron_AddSingleton(t *testing.T) {
    93  	// un used, can be removed
    94  	gtest.Case(t, func() {
    95  		cron := gcron.New()
    96  		cron.Add("* * * * * *", func() {}, "add")
    97  		cron.DelayAdd(time.Second, "* * * * * *", func() {}, "delay_add")
    98  		gtest.Assert(cron.Size(), 1)
    99  		time.Sleep(1200 * time.Millisecond)
   100  		gtest.Assert(cron.Size(), 2)
   101  
   102  		cron.Remove("delay_add")
   103  		gtest.Assert(cron.Size(), 1)
   104  
   105  		entry1 := cron.Search("add")
   106  		entry2 := cron.Search("test-none")
   107  		gtest.AssertNE(entry1, nil)
   108  		gtest.Assert(entry2, nil)
   109  	})
   110  	// keep this
   111  	gtest.Case(t, func() {
   112  		cron := gcron.New()
   113  		array := garray.New()
   114  		cron.AddSingleton("* * * * * *", func() {
   115  			array.Append(1)
   116  			time.Sleep(50 * time.Second)
   117  		})
   118  		gtest.Assert(cron.Size(), 1)
   119  		time.Sleep(3500 * time.Millisecond)
   120  		gtest.Assert(array.Len(), 1)
   121  	})
   122  
   123  }
   124  
   125  func TestCron_AddOnce1(t *testing.T) {
   126  	gtest.Case(t, func() {
   127  		cron := gcron.New()
   128  		array := garray.New()
   129  		cron.AddOnce("* * * * * *", func() {
   130  			array.Append(1)
   131  		})
   132  		cron.AddOnce("* * * * * *", func() {
   133  			array.Append(1)
   134  		})
   135  		gtest.Assert(cron.Size(), 2)
   136  		time.Sleep(2500 * time.Millisecond)
   137  		gtest.Assert(array.Len(), 2)
   138  		gtest.Assert(cron.Size(), 0)
   139  	})
   140  }
   141  
   142  func TestCron_AddOnce2(t *testing.T) {
   143  	gtest.Case(t, func() {
   144  		cron := gcron.New()
   145  		array := garray.New()
   146  		cron.AddOnce("@every 2s", func() {
   147  			array.Append(1)
   148  		})
   149  		gtest.Assert(cron.Size(), 1)
   150  		time.Sleep(3000 * time.Millisecond)
   151  		gtest.Assert(array.Len(), 1)
   152  		gtest.Assert(cron.Size(), 0)
   153  	})
   154  }
   155  
   156  func TestCron_AddTimes(t *testing.T) {
   157  	gtest.Case(t, func() {
   158  		cron := gcron.New()
   159  		array := garray.New()
   160  		cron.AddTimes("* * * * * *", 2, func() {
   161  			array.Append(1)
   162  		})
   163  		time.Sleep(3500 * time.Millisecond)
   164  		gtest.Assert(array.Len(), 2)
   165  		gtest.Assert(cron.Size(), 0)
   166  	})
   167  }
   168  
   169  func TestCron_DelayAdd(t *testing.T) {
   170  	gtest.Case(t, func() {
   171  		cron := gcron.New()
   172  		array := garray.New()
   173  		cron.DelayAdd(500*time.Millisecond, "* * * * * *", func() {
   174  			array.Append(1)
   175  		})
   176  		gtest.Assert(cron.Size(), 0)
   177  		time.Sleep(800 * time.Millisecond)
   178  		gtest.Assert(array.Len(), 0)
   179  		gtest.Assert(cron.Size(), 1)
   180  		time.Sleep(1000 * time.Millisecond)
   181  		gtest.Assert(array.Len(), 1)
   182  		gtest.Assert(cron.Size(), 1)
   183  	})
   184  }
   185  
   186  func TestCron_DelayAddSingleton(t *testing.T) {
   187  	gtest.Case(t, func() {
   188  		cron := gcron.New()
   189  		array := garray.New()
   190  		cron.DelayAddSingleton(500*time.Millisecond, "* * * * * *", func() {
   191  			array.Append(1)
   192  			time.Sleep(10 * time.Second)
   193  		})
   194  		gtest.Assert(cron.Size(), 0)
   195  		time.Sleep(2200 * time.Millisecond)
   196  		gtest.Assert(array.Len(), 1)
   197  		gtest.Assert(cron.Size(), 1)
   198  	})
   199  }
   200  
   201  func TestCron_DelayAddOnce(t *testing.T) {
   202  	gtest.Case(t, func() {
   203  		cron := gcron.New()
   204  		array := garray.New()
   205  		cron.DelayAddOnce(500*time.Millisecond, "* * * * * *", func() {
   206  			array.Append(1)
   207  		})
   208  		gtest.Assert(cron.Size(), 0)
   209  		time.Sleep(800 * time.Millisecond)
   210  		gtest.Assert(array.Len(), 0)
   211  		gtest.Assert(cron.Size(), 1)
   212  		time.Sleep(2200 * time.Millisecond)
   213  		gtest.Assert(array.Len(), 1)
   214  		gtest.Assert(cron.Size(), 0)
   215  	})
   216  }
   217  
   218  func TestCron_DelayAddTimes(t *testing.T) {
   219  	gtest.Case(t, func() {
   220  		cron := gcron.New()
   221  		array := garray.New()
   222  		cron.DelayAddTimes(500*time.Millisecond, "* * * * * *", 2, func() {
   223  			array.Append(1)
   224  		})
   225  		gtest.Assert(cron.Size(), 0)
   226  		time.Sleep(800 * time.Millisecond)
   227  		gtest.Assert(array.Len(), 0)
   228  		gtest.Assert(cron.Size(), 1)
   229  		time.Sleep(3000 * time.Millisecond)
   230  		gtest.Assert(array.Len(), 2)
   231  		gtest.Assert(cron.Size(), 0)
   232  	})
   233  }