mirror of
https://github.com/Luzifer/go_helpers.git
synced 2024-12-26 14:01:20 +00:00
34 lines
886 B
Go
34 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)))
|
||
|
}
|