github.com/wangyougui/gf/v2@v2.6.5/os/gtimer/gtimer_z_unit_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/wangyougui/gf.
     6  
     7  // Package functions
     8  
     9  package gtimer_test
    10  
    11  import (
    12  	"context"
    13  	"testing"
    14  	"time"
    15  
    16  	"github.com/wangyougui/gf/v2/container/garray"
    17  	"github.com/wangyougui/gf/v2/os/gtimer"
    18  	"github.com/wangyougui/gf/v2/test/gtest"
    19  )
    20  
    21  var (
    22  	ctx = context.TODO()
    23  )
    24  
    25  func TestSetTimeout(t *testing.T) {
    26  	gtest.C(t, func(t *gtest.T) {
    27  		array := garray.New(true)
    28  		gtimer.SetTimeout(ctx, 200*time.Millisecond, func(ctx context.Context) {
    29  			array.Append(1)
    30  		})
    31  		time.Sleep(1000 * time.Millisecond)
    32  		t.Assert(array.Len(), 1)
    33  	})
    34  }
    35  
    36  func TestSetInterval(t *testing.T) {
    37  	gtest.C(t, func(t *gtest.T) {
    38  		array := garray.New(true)
    39  		gtimer.SetInterval(ctx, 300*time.Millisecond, func(ctx context.Context) {
    40  			array.Append(1)
    41  		})
    42  		time.Sleep(1000 * time.Millisecond)
    43  		t.Assert(array.Len(), 3)
    44  	})
    45  }
    46  
    47  func TestAddEntry(t *testing.T) {
    48  	gtest.C(t, func(t *gtest.T) {
    49  		array := garray.New(true)
    50  		gtimer.AddEntry(ctx, 200*time.Millisecond, func(ctx context.Context) {
    51  			array.Append(1)
    52  		}, false, 2, gtimer.StatusReady)
    53  		time.Sleep(1100 * time.Millisecond)
    54  		t.Assert(array.Len(), 2)
    55  	})
    56  }
    57  
    58  func TestAddSingleton(t *testing.T) {
    59  	gtest.C(t, func(t *gtest.T) {
    60  		array := garray.New(true)
    61  		gtimer.AddSingleton(ctx, 200*time.Millisecond, func(ctx context.Context) {
    62  			array.Append(1)
    63  			time.Sleep(10000 * time.Millisecond)
    64  		})
    65  		time.Sleep(1100 * time.Millisecond)
    66  		t.Assert(array.Len(), 1)
    67  	})
    68  }
    69  
    70  func TestAddTimes(t *testing.T) {
    71  	gtest.C(t, func(t *gtest.T) {
    72  		array := garray.New(true)
    73  		gtimer.AddTimes(ctx, 200*time.Millisecond, 2, func(ctx context.Context) {
    74  			array.Append(1)
    75  		})
    76  		time.Sleep(1000 * time.Millisecond)
    77  		t.Assert(array.Len(), 2)
    78  	})
    79  }
    80  
    81  func TestDelayAdd(t *testing.T) {
    82  	gtest.C(t, func(t *gtest.T) {
    83  		array := garray.New(true)
    84  		gtimer.DelayAdd(ctx, 500*time.Millisecond, 500*time.Millisecond, func(ctx context.Context) {
    85  			array.Append(1)
    86  		})
    87  		time.Sleep(600 * time.Millisecond)
    88  		t.Assert(array.Len(), 0)
    89  		time.Sleep(600 * time.Millisecond)
    90  		t.Assert(array.Len(), 1)
    91  	})
    92  }
    93  
    94  func TestDelayAddEntry(t *testing.T) {
    95  	gtest.C(t, func(t *gtest.T) {
    96  		array := garray.New(true)
    97  		gtimer.DelayAddEntry(ctx, 500*time.Millisecond, 500*time.Millisecond, func(ctx context.Context) {
    98  			array.Append(1)
    99  		}, false, 2, gtimer.StatusReady)
   100  		time.Sleep(500 * time.Millisecond)
   101  		t.Assert(array.Len(), 0)
   102  		time.Sleep(2000 * time.Millisecond)
   103  		t.Assert(array.Len(), 2)
   104  	})
   105  }
   106  
   107  func TestDelayAddSingleton(t *testing.T) {
   108  	gtest.C(t, func(t *gtest.T) {
   109  		array := garray.New(true)
   110  		gtimer.DelayAddSingleton(ctx, 500*time.Millisecond, 500*time.Millisecond, func(ctx context.Context) {
   111  			array.Append(1)
   112  			time.Sleep(10000 * time.Millisecond)
   113  		})
   114  		time.Sleep(300 * time.Millisecond)
   115  		t.Assert(array.Len(), 0)
   116  		time.Sleep(1000 * time.Millisecond)
   117  		t.Assert(array.Len(), 1)
   118  	})
   119  }
   120  
   121  func TestDelayAddOnce(t *testing.T) {
   122  	gtest.C(t, func(t *gtest.T) {
   123  		array := garray.New(true)
   124  		gtimer.DelayAddOnce(ctx, 1000*time.Millisecond, 2000*time.Millisecond, func(ctx context.Context) {
   125  			array.Append(1)
   126  		})
   127  		time.Sleep(2000 * time.Millisecond)
   128  		t.Assert(array.Len(), 0)
   129  		time.Sleep(2000 * time.Millisecond)
   130  		t.Assert(array.Len(), 1)
   131  	})
   132  }
   133  
   134  func TestDelayAddTimes(t *testing.T) {
   135  	gtest.C(t, func(t *gtest.T) {
   136  		array := garray.New(true)
   137  		gtimer.DelayAddTimes(ctx, 500*time.Millisecond, 500*time.Millisecond, 2, func(ctx context.Context) {
   138  			array.Append(1)
   139  		})
   140  		time.Sleep(300 * time.Millisecond)
   141  		t.Assert(array.Len(), 0)
   142  		time.Sleep(1500 * time.Millisecond)
   143  		t.Assert(array.Len(), 2)
   144  	})
   145  }