From 155df18dc3493eb43c65b2b7f9a3f6a96217927f Mon Sep 17 00:00:00 2001 From: imsunv Date: Wed, 19 Oct 2022 10:51:37 +0800 Subject: [PATCH] add test case16 for copy rete limiter --- all_test.go | 27 +++++++++++++++++++++++++++ ratelimited_reader.go | 2 +- test/data/case16/README.md | 5 +++++ 3 files changed, 33 insertions(+), 1 deletion(-) create mode 100644 test/data/case16/README.md diff --git a/all_test.go b/all_test.go index b77a182..b1d7fb9 100644 --- a/all_test.go +++ b/all_test.go @@ -3,10 +3,12 @@ package copy import ( "errors" "io/ioutil" + "math" "os" "runtime" "strings" "testing" + "time" . "github.com/otiai10/mint" ) @@ -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) +} diff --git a/ratelimited_reader.go b/ratelimited_reader.go index 36be9ad..f99b690 100644 --- a/ratelimited_reader.go +++ b/ratelimited_reader.go @@ -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, diff --git a/test/data/case16/README.md b/test/data/case16/README.md new file mode 100644 index 0000000..29064a8 --- /dev/null +++ b/test/data/case16/README.md @@ -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. \ No newline at end of file