github.com/dcarley/cf-cli@v6.24.1-0.20170220111324-4225ff346898+incompatible/cf/net/progress_reader_test.go (about)

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