github.com/dubbogo/gost@v1.14.0/time/sleep_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  	"fmt"
    23  	"sync"
    24  	"testing"
    25  	"time"
    26  )
    27  
    28  import (
    29  	"github.com/stretchr/testify/assert"
    30  )
    31  
    32  import (
    33  	"github.com/dubbogo/gost/log"
    34  )
    35  
    36  func TestNewTimerWheel(t *testing.T) {
    37  	var (
    38  		index int
    39  		wheel *TimerWheel
    40  		cw    CountWatch
    41  	)
    42  
    43  	wheel = NewTimerWheel()
    44  	defer func() {
    45  		fmt.Println("timer costs:", cw.Count()/1e6, "ms")
    46  		wheel.Stop()
    47  	}()
    48  
    49  	cw.Start()
    50  	for {
    51  		select {
    52  		case <-wheel.After(TimeMillisecondDuration(100)):
    53  			index++
    54  			if index >= 10 {
    55  				return
    56  			}
    57  		}
    58  	}
    59  }
    60  
    61  func TestAfter(t *testing.T) {
    62  	var (
    63  		wheel *TimerWheel
    64  		wg    sync.WaitGroup
    65  	)
    66  	wheel = NewTimerWheel()
    67  
    68  	// Init()
    69  
    70  	defer wheel.Stop()
    71  
    72  	f := func(d time.Duration, num int) {
    73  		var (
    74  			cw    CountWatch
    75  			index int
    76  		)
    77  
    78  		defer func() {
    79  			gxlog.CInfo("duration %d loop %d, timer costs:%dms", d, num, cw.Count()/1e6)
    80  			gxlog.CInfo("in timer func, timer number:%d", wheel.TimerNumber())
    81  			wg.Done()
    82  		}()
    83  
    84  		cw.Start()
    85  		for {
    86  			select {
    87  			case <-wheel.After(d):
    88  				index++
    89  				if index >= num {
    90  					return
    91  				}
    92  			}
    93  		}
    94  	}
    95  
    96  	wg.Add(6)
    97  	go f(TimeSecondDuration(1.5), 15)
    98  	go f(TimeSecondDuration(2.510), 10)
    99  	go f(TimeSecondDuration(1.5), 40)
   100  	go f(TimeSecondDuration(0.15), 200)
   101  	go f(TimeSecondDuration(3), 20)
   102  	go f(TimeSecondDuration(63), 1)
   103  
   104  	time.Sleep(TimeSecondDuration(0.01))
   105  	assert.Equalf(t, 6, wheel.TimerNumber(), "")
   106  	wg.Wait()
   107  }
   108  
   109  func TestAfterFunc(t *testing.T) {
   110  	var (
   111  		wg sync.WaitGroup
   112  		cw CountWatch
   113  	)
   114  
   115  	InitDefaultTimerWheel()
   116  
   117  	f := func() {
   118  		defer wg.Done()
   119  		gxlog.CInfo("timer costs:%dms", cw.Count()/1e6)
   120  		gxlog.CInfo("in timer func, timer number:%d", defaultTimerWheel.TimerNumber())
   121  	}
   122  
   123  	wg.Add(3)
   124  	cw.Start()
   125  	AfterFunc(TimeSecondDuration(0.5), f)
   126  	AfterFunc(TimeSecondDuration(1.5), f)
   127  	AfterFunc(TimeSecondDuration(61.5), f)
   128  
   129  	time.Sleep(TimeSecondDuration(0.01))
   130  	assert.Equalf(t, 3, defaultTimerWheel.TimerNumber(), "")
   131  	wg.Wait()
   132  }
   133  
   134  func TestTimer_Reset(t *testing.T) {
   135  	var (
   136  		timer *Timer
   137  		wg    sync.WaitGroup
   138  		cw    CountWatch
   139  	)
   140  
   141  	InitDefaultTimerWheel()
   142  
   143  	f := func() {
   144  		defer wg.Done()
   145  		gxlog.CInfo("timer costs:%dms", cw.Count()/1e6)
   146  		gxlog.CInfo("in timer func, timer number:%d", defaultTimerWheel.TimerNumber())
   147  	}
   148  
   149  	wg.Add(1)
   150  	cw.Start()
   151  	timer = AfterFunc(TimeSecondDuration(1.5), f)
   152  	timer.Reset(TimeSecondDuration(3.5))
   153  
   154  	time.Sleep(TimeSecondDuration(0.01))
   155  	assert.Equalf(t, 1, defaultTimerWheel.TimerNumber(), "")
   156  	wg.Wait()
   157  }
   158  
   159  func TestTimer_Stop(t *testing.T) {
   160  	var (
   161  		timer *Timer
   162  		cw    CountWatch
   163  	)
   164  
   165  	InitDefaultTimerWheel()
   166  
   167  	f := func() {
   168  		gxlog.CInfo("timer costs:%dms", cw.Count()/1e6)
   169  	}
   170  
   171  	timer = AfterFunc(TimeSecondDuration(4.5), f)
   172  	// 添加是异步进行的,所以sleep一段时间再去检测timer number
   173  	time.Sleep(1e9)
   174  	assert.Equalf(t, 1, defaultTimerWheel.TimerNumber(), "before stop")
   175  	timer.Stop()
   176  	// 删除是异步进行的,所以sleep一段时间再去检测timer number
   177  	time.Sleep(1e9)
   178  
   179  	time.Sleep(TimeSecondDuration(0.01))
   180  	// assert.Equalf(t, 0, defaultTimerWheel.TimerNumber(), "after stop")
   181  	time.Sleep(3e9)
   182  }