github.com/TeaOSLab/EdgeNode@v1.3.8/internal/utils/writers/writer_rate_limit_test.go (about)

     1  // Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
     2  
     3  package writers
     4  
     5  import (
     6  	"sync"
     7  	"testing"
     8  	"time"
     9  )
    10  
    11  func TestSleep(t *testing.T) {
    12  	var count = 2000
    13  	var wg = sync.WaitGroup{}
    14  	wg.Add(count)
    15  	var before = time.Now()
    16  	for i := 0; i < count; i++ {
    17  		go func() {
    18  			defer wg.Done()
    19  			time.Sleep(1 * time.Second)
    20  		}()
    21  	}
    22  	wg.Wait()
    23  	t.Log(time.Since(before).Seconds()*1000, "ms")
    24  }
    25  
    26  func TestTimeout(t *testing.T) {
    27  	var count = 2000
    28  	var wg = sync.WaitGroup{}
    29  	wg.Add(count)
    30  	var before = time.Now()
    31  	for i := 0; i < count; i++ {
    32  		go func() {
    33  			defer wg.Done()
    34  
    35  			var timeout = time.NewTimer(1 * time.Second)
    36  			<-timeout.C
    37  		}()
    38  	}
    39  	wg.Wait()
    40  	t.Log(time.Since(before).Seconds()*1000, "ms")
    41  }