github.com/TeaOSLab/EdgeNode@v1.3.8/internal/utils/fs/limiter_test.go (about)

     1  // Copyright 2024 GoEdge CDN goedge.cdn@gmail.com. All rights reserved. Official site: https://goedge.cn .
     2  
     3  package fsutils_test
     4  
     5  import (
     6  	fsutils "github.com/TeaOSLab/EdgeNode/internal/utils/fs"
     7  	"github.com/TeaOSLab/EdgeNode/internal/utils/testutils"
     8  	"github.com/iwind/TeaGo/assert"
     9  	"math/rand"
    10  	"sync"
    11  	"testing"
    12  	"time"
    13  )
    14  
    15  func TestLimiter_SetThreads(t *testing.T) {
    16  	var limiter = fsutils.NewLimiter(4)
    17  
    18  	var concurrent = 1024
    19  
    20  	var wg = sync.WaitGroup{}
    21  	wg.Add(concurrent)
    22  
    23  	for i := 0; i < concurrent; i++ {
    24  		go func() {
    25  			defer wg.Done()
    26  
    27  			limiter.SetThreads(rand.Int() % 128)
    28  			limiter.TryAck()
    29  		}()
    30  	}
    31  
    32  	wg.Wait()
    33  }
    34  
    35  func TestLimiter_Ack(t *testing.T) {
    36  	var a = assert.NewAssertion(t)
    37  
    38  	{
    39  		var limiter = fsutils.NewLimiter(4)
    40  		a.IsTrue(limiter.FreeThreads() == 4)
    41  		limiter.Ack()
    42  		a.IsTrue(limiter.FreeThreads() == 3)
    43  		limiter.Ack()
    44  		a.IsTrue(limiter.FreeThreads() == 2)
    45  		limiter.Release()
    46  		a.IsTrue(limiter.FreeThreads() == 3)
    47  		limiter.Release()
    48  		a.IsTrue(limiter.FreeThreads() == 4)
    49  	}
    50  }
    51  
    52  func TestLimiter_TryAck(t *testing.T) {
    53  	var a = assert.NewAssertion(t)
    54  
    55  	{
    56  		var limiter = fsutils.NewLimiter(4)
    57  		var count = limiter.FreeThreads()
    58  		a.IsTrue(count == 4)
    59  		for i := 0; i < count; i++ {
    60  			limiter.Ack()
    61  		}
    62  		a.IsTrue(limiter.FreeThreads() == 0)
    63  		a.IsFalse(limiter.TryAck())
    64  		a.IsTrue(limiter.FreeThreads() == 0)
    65  	}
    66  
    67  	{
    68  		var limiter = fsutils.NewLimiter(4)
    69  		var count = limiter.FreeThreads()
    70  		a.IsTrue(count == 4)
    71  		for i := 0; i < count-1; i++ {
    72  			limiter.Ack()
    73  		}
    74  		a.IsTrue(limiter.FreeThreads() == 1)
    75  		a.IsTrue(limiter.TryAck())
    76  		a.IsTrue(limiter.FreeThreads() == 0)
    77  	}
    78  }
    79  
    80  func TestLimiter_TryAck2(t *testing.T) {
    81  	if !testutils.IsSingleTesting() {
    82  		return
    83  	}
    84  
    85  	var a = assert.NewAssertion(t)
    86  
    87  	{
    88  		var limiter = fsutils.NewLimiter(4)
    89  		var count = limiter.FreeThreads()
    90  		a.IsTrue(count == 4)
    91  		for i := 0; i < count-1; i++ {
    92  			limiter.Ack()
    93  		}
    94  		a.IsTrue(limiter.FreeThreads() == 1)
    95  		a.IsTrue(limiter.TryAck())
    96  		a.IsFalse(limiter.TryAck())
    97  		a.IsFalse(limiter.TryAck())
    98  
    99  		limiter.Release()
   100  		a.IsTrue(limiter.TryAck())
   101  	}
   102  }
   103  
   104  func TestLimiter_Timout(t *testing.T) {
   105  	var timeout = time.NewTimer(100 * time.Millisecond)
   106  
   107  	var r = make(chan bool, 1)
   108  	r <- true
   109  
   110  	var before = time.Now()
   111  	select {
   112  	case <-r:
   113  	case <-timeout.C:
   114  	}
   115  	t.Log(time.Since(before).Seconds()*1000, "ms")
   116  
   117  	timeout.Stop()
   118  
   119  	before = time.Now()
   120  	timeout.Reset(100 * time.Millisecond)
   121  	<-timeout.C
   122  	t.Log(time.Since(before).Seconds()*1000, "ms")
   123  }