github.com/weedge/lib@v0.0.0-20230424045628-a36dcc1d90e4/timingwheel/timingwheel_examples_test.go (about)

     1  package timingwheel
     2  
     3  import (
     4  	"fmt"
     5  	"time"
     6  )
     7  
     8  func Example_startTimer() {
     9  	tw := NewTimingWheel(time.Millisecond, 20)
    10  	tw.Start()
    11  	defer tw.Stop()
    12  
    13  	exitC := make(chan time.Time)
    14  	tw.AfterFunc(time.Second, func() {
    15  		fmt.Println("The timer fires")
    16  		exitC <- time.Now().UTC()
    17  	})
    18  
    19  	<-exitC
    20  
    21  	// Output:
    22  	// The timer fires
    23  }
    24  
    25  func Example_stopTimer() {
    26  	tw := NewTimingWheel(time.Millisecond, 20)
    27  	tw.Start()
    28  	defer tw.Stop()
    29  
    30  	t := tw.AfterFunc(time.Second, func() {
    31  		fmt.Println("The timer fires")
    32  	})
    33  
    34  	<-time.After(900 * time.Millisecond)
    35  	// Stop the timer before it fires
    36  	t.Stop()
    37  
    38  	// Output:
    39  	//
    40  }