github.com/divyam234/rclone@v1.64.1/fs/accounting/tpslimit_test.go (about)

     1  package accounting
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  	"time"
     7  
     8  	"github.com/divyam234/rclone/fs"
     9  	"github.com/stretchr/testify/assert"
    10  )
    11  
    12  func TestLimitTPS(t *testing.T) {
    13  	timeTransactions := func(n int, minTime, maxTime time.Duration) {
    14  		start := time.Now()
    15  		for i := 0; i < n; i++ {
    16  			LimitTPS(context.Background())
    17  		}
    18  		dt := time.Since(start)
    19  		assert.True(t, dt >= minTime && dt <= maxTime, "Expecting time between %v and %v, got %v", minTime, maxTime, dt)
    20  	}
    21  
    22  	t.Run("Off", func(t *testing.T) {
    23  		assert.Nil(t, tpsBucket)
    24  		timeTransactions(100, 0*time.Millisecond, 100*time.Millisecond)
    25  	})
    26  
    27  	t.Run("On", func(t *testing.T) {
    28  		ctx, ci := fs.AddConfig(context.Background())
    29  		ci.TPSLimit = 100.0
    30  		ci.TPSLimitBurst = 0
    31  		StartLimitTPS(ctx)
    32  		assert.NotNil(t, tpsBucket)
    33  		defer func() {
    34  			tpsBucket = nil
    35  		}()
    36  
    37  		timeTransactions(100, 900*time.Millisecond, 5000*time.Millisecond)
    38  	})
    39  }