github.com/gogf/gf/v2@v2.7.4/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  	"context"
    13  	"testing"
    14  	"time"
    15  
    16  	"github.com/gogf/gf/v2/container/garray"
    17  	"github.com/gogf/gf/v2/os/gtimer"
    18  	"github.com/gogf/gf/v2/test/gtest"
    19  )
    20  
    21  func TestJob_Start_Stop_Close(t *testing.T) {
    22  	gtest.C(t, func(t *gtest.T) {
    23  		timer := gtimer.New()
    24  		array := garray.New(true)
    25  		job := timer.Add(ctx, 200*time.Millisecond, func(ctx context.Context) {
    26  			array.Append(1)
    27  		})
    28  		time.Sleep(250 * time.Millisecond)
    29  		t.Assert(array.Len(), 1)
    30  		job.Stop()
    31  		time.Sleep(250 * time.Millisecond)
    32  		t.Assert(array.Len(), 1)
    33  		job.Start()
    34  		time.Sleep(250 * time.Millisecond)
    35  		t.Assert(array.Len(), 2)
    36  		job.Close()
    37  		time.Sleep(250 * time.Millisecond)
    38  		t.Assert(array.Len(), 2)
    39  
    40  		t.Assert(job.Status(), gtimer.StatusClosed)
    41  	})
    42  }
    43  
    44  func TestJob_Singleton(t *testing.T) {
    45  	gtest.C(t, func(t *gtest.T) {
    46  		timer := gtimer.New()
    47  		array := garray.New(true)
    48  		job := timer.Add(ctx, 200*time.Millisecond, func(ctx context.Context) {
    49  			array.Append(1)
    50  			time.Sleep(10 * time.Second)
    51  		})
    52  		t.Assert(job.IsSingleton(), false)
    53  		job.SetSingleton(true)
    54  		t.Assert(job.IsSingleton(), true)
    55  		time.Sleep(250 * time.Millisecond)
    56  		t.Assert(array.Len(), 1)
    57  
    58  		time.Sleep(250 * time.Millisecond)
    59  		t.Assert(array.Len(), 1)
    60  	})
    61  }
    62  
    63  func TestJob_SingletonQuick(t *testing.T) {
    64  	gtest.C(t, func(t *gtest.T) {
    65  		timer := gtimer.New(gtimer.TimerOptions{
    66  			Quick: true,
    67  		})
    68  		array := garray.New(true)
    69  		job := timer.Add(ctx, 5*time.Second, func(ctx context.Context) {
    70  			array.Append(1)
    71  			time.Sleep(10 * time.Second)
    72  		})
    73  		t.Assert(job.IsSingleton(), false)
    74  		job.SetSingleton(true)
    75  		t.Assert(job.IsSingleton(), true)
    76  		time.Sleep(250 * time.Millisecond)
    77  		t.Assert(array.Len(), 1)
    78  
    79  		time.Sleep(250 * time.Millisecond)
    80  		t.Assert(array.Len(), 1)
    81  	})
    82  }
    83  
    84  func TestJob_SetTimes(t *testing.T) {
    85  	gtest.C(t, func(t *gtest.T) {
    86  		timer := gtimer.New()
    87  		array := garray.New(true)
    88  		job := timer.Add(ctx, 200*time.Millisecond, func(ctx context.Context) {
    89  			array.Append(1)
    90  		})
    91  		job.SetTimes(2)
    92  		//job.IsSingleton()
    93  		time.Sleep(1200 * time.Millisecond)
    94  		t.Assert(array.Len(), 2)
    95  	})
    96  }
    97  
    98  func TestJob_Run(t *testing.T) {
    99  	gtest.C(t, func(t *gtest.T) {
   100  		timer := gtimer.New()
   101  		array := garray.New(true)
   102  		job := timer.Add(ctx, 1000*time.Millisecond, func(ctx context.Context) {
   103  			array.Append(1)
   104  		})
   105  		job.Job()(ctx)
   106  		t.Assert(array.Len(), 1)
   107  	})
   108  }