~nskaggs/+junk/xenial-test

« back to all changes in this revision

Viewing changes to src/github.com/juju/govmomi/vim25/progress/reader_test.go

  • Committer: Nicholas Skaggs
  • Date: 2016-10-24 20:56:05 UTC
  • Revision ID: nicholas.skaggs@canonical.com-20161024205605-z8lta0uvuhtxwzwl
Initi with beta15

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
        "io"
 
21
        "strings"
 
22
        "testing"
 
23
)
 
24
 
 
25
func TestReader(t *testing.T) {
 
26
        s := "helloworld"
 
27
        ch := make(chan Report, 1)
 
28
        pr := NewReader(&dummySinker{ch}, strings.NewReader(s), int64(len(s)))
 
29
 
 
30
        var buf [10]byte
 
31
        var q Report
 
32
        var n int
 
33
        var err error
 
34
 
 
35
        // Read first byte
 
36
        n, err = pr.Read(buf[0:1])
 
37
        if n != 1 {
 
38
                t.Errorf("Expected n=1, but got: %d", n)
 
39
        }
 
40
 
 
41
        if err != nil {
 
42
                t.Errorf("Error: %s", err)
 
43
        }
 
44
 
 
45
        q = <-ch
 
46
        if q.Error() != nil {
 
47
                t.Errorf("Error: %s", err)
 
48
        }
 
49
 
 
50
        if f := q.Percentage(); f != 10.0 {
 
51
                t.Errorf("Expected percentage after 1 byte to be 10%%, but got: %.0f%%", f)
 
52
        }
 
53
 
 
54
        // Read remaining bytes
 
55
        n, err = pr.Read(buf[:])
 
56
        if n != 9 {
 
57
                t.Errorf("Expected n=1, but got: %d", n)
 
58
        }
 
59
        if err != nil {
 
60
                t.Errorf("Error: %s", err)
 
61
        }
 
62
 
 
63
        q = <-ch
 
64
        if q.Error() != nil {
 
65
                t.Errorf("Error: %s", err)
 
66
        }
 
67
 
 
68
        if f := q.Percentage(); f != 100.0 {
 
69
                t.Errorf("Expected percentage after 10 bytes to be 100%%, but got: %.0f%%", f)
 
70
        }
 
71
 
 
72
        // Read EOF
 
73
        _, err = pr.Read(buf[:])
 
74
        if err != io.EOF {
 
75
                t.Errorf("Expected io.EOF, but got: %s", err)
 
76
        }
 
77
 
 
78
        // Mark progress reader as done
 
79
        pr.Done(io.EOF)
 
80
        q = <-ch
 
81
        if err != io.EOF {
 
82
                t.Errorf("Expected io.EOF, but got: %s", err)
 
83
        }
 
84
 
 
85
        // Progress channel should be closed after progress reader is marked done
 
86
        _, ok := <-ch
 
87
        if ok {
 
88
                t.Errorf("Expected channel to be closed")
 
89
        }
 
90
}