github.com/vmware/govmomi@v0.43.0/vim25/progress/reader_test.go (about) 1 /* 2 Copyright (c) 2014 VMware, Inc. All Rights Reserved. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package progress 18 19 import ( 20 "context" 21 "io" 22 "strings" 23 "testing" 24 ) 25 26 func TestReader(t *testing.T) { 27 s := "helloworld" 28 ch := make(chan Report, 1) 29 pr := NewReader(context.Background(), &dummySinker{ch}, strings.NewReader(s), int64(len(s))) 30 31 var buf [10]byte 32 var q Report 33 var n int 34 var err error 35 36 // Read first byte 37 n, err = pr.Read(buf[0:1]) 38 if n != 1 { 39 t.Errorf("Expected n=1, but got: %d", n) 40 } 41 42 if err != nil { 43 t.Errorf("Error: %s", err) 44 } 45 46 q = <-ch 47 if q.Error() != nil { 48 t.Errorf("Error: %s", err) 49 } 50 51 if f := q.Percentage(); f != 10.0 { 52 t.Errorf("Expected percentage after 1 byte to be 10%%, but got: %.0f%%", f) 53 } 54 55 // Read remaining bytes 56 n, err = pr.Read(buf[:]) 57 if n != 9 { 58 t.Errorf("Expected n=1, but got: %d", n) 59 } 60 if err != nil { 61 t.Errorf("Error: %s", err) 62 } 63 64 q = <-ch 65 if q.Error() != nil { 66 t.Errorf("Error: %s", err) 67 } 68 69 if f := q.Percentage(); f != 100.0 { 70 t.Errorf("Expected percentage after 10 bytes to be 100%%, but got: %.0f%%", f) 71 } 72 73 // Read EOF 74 _, err = pr.Read(buf[:]) 75 <-ch 76 if err != io.EOF { 77 t.Errorf("Expected io.EOF, but got: %s", err) 78 } 79 80 // Mark progress reader as done 81 pr.Done(io.EOF) 82 <-ch 83 if err != io.EOF { 84 t.Errorf("Expected io.EOF, but got: %s", err) 85 } 86 87 // Progress channel should be closed after progress reader is marked done 88 _, ok := <-ch 89 if ok { 90 t.Errorf("Expected channel to be closed") 91 } 92 }