From 1a319b80ad7885ddc4ac11d0e1e338ea75bea6d7 Mon Sep 17 00:00:00 2001 From: Harshavardhana Date: Wed, 20 May 2015 21:43:56 -0700 Subject: [PATCH] Implement cp tests -- update bar.Total with atomic.AddInt64 -- expose console mutex Lock/Unlock() -- send errors in cp over channels --- cmd-cp.go | 71 ++++++++++++---------- cp-bar.go | 6 +- cp_test.go | 132 +++++++++++++++++++++++++++++++++++++++++ pkg/console/console.go | 10 ++++ 4 files changed, 185 insertions(+), 34 deletions(-) create mode 100644 cp_test.go diff --git a/cmd-cp.go b/cmd-cp.go index af95015d..18690192 100644 --- a/cmd-cp.go +++ b/cmd-cp.go @@ -60,24 +60,58 @@ func args2URLs(args cli.Args) ([]string, error) { return URLs, nil } -func runCopyInRoutine(cpurls *cpURLs, bar *barSend, cpQueue chan bool, wg *sync.WaitGroup) { +func doCopyInRoutine(cpurls *cpURLs, bar *barSend, cpQueue chan bool, ch chan error, wg *sync.WaitGroup) { defer wg.Done() srcConfig, err := getHostConfig(cpurls.SourceContent.Name) if err != nil { - console.Errorln(iodine.ToError(err)) + ch <- err return } tgtConfig, err := getHostConfig(cpurls.TargetContent.Name) if err != nil { - console.Errorln(iodine.ToError(err)) + ch <- err return } if err := doCopy(cpurls.SourceContent.Name, srcConfig, cpurls.TargetContent.Name, tgtConfig, bar); err != nil { - console.Errorln(iodine.ToError(err)) + ch <- err } <-cpQueue } +func doCopyCmd(sourceURLs []string, targetURL string, bar barSend) <-chan error { + ch := make(chan error) + go func(sourceURLs []string, targetURL string, bar barSend, ch chan error) { + defer close(ch) + go func(sourceURLs []string, targetURL string) { + for cpURLs := range prepareCopyURLs(sourceURLs, targetURL) { + if cpURLs.Error != nil { + // no need to print errors here, any error here + // will be printed later during Copy() + continue + } + if !globalQuietFlag { + bar.Extend(cpURLs.SourceContent.Size) + } + } + }(sourceURLs, targetURL) + + cpQueue := make(chan bool, intMax(runtime.NumCPU()-1, 1)) + wg := new(sync.WaitGroup) + for cpURLs := range prepareCopyURLs(sourceURLs, targetURL) { + if cpURLs.Error != nil { + ch <- cpURLs.Error + continue + } + cpQueue <- true + wg.Add(1) + go doCopyInRoutine(cpURLs, &bar, cpQueue, ch, wg) + } + close(cpQueue) + wg.Wait() + }(sourceURLs, targetURL, bar, ch) + return ch +} + // runCopyCmd is bound to sub-command func runCopyCmd(ctx *cli.Context) { if len(ctx.Args()) < 2 || ctx.Args().First() == "help" { @@ -102,34 +136,11 @@ func runCopyCmd(ctx *cli.Context) { if !globalQuietFlag { bar = newCpBar() } - - go func(sourceURLs []string, targetURL string) { - for cpURLs := range prepareCopyURLs(sourceURLs, targetURL) { - if cpURLs.Error != nil { - // no need to print errors here, any error here - // will be printed later during Copy() - continue - } - if !globalQuietFlag { - bar.Extend(cpURLs.SourceContent.Size) - } + for err := range doCopyCmd(sourceURLs, targetURL, bar) { + if err != nil { + console.Errorln(iodine.ToError(err)) } - }(sourceURLs, targetURL) - - cpQueue := make(chan bool, intMax(runtime.NumCPU()-1, 1)) - wg := new(sync.WaitGroup) - - for cpURLs := range prepareCopyURLs(sourceURLs, targetURL) { - if cpURLs.Error != nil { - console.Errorln(iodine.ToError(cpURLs.Error)) - continue - } - cpQueue <- true - wg.Add(1) - go runCopyInRoutine(cpURLs, &bar, cpQueue, wg) } - close(cpQueue) - wg.Wait() if !globalQuietFlag { bar.Finish() } diff --git a/cp-bar.go b/cp-bar.go index 0924b378..9833fe63 100644 --- a/cp-bar.go +++ b/cp-bar.go @@ -18,6 +18,7 @@ package main import ( "io" + "sync/atomic" "time" "github.com/cheggaaa/pb" @@ -75,10 +76,8 @@ func (b barSend) Finish() { func newCpBar() barSend { cmdCh := make(chan barMsg) finishCh := make(chan bool) - go func(cmdCh <-chan barMsg, finishCh chan<- bool) { started := false - bar := pb.New64(0) bar.SetUnits(pb.U_BYTES) bar.SetRefreshRate(time.Millisecond * 10) @@ -88,13 +87,12 @@ func newCpBar() barSend { // Colorize console.Print("\r" + s) } - // Feels like wget bar.Format("[=> ]") for msg := range cmdCh { switch msg.Cmd { case cpBarCmdExtend: - bar.Total += msg.Arg.(int64) + atomic.AddInt64(&bar.Total, msg.Arg.(int64)) if bar.Total > 0 && !started { started = true bar.Start() diff --git a/cp_test.go b/cp_test.go new file mode 100644 index 00000000..8533646c --- /dev/null +++ b/cp_test.go @@ -0,0 +1,132 @@ +/* + * Minio Client (C) 2015 Minio, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package main + +import ( + "bytes" + "io/ioutil" + "os" + "path/filepath" + "runtime" + "strconv" + + . "github.com/minio/check" +) + +var bar = newCpBar() + +func (s *CmdTestSuite) TestCpTypeA(c *C) { + configDir, err := ioutil.TempDir(os.TempDir(), "cmd-") + c.Assert(err, IsNil) + defer os.RemoveAll(configDir) + switch runtime.GOOS { + case "windows": + mcConfigWindowsDir = configDir + default: + mcConfigDir = configDir + } + _, err = doConfig("generate", nil) + c.Assert(err, IsNil) + + /// filesystem + source, err := ioutil.TempDir(os.TempDir(), "cmd-") + c.Assert(err, IsNil) + defer os.RemoveAll(source) + + sourcePath := filepath.Join(source, "object1") + data := "hello" + dataLen := len(data) + err = putTarget(sourcePath, &hostConfig{}, uint64(dataLen), bytes.NewReader([]byte(data))) + c.Assert(err, IsNil) + + target, err := ioutil.TempDir(os.TempDir(), "cmd-") + c.Assert(err, IsNil) + defer os.RemoveAll(target) + targetPath := filepath.Join(target, "newObject1") + + for err := range doCopyCmd([]string{sourcePath}, targetPath, bar) { + c.Assert(err, IsNil) + } +} + +func (s *CmdTestSuite) TestCpTypeB(c *C) { + configDir, err := ioutil.TempDir(os.TempDir(), "cmd-") + c.Assert(err, IsNil) + defer os.RemoveAll(configDir) + switch runtime.GOOS { + case "windows": + mcConfigWindowsDir = configDir + default: + mcConfigDir = configDir + } + _, err = doConfig("generate", nil) + c.Assert(err, IsNil) + + /// filesystem + source, err := ioutil.TempDir(os.TempDir(), "cmd-") + c.Assert(err, IsNil) + defer os.RemoveAll(source) + + sourcePath := filepath.Join(source, "object1") + data := "hello" + dataLen := len(data) + err = putTarget(sourcePath, &hostConfig{}, uint64(dataLen), bytes.NewReader([]byte(data))) + c.Assert(err, IsNil) + + target, err := ioutil.TempDir(os.TempDir(), "cmd-") + c.Assert(err, IsNil) + defer os.RemoveAll(target) + + for err := range doCopyCmd([]string{sourcePath}, target, bar) { + c.Assert(err, IsNil) + } +} + +func (s *CmdTestSuite) TestCpTypeC(c *C) { + configDir, err := ioutil.TempDir(os.TempDir(), "cmd-") + c.Assert(err, IsNil) + defer os.RemoveAll(configDir) + switch runtime.GOOS { + case "windows": + mcConfigWindowsDir = configDir + default: + mcConfigDir = configDir + } + _, err = doConfig("generate", nil) + c.Assert(err, IsNil) + + /// filesystem + source, err := ioutil.TempDir(os.TempDir(), "cmd-") + c.Assert(err, IsNil) + defer os.RemoveAll(source) + + for i := 0; i < 10; i++ { + objectPath := filepath.Join(source, "object"+strconv.Itoa(i)) + data := "hello" + dataLen := len(data) + err = putTarget(objectPath, &hostConfig{}, uint64(dataLen), bytes.NewReader([]byte(data))) + c.Assert(err, IsNil) + } + + target, err := ioutil.TempDir(os.TempDir(), "cmd-") + c.Assert(err, IsNil) + defer os.RemoveAll(target) + + for err := range doCopyCmd([]string{source + "..."}, target, bar) { + c.Assert(err, IsNil) + } +} diff --git a/pkg/console/console.go b/pkg/console/console.go index 4365956a..f3081e48 100644 --- a/pkg/console/console.go +++ b/pkg/console/console.go @@ -228,6 +228,16 @@ var ( } ) +// Lock console +func Lock() { + mutex.Lock() +} + +// Unlock locked console +func Unlock() { + mutex.Unlock() +} + // SetTheme sets a color theme func SetTheme(themeName string) error { if !IsValidTheme(themeName) {