github.com/dubbogo/gost@v1.14.0/time/ticker_test.go (about)

     1  /*
     2   * Licensed to the Apache Software Foundation (ASF) under one or more
     3   * contributor license agreements.  See the NOTICE file distributed with
     4   * this work for additional information regarding copyright ownership.
     5   * The ASF licenses this file to You under the Apache License, Version 2.0
     6   * (the "License"); you may not use this file except in compliance with
     7   * the License.  You may obtain a copy of the License at
     8   *
     9   *     http://www.apache.org/licenses/LICENSE-2.0
    10   *
    11   * Unless required by applicable law or agreed to in writing, software
    12   * distributed under the License is distributed on an "AS IS" BASIS,
    13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14   * See the License for the specific language governing permissions and
    15   * limitations under the License.
    16   */
    17  
    18  // Package gxtime encapsulates some golang.time functions
    19  package gxtime
    20  
    21  import (
    22  	"testing"
    23  	"time"
    24  )
    25  
    26  import (
    27  	gxlog "github.com/dubbogo/gost/log"
    28  )
    29  
    30  func TestTickFunc(t *testing.T) {
    31  
    32  	// num     int
    33  	var cw CountWatch // xassert *assert.Assertions
    34  
    35  	InitDefaultTimerWheel()
    36  
    37  	f := func() {
    38  		gxlog.CInfo("timer costs:%dms", cw.Count()/1e6)
    39  	}
    40  
    41  	// num = 3
    42  	// xassert = assert.New(t)
    43  	cw.Start()
    44  	TickFunc(TimeSecondDuration(0.5), f)
    45  	TickFunc(TimeSecondDuration(1.3), f)
    46  	TickFunc(TimeSecondDuration(6.5), f)
    47  	time.Sleep(6e9)
    48  	// xassert.Equal(defaultTimerWheel.TimerNumber(), num, "") // just equal in this ut
    49  }
    50  
    51  func TestTicker_Reset(t *testing.T) {
    52  	//var (
    53  	//	ticker *Ticker
    54  	//	wg     sync.WaitGroup
    55  	//	cw     CountWatch
    56  	//	xassert *assert.Assertions
    57  	//)
    58  	//
    59  	//Init()
    60  	//
    61  	//f := func() {
    62  	//	defer wg.Done()
    63  	//	gxlog.CInfo("timer costs:%dms", cw.Count()/1e6)
    64  	//	gxlog.CInfo("in timer func, timer number:%d", defaultTimerWheel.TimerNumber())
    65  	//}
    66  	//
    67  	//xassert = assert.New(t)
    68  	//wg.Add(1)
    69  	//cw.Start()
    70  	//ticker = TickFunc(TimeSecondDuration(1.5), f)
    71  	//ticker.Reset(TimeSecondDuration(3.5))
    72  	//time.Sleep(TimeSecondDuration(0.001))
    73  	//xassert.Equal(defaultTimerWheel.TimerNumber(), 1, "") // just equal on this ut
    74  	//wg.Wait()
    75  }
    76  
    77  func TestTicker_Stop(t *testing.T) {
    78  	var (
    79  		ticker *Ticker
    80  		cw     CountWatch
    81  		// xassert assert.Assertions
    82  	)
    83  
    84  	InitDefaultTimerWheel()
    85  
    86  	f := func() {
    87  		gxlog.CInfo("timer costs:%dms", cw.Count()/1e6)
    88  	}
    89  
    90  	cw.Start()
    91  	ticker = TickFunc(TimeSecondDuration(4.5), f)
    92  	// 添加是异步进行的,所以sleep一段时间再去检测timer number
    93  	time.Sleep(TimeSecondDuration(0.001))
    94  	// timerNumber := defaultTimerWheel.TimerNumber()
    95  	// xassert.Equal(timerNumber, 1, "")
    96  	time.Sleep(TimeSecondDuration(5))
    97  	ticker.Stop()
    98  	// 删除是异步进行的,所以sleep一段时间再去检测timer number
    99  	// time.Sleep(TimeSecondDuration(0.001))
   100  	// timerNumber = defaultTimerWheel.TimerNumber()
   101  	// xassert.Equal(timerNumber, 0, "")
   102  }