github.com/vmware/govmomi@v0.51.0/vim25/progress/reader_test.go (about)

     1  // © Broadcom. All Rights Reserved.
     2  // The term “Broadcom” refers to Broadcom Inc. and/or its subsidiaries.
     3  // SPDX-License-Identifier: Apache-2.0
     4  
     5  package progress
     6  
     7  import (
     8  	"context"
     9  	"io"
    10  	"strings"
    11  	"testing"
    12  )
    13  
    14  func TestReader(t *testing.T) {
    15  	s := "helloworld"
    16  	ch := make(chan Report, 1)
    17  	pr := NewReader(context.Background(), &dummySinker{ch}, strings.NewReader(s), int64(len(s)))
    18  
    19  	var buf [10]byte
    20  	var q Report
    21  	var n int
    22  	var err error
    23  
    24  	// Read first byte
    25  	n, err = pr.Read(buf[0:1])
    26  	if n != 1 {
    27  		t.Errorf("Expected n=1, but got: %d", n)
    28  	}
    29  
    30  	if err != nil {
    31  		t.Errorf("Error: %s", err)
    32  	}
    33  
    34  	q = <-ch
    35  	if q.Error() != nil {
    36  		t.Errorf("Error: %s", err)
    37  	}
    38  
    39  	if f := q.Percentage(); f != 10.0 {
    40  		t.Errorf("Expected percentage after 1 byte to be 10%%, but got: %.0f%%", f)
    41  	}
    42  
    43  	// Read remaining bytes
    44  	n, err = pr.Read(buf[:])
    45  	if n != 9 {
    46  		t.Errorf("Expected n=1, but got: %d", n)
    47  	}
    48  	if err != nil {
    49  		t.Errorf("Error: %s", err)
    50  	}
    51  
    52  	q = <-ch
    53  	if q.Error() != nil {
    54  		t.Errorf("Error: %s", err)
    55  	}
    56  
    57  	if f := q.Percentage(); f != 100.0 {
    58  		t.Errorf("Expected percentage after 10 bytes to be 100%%, but got: %.0f%%", f)
    59  	}
    60  
    61  	// Read EOF
    62  	_, err = pr.Read(buf[:])
    63  	<-ch
    64  	if err != io.EOF {
    65  		t.Errorf("Expected io.EOF, but got: %s", err)
    66  	}
    67  
    68  	// Mark progress reader as done
    69  	pr.Done(io.EOF)
    70  	<-ch
    71  	if err != io.EOF {
    72  		t.Errorf("Expected io.EOF, but got: %s", err)
    73  	}
    74  
    75  	// Progress channel should be closed after progress reader is marked done
    76  	_, ok := <-ch
    77  	if ok {
    78  		t.Errorf("Expected channel to be closed")
    79  	}
    80  }