1
0
Fork 0
mirror of https://github.com/Luzifer/go_helpers.git synced 2024-12-25 21:41:20 +00:00
go_helpers/io/throttledReader_test.go
Knut Ahlers 7179a1859b
Add ThrottledReader as io-helper
Signed-off-by: Knut Ahlers <knut@ahlers.me>
2024-03-06 17:16:34 +01:00

33 lines
886 B
Go

package io
import (
"crypto/rand"
"io"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestThrottledReader(t *testing.T) {
var (
testSize = 10 * 1024 * 1024 // 10 Mi
testLimit = 20 * 1024 * 1024 // 20 Mi/s
// 20Mi/s on 10M = 500ms exec time
expectedTimeMillisecs = float64(testSize) / float64(testLimit) * 1000
tolerance = 50 // Millisecs
)
lr := io.LimitReader(rand.Reader, int64(testSize))
var tr io.Reader = NewThrottledReader(lr, float64(testLimit))
start := time.Now()
n, err := io.Copy(io.Discard, tr)
require.NoError(t, err)
assert.Equal(t, int64(testSize), n)
assert.Greater(t, time.Since(start)/time.Millisecond, time.Duration(expectedTimeMillisecs-float64(tolerance)))
assert.Less(t, time.Since(start)/time.Millisecond, time.Duration(expectedTimeMillisecs+float64(tolerance)))
}