github.com/gogf/gf@v1.16.9/os/gtimer/gtimer_z_unit_entry_test.go (about)

     1  // Copyright GoFrame Author(https://goframe.org). 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/gogf/gf.
     6  
     7  // Job Operations
     8  
     9  package gtimer_test
    10  
    11  import (
    12  	"testing"
    13  	"time"
    14  
    15  	"github.com/gogf/gf/container/garray"
    16  	"github.com/gogf/gf/os/gtimer"
    17  	"github.com/gogf/gf/test/gtest"
    18  )
    19  
    20  func TestJob_Start_Stop_Close(t *testing.T) {
    21  	gtest.C(t, func(t *gtest.T) {
    22  		timer := New()
    23  		array := garray.New(true)
    24  		job := timer.Add(200*time.Millisecond, func() {
    25  			array.Append(1)
    26  		})
    27  		time.Sleep(250 * time.Millisecond)
    28  		t.Assert(array.Len(), 1)
    29  		job.Stop()
    30  		time.Sleep(250 * time.Millisecond)
    31  		t.Assert(array.Len(), 1)
    32  		job.Start()
    33  		time.Sleep(250 * time.Millisecond)
    34  		t.Assert(array.Len(), 2)
    35  		job.Close()
    36  		time.Sleep(250 * time.Millisecond)
    37  		t.Assert(array.Len(), 2)
    38  
    39  		t.Assert(job.Status(), gtimer.StatusClosed)
    40  	})
    41  }
    42  
    43  func TestJob_Singleton(t *testing.T) {
    44  	gtest.C(t, func(t *gtest.T) {
    45  		timer := New()
    46  		array := garray.New(true)
    47  		job := timer.Add(200*time.Millisecond, func() {
    48  			array.Append(1)
    49  			time.Sleep(10 * time.Second)
    50  		})
    51  		t.Assert(job.IsSingleton(), false)
    52  		job.SetSingleton(true)
    53  		t.Assert(job.IsSingleton(), true)
    54  		time.Sleep(250 * time.Millisecond)
    55  		t.Assert(array.Len(), 1)
    56  
    57  		time.Sleep(250 * time.Millisecond)
    58  		t.Assert(array.Len(), 1)
    59  	})
    60  }
    61  
    62  func TestJob_SetTimes(t *testing.T) {
    63  	gtest.C(t, func(t *gtest.T) {
    64  		timer := New()
    65  		array := garray.New(true)
    66  		job := timer.Add(200*time.Millisecond, func() {
    67  			array.Append(1)
    68  		})
    69  		job.SetTimes(2)
    70  		//job.IsSingleton()
    71  		time.Sleep(1200 * time.Millisecond)
    72  		t.Assert(array.Len(), 2)
    73  	})
    74  }
    75  
    76  func TestJob_Run(t *testing.T) {
    77  	gtest.C(t, func(t *gtest.T) {
    78  		timer := New()
    79  		array := garray.New(true)
    80  		job := timer.Add(1000*time.Millisecond, func() {
    81  			array.Append(1)
    82  		})
    83  		job.Job()()
    84  		t.Assert(array.Len(), 1)
    85  	})
    86  }