github.com/kaydxh/golang@v0.0.131/go/time/rate/rate_test.go (about)

     1  /*
     2   *Copyright (c) 2022, kaydxh
     3   *
     4   *Permission is hereby granted, free of charge, to any person obtaining a copy
     5   *of this software and associated documentation files (the "Software"), to deal
     6   *in the Software without restriction, including without limitation the rights
     7   *to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     8   *copies of the Software, and to permit persons to whom the Software is
     9   *furnished to do so, subject to the following conditions:
    10   *
    11   *The above copyright notice and this permission notice shall be included in all
    12   *copies or substantial portions of the Software.
    13   *
    14   *THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    15   *IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    16   *FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    17   *AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    18   *LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    19   *OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
    20   *SOFTWARE.
    21   */
    22  package rate_test
    23  
    24  import (
    25  	"sync"
    26  	"sync/atomic"
    27  	"testing"
    28  	"time"
    29  
    30  	rate_ "github.com/kaydxh/golang/go/time/rate"
    31  	"gotest.tools/assert"
    32  )
    33  
    34  func TestLimitAllow(t *testing.T) {
    35  	// The test runs for a few seconds executing many requests and then checks
    36  	// that overall number of requests is reasonable.
    37  	const (
    38  		limit = 100
    39  		burst = 100
    40  	)
    41  
    42  	var (
    43  		numAll    int32 = 0
    44  		numOK     int32 = 0
    45  		numFailed int32 = 0
    46  	)
    47  
    48  	lim := rate_.NewLimiter(burst)
    49  
    50  	var wg sync.WaitGroup
    51  	f := func() {
    52  		if ok := lim.Allow(); ok {
    53  			defer lim.Put()
    54  			atomic.AddInt32(&numOK, 1)
    55  			time.Sleep(10 * time.Millisecond)
    56  
    57  		} else {
    58  			atomic.AddInt32(&numFailed, 1)
    59  			//			t.Logf("not Allowed, bursting: %v", lim.Bursting())
    60  		}
    61  
    62  		atomic.AddInt32(&numAll, 1)
    63  		wg.Done()
    64  	}
    65  
    66  	start := time.Now()
    67  	end := start.Add(5 * time.Second)
    68  
    69  	for time.Now().Before(end) {
    70  		wg.Add(1)
    71  		go f()
    72  	}
    73  	wg.Wait()
    74  
    75  	assert.Equal(t, numAll, numOK+numFailed)
    76  
    77  	// numOK should get very close to the number of requests allowed  50000.
    78  	t.Logf("%v request ==> %v requset Allowed, %v request Unallowed ", numAll, numOK, numFailed)
    79  
    80  }
    81  
    82  func TestLimitAllowFor(t *testing.T) {
    83  	// The test runs for a few seconds executing many requests and then checks
    84  	// that overall number of requests is reasonable.
    85  	const (
    86  		limit = 100
    87  		burst = 100
    88  	)
    89  
    90  	var (
    91  		numAll    int32 = 0
    92  		numOK     int32 = 0
    93  		numFailed int32 = 0
    94  	)
    95  
    96  	//timout > 5s, so must process 50001 times
    97  	timeout := 6 * time.Second
    98  
    99  	lim := rate_.NewLimiter(burst)
   100  
   101  	var wg sync.WaitGroup
   102  	f := func() {
   103  		if ok := lim.AllowFor(timeout); ok {
   104  			defer lim.Put()
   105  			atomic.AddInt32(&numOK, 1)
   106  			//about process 50000 times in 5s
   107  			time.Sleep(10 * time.Millisecond)
   108  
   109  		} else {
   110  			atomic.AddInt32(&numFailed, 1)
   111  			//			t.Logf("not Allowed, bursting: %v", lim.Bursting())
   112  		}
   113  
   114  		atomic.AddInt32(&numAll, 1)
   115  		wg.Done()
   116  	}
   117  
   118  	for i := 0; i < 50001; i++ {
   119  		wg.Add(1)
   120  		go f()
   121  	}
   122  	wg.Wait()
   123  
   124  	assert.Equal(t, numAll, numOK+numFailed)
   125  
   126  	// numOK should get very close to the number of requests allowed  50000.
   127  	t.Logf("%v request ==> %v requset Allowed, %v request Unallowed ", numAll, numOK, numFailed)
   128  
   129  }