/
examples_test.go
117 lines (96 loc) · 2.42 KB
/
examples_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package progressio
import (
"bytes"
"fmt"
"io"
"strings"
"time"
)
const throttleTime = 15 * time.Millisecond
const bufSize = 15000000
type throttleWriter struct {
lastTime time.Time
}
func (t *throttleWriter) Write(b []byte) (n int, err error) {
sleep := time.Since(t.lastTime)
if t.lastTime.IsZero() {
sleep = 0
}
if sleep < throttleTime {
<-time.After(throttleTime - sleep)
}
t.lastTime = time.Now()
return len(b), nil
}
type throttleReader struct {
i io.Reader
lastTime time.Time
}
func (t *throttleReader) Read(b []byte) (n int, err error) {
sleep := time.Since(t.lastTime)
if t.lastTime.IsZero() {
sleep = 0
}
if sleep < throttleTime {
<-time.After(throttleTime - sleep)
}
t.lastTime = time.Now()
return t.i.Read(b)
}
func getWriter() io.Writer {
return &throttleWriter{}
}
func getReader() io.Reader {
ibuf := make([]byte, bufSize)
return &throttleReader{
i: bytes.NewBuffer(ibuf),
}
}
func printProgress(msg string, ch <-chan Progress) {
cs := ""
p := Progress{}
for p = range ch {
ps := msg + ": " + p.String()
if len(cs) < len(ps) {
cs = strings.Repeat(" ", len(ps))
}
fmt.Printf("\r%s\r%s", cs, ps)
}
fmt.Printf("\n%s\n", p.String())
}
// ExampleWriter is an example of using the progressio package with an
// io.Writer without knowing the amount of bytes to be processed.
func ExampleWriter() {
r := getReader()
w, ch := NewProgressWriter(getWriter(), -1)
go printProgress("TestWriter", ch)
io.Copy(w, r)
fmt.Printf("Copy done\n")
}
// ExampleWriterSize is an example of using the progressio package with an
// io.Writer while knowing the expected amount of bytes to be processed.
func ExampleWriterSize() {
r := getReader()
w, ch := NewProgressWriter(getWriter(), -1)
go printProgress("TestWriterSize", ch)
io.Copy(w, r)
fmt.Printf("Copy done\n")
}
// ExampleReader is an example of using the progressio package with an
// io.Reader without knowing the amount of bytes to be processed.
func ExampleReader() {
r, ch := NewProgressReader(getReader(), -1)
w := getWriter()
go printProgress("TestReader", ch)
io.Copy(w, r)
fmt.Printf("Copy done\n")
}
// ExampleReaderSize is an example of using the progressio package with an
// io.Reader while knowing the expected amount of bytes to be processed.
func ExampleReaderSize() {
r, ch := NewProgressReader(getReader(), bufSize)
w := getWriter()
go printProgress("TestReaderSize", ch)
io.Copy(w, r)
fmt.Printf("Copy done\n")
}