Skip to content

Commit

Permalink
add test case16 for copy rete limiter
Browse files Browse the repository at this point in the history
  • Loading branch information
imsunv committed Oct 19, 2022
1 parent 432adff commit 155df18
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 1 deletion.
27 changes: 27 additions & 0 deletions all_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ package copy
import (
"errors"
"io/ioutil"
"math"
"os"
"runtime"
"strings"
"testing"
"time"

. "github.com/otiai10/mint"
)
Expand Down Expand Up @@ -326,3 +328,28 @@ func TestOptions_PreserveOwner(t *testing.T) {
err := Copy("test/data/case13", "test/data.copy/case13", opt)
Expect(t, err).ToBe(nil)
}

func TestOptions_CopyRateLimit(t *testing.T) {
opt := Options{CopyRateLimit: 50} // 50 KB/s
size := int64(100 * 1024) // 100 KB
costSec := int64(2) // 2 seconds

file, err := os.Create("test/data/case16/large.file")
if err != nil {
t.Errorf("failed to create test file: %v", err)
return
}

if err := file.Truncate(size); err != nil {
t.Errorf("failed to truncate test file: %v", err)
t.SkipNow()
return
}

start := time.Now()
err = Copy("test/data/case16", "test/data.copy/case16", opt)
copyCost := time.Since(start)
Expect(t, err).ToBe(nil)
t.Log("copy cost", copyCost)
Expect(t, int64(math.Floor(copyCost.Seconds()+0.5)) == costSec).ToBe(true)
}
2 changes: 1 addition & 1 deletion ratelimited_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ type RateLimitedReader struct {
}

// NewRateLimitedReader
// n means the number of Kb to be read per second
// n means the number of KB to be read per second
func NewRateLimitedReader(src io.Reader, n int64) io.Reader {
return &RateLimitedReader{
src: src,
Expand Down
5 changes: 5 additions & 0 deletions test/data/case16/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
So if you wanted to limit copy rate with a KB/second value, you could add options like this:
```go
opt.CopyRateLimit = 100
```
The default value is 0, which means no limit.

0 comments on commit 155df18

Please sign in to comment.