github.com/rakutentech/cli@v6.12.5-0.20151006231303-24468b65536e+incompatible/cf/net/progress_reader_test.go (about)

     1  package net_test
     2  
     3  import (
     4  	"os"
     5  	"time"
     6  
     7  	testterm "github.com/cloudfoundry/cli/testhelpers/terminal"
     8  
     9  	. "github.com/cloudfoundry/cli/cf/net"
    10  	. "github.com/cloudfoundry/cli/testhelpers/matchers"
    11  
    12  	. "github.com/onsi/ginkgo"
    13  	. "github.com/onsi/gomega"
    14  )
    15  
    16  var _ = Describe("ProgressReader", func() {
    17  
    18  	var (
    19  		testFile       *os.File
    20  		err            error
    21  		progressReader *ProgressReader
    22  		ui             *testterm.FakeUI
    23  		b              []byte
    24  		fileStat       os.FileInfo
    25  	)
    26  
    27  	BeforeEach(func() {
    28  		ui = &testterm.FakeUI{}
    29  		testFile, err = os.Open("../../fixtures/test.file")
    30  		Expect(err).ToNot(HaveOccurred())
    31  		fileStat, err = testFile.Stat()
    32  		Expect(err).ToNot(HaveOccurred())
    33  
    34  		b = make([]byte, 1024)
    35  		progressReader = NewProgressReader(testFile, ui, 1*time.Millisecond)
    36  		progressReader.SetTotalSize(fileStat.Size())
    37  	})
    38  
    39  	It("prints progress while content is being read", func() {
    40  		for {
    41  			time.Sleep(50 * time.Microsecond)
    42  			_, err := progressReader.Read(b)
    43  			if err != nil {
    44  				break
    45  			}
    46  		}
    47  
    48  		Expect(ui.UncapturedOutput).To(ContainSubstrings([]string{"\r", "uploaded..."}))
    49  		Expect(ui.UncapturedOutput).To(ContainSubstrings([]string{"\r    "}))
    50  		Expect(ui.Outputs).To(ContainSubstrings([]string{"\rDone "}))
    51  	})
    52  
    53  	It("reads the correct number of bytes", func() {
    54  		bytesRead := 0
    55  
    56  		for {
    57  			n, err := progressReader.Read(b)
    58  			if err != nil {
    59  				break
    60  			}
    61  
    62  			bytesRead += n
    63  		}
    64  
    65  		Expect(int64(bytesRead)).To(Equal(fileStat.Size()))
    66  	})
    67  })