2019-06-24 12:18:52 +00:00
|
|
|
package accounting
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"testing"
|
|
|
|
|
2019-07-28 17:47:38 +00:00
|
|
|
"github.com/rclone/rclone/fs/rc"
|
2019-06-24 12:18:52 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"golang.org/x/time/rate"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestRcBwLimit(t *testing.T) {
|
|
|
|
call := rc.Calls.Get("core/bwlimit")
|
|
|
|
assert.NotNil(t, call)
|
|
|
|
|
|
|
|
// Set
|
|
|
|
in := rc.Params{
|
|
|
|
"rate": "1M",
|
|
|
|
}
|
|
|
|
out, err := call.Fn(context.Background(), in)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, rc.Params{
|
2020-12-07 16:19:20 +00:00
|
|
|
"bytesPerSecond": int64(1048576),
|
|
|
|
"bytesPerSecondTx": int64(1048576),
|
|
|
|
"bytesPerSecondRx": int64(1048576),
|
2021-03-02 19:11:57 +00:00
|
|
|
"rate": "1Mi",
|
2019-06-24 12:18:52 +00:00
|
|
|
}, out)
|
2020-07-04 16:20:54 +00:00
|
|
|
assert.Equal(t, rate.Limit(1048576), TokenBucket.curr[0].Limit())
|
2019-06-24 12:18:52 +00:00
|
|
|
|
|
|
|
// Query
|
|
|
|
in = rc.Params{}
|
|
|
|
out, err = call.Fn(context.Background(), in)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, rc.Params{
|
2020-12-07 16:19:20 +00:00
|
|
|
"bytesPerSecond": int64(1048576),
|
|
|
|
"bytesPerSecondTx": int64(1048576),
|
|
|
|
"bytesPerSecondRx": int64(1048576),
|
2021-03-02 19:11:57 +00:00
|
|
|
"rate": "1Mi",
|
2020-12-07 16:19:20 +00:00
|
|
|
}, out)
|
|
|
|
|
|
|
|
// Set
|
|
|
|
in = rc.Params{
|
|
|
|
"rate": "10M:1M",
|
|
|
|
}
|
|
|
|
out, err = call.Fn(context.Background(), in)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, rc.Params{
|
|
|
|
"bytesPerSecond": int64(10485760),
|
|
|
|
"bytesPerSecondTx": int64(10485760),
|
|
|
|
"bytesPerSecondRx": int64(1048576),
|
2021-03-02 19:11:57 +00:00
|
|
|
"rate": "10Mi:1Mi",
|
2020-12-07 16:19:20 +00:00
|
|
|
}, out)
|
|
|
|
assert.Equal(t, rate.Limit(10485760), TokenBucket.curr[0].Limit())
|
|
|
|
|
|
|
|
// Query
|
|
|
|
in = rc.Params{}
|
|
|
|
out, err = call.Fn(context.Background(), in)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, rc.Params{
|
|
|
|
"bytesPerSecond": int64(10485760),
|
|
|
|
"bytesPerSecondTx": int64(10485760),
|
|
|
|
"bytesPerSecondRx": int64(1048576),
|
2021-03-02 19:11:57 +00:00
|
|
|
"rate": "10Mi:1Mi",
|
2019-06-24 12:18:52 +00:00
|
|
|
}, out)
|
|
|
|
|
|
|
|
// Reset
|
|
|
|
in = rc.Params{
|
|
|
|
"rate": "off",
|
|
|
|
}
|
|
|
|
out, err = call.Fn(context.Background(), in)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, rc.Params{
|
2020-12-07 16:19:20 +00:00
|
|
|
"bytesPerSecond": int64(-1),
|
|
|
|
"bytesPerSecondTx": int64(-1),
|
|
|
|
"bytesPerSecondRx": int64(-1),
|
|
|
|
"rate": "off",
|
2019-06-24 12:18:52 +00:00
|
|
|
}, out)
|
2020-07-04 16:20:54 +00:00
|
|
|
assert.Nil(t, TokenBucket.curr[0])
|
2019-06-24 12:18:52 +00:00
|
|
|
|
|
|
|
// Query
|
|
|
|
in = rc.Params{}
|
|
|
|
out, err = call.Fn(context.Background(), in)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, rc.Params{
|
2020-12-07 16:19:20 +00:00
|
|
|
"bytesPerSecond": int64(-1),
|
|
|
|
"bytesPerSecondTx": int64(-1),
|
|
|
|
"bytesPerSecondRx": int64(-1),
|
|
|
|
"rate": "off",
|
2019-06-24 12:18:52 +00:00
|
|
|
}, out)
|
|
|
|
|
|
|
|
}
|