github.com/lukasheimann/cloudfoundrycli@v7.1.0+incompatible/cf/net/progress_reader_test.go (about)

     1  // +build windows
     2  
     3  package net_test
     4  
     5  import (
     6  	"os"
     7  	"time"
     8  
     9  	. "code.cloudfoundry.org/cli/cf/net"
    10  	"code.cloudfoundry.org/cli/cf/terminal/terminalfakes"
    11  	. "github.com/onsi/ginkgo"
    12  	. "github.com/onsi/gomega"
    13  )
    14  
    15  var _ = Describe("ProgressReader", func() {
    16  	var (
    17  		testFile       *os.File
    18  		err            error
    19  		progressReader *ProgressReader
    20  		ui             *terminalfakes.FakeUI
    21  		b              []byte
    22  		fileStat       os.FileInfo
    23  	)
    24  
    25  	BeforeEach(func() {
    26  		ui = new(terminalfakes.FakeUI)
    27  
    28  		testFile, err = os.Open("../../fixtures/test.file")
    29  		Expect(err).NotTo(HaveOccurred())
    30  
    31  		fileStat, err = testFile.Stat()
    32  		Expect(err).NotTo(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.SayCallCount()).To(Equal(1))
    49  		Expect(ui.SayArgsForCall(0)).To(ContainSubstring("\rDone "))
    50  
    51  		Expect(ui.PrintCapturingNoOutputCallCount()).To(BeNumerically(">", 0))
    52  		status, _ := ui.PrintCapturingNoOutputArgsForCall(0)
    53  		Expect(status).To(ContainSubstring("uploaded..."))
    54  		status, _ = ui.PrintCapturingNoOutputArgsForCall(ui.PrintCapturingNoOutputCallCount() - 1)
    55  		Expect(status).To(Equal("\r                             "))
    56  	})
    57  
    58  	It("reads the correct number of bytes", func() {
    59  		bytesRead := 0
    60  
    61  		for {
    62  			n, err := progressReader.Read(b)
    63  			if err != nil {
    64  				break
    65  			}
    66  
    67  			bytesRead += n
    68  		}
    69  
    70  		Expect(int64(bytesRead)).To(Equal(fileStat.Size()))
    71  	})
    72  })