github.com/zhongdalu/gf@v1.0.0/g/os/gtimer/gtimer_z_unit_2_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  // Entry Operations
     8  
     9  package gtimer_test
    10  
    11  import (
    12  	"github.com/zhongdalu/gf/g/container/garray"
    13  	"github.com/zhongdalu/gf/g/os/gtimer"
    14  	"github.com/zhongdalu/gf/g/test/gtest"
    15  	"testing"
    16  	"time"
    17  )
    18  
    19  func TestEntry_Start_Stop_Close(t *testing.T) {
    20  	timer := New()
    21  	array := garray.New()
    22  	entry := timer.Add(200*time.Millisecond, func() {
    23  		array.Append(1)
    24  	})
    25  	time.Sleep(250 * time.Millisecond)
    26  	gtest.Assert(array.Len(), 1)
    27  	entry.Stop()
    28  	time.Sleep(250 * time.Millisecond)
    29  	gtest.Assert(array.Len(), 1)
    30  	entry.Start()
    31  	time.Sleep(250 * time.Millisecond)
    32  	gtest.Assert(array.Len(), 2)
    33  	entry.Close()
    34  	time.Sleep(250 * time.Millisecond)
    35  	gtest.Assert(array.Len(), 2)
    36  
    37  	gtest.Assert(entry.Status(), gtimer.STATUS_CLOSED)
    38  }
    39  
    40  func TestEntry_Singleton(t *testing.T) {
    41  	timer := New()
    42  	array := garray.New()
    43  	entry := timer.Add(200*time.Millisecond, func() {
    44  		array.Append(1)
    45  		time.Sleep(10 * time.Second)
    46  	})
    47  	gtest.Assert(entry.IsSingleton(), false)
    48  	entry.SetSingleton(true)
    49  	gtest.Assert(entry.IsSingleton(), true)
    50  	time.Sleep(250 * time.Millisecond)
    51  	gtest.Assert(array.Len(), 1)
    52  
    53  	time.Sleep(250 * time.Millisecond)
    54  	gtest.Assert(array.Len(), 1)
    55  }
    56  
    57  func TestEntry_SetTimes(t *testing.T) {
    58  	timer := New()
    59  	array := garray.New()
    60  	entry := timer.Add(200*time.Millisecond, func() {
    61  		array.Append(1)
    62  	})
    63  	entry.SetTimes(2)
    64  	time.Sleep(1200 * time.Millisecond)
    65  	gtest.Assert(array.Len(), 2)
    66  }
    67  
    68  func TestEntry_Run(t *testing.T) {
    69  	timer := New()
    70  	array := garray.New()
    71  	entry := timer.Add(1000*time.Millisecond, func() {
    72  		array.Append(1)
    73  	})
    74  	entry.Run()
    75  	gtest.Assert(array.Len(), 1)
    76  }