-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpipe_test.go
192 lines (152 loc) · 3.5 KB
/
pipe_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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
//
// pipe_test.go
//
// Created by Frederic DELBOS - [email protected] on Apr 27 2015.
// This file is subject to the terms and conditions defined in
// file 'LICENSE', which is part of this source code package.
//
package pipe_test
import (
"bufio"
"bytes"
"compress/gzip"
"errors"
"github.com/hyperboloide/pipe"
"io"
"log"
"os"
"testing"
)
func genBlob(size int) []byte {
blob := make([]byte, size)
for i := 0; i < size; i++ {
blob[i] = 65 // ascii 'A'
}
return blob
}
var bin = genBlob(1 << 24)
func passProc(r io.Reader, w io.Writer) error {
_, err := io.Copy(w, r)
return err
}
func zip(r io.Reader, w io.Writer) error {
gzw, err := gzip.NewWriterLevel(w, gzip.BestSpeed)
if err != nil {
return err
}
defer gzw.Close()
_, err = io.Copy(gzw, r)
return err
}
func unzip(r io.Reader, w io.Writer) error {
gzr, err := gzip.NewReader(r)
if err != nil {
return err
}
defer gzr.Close()
_, err = io.Copy(w, gzr)
return err
}
func TestBasic(t *testing.T) {
binReader := bytes.NewReader(bin)
var result bytes.Buffer
writer := bufio.NewWriter(&result)
p := pipe.New(binReader).To(writer)
if err := p.Exec(); err != nil {
t.Errorf("errors detected during pipe: %s", err)
}
if !bytes.Equal(result.Bytes(), bin) {
t.Errorf("result do not match")
}
if p.TotalIn != int64(len(bin)) {
t.Errorf("TotalIn do not match")
}
if p.TotalOut != p.TotalIn {
t.Errorf("TotalOut: %d should equal TotalIn", p.TotalOut)
}
}
func TestProcess(t *testing.T) {
p := pipe.New(bytes.NewReader(bin)).Push(passProc, zip, unzip, zip)
var result bytes.Buffer
writer := bufio.NewWriter(&result)
p.To(writer)
if err := p.Exec(); err != nil {
t.Errorf("errors detected during pipe: %s", err)
}
if bytes.Equal(result.Bytes(), bin) {
t.Errorf("result should not match")
}
}
func TestError(t *testing.T) {
p := pipe.New(bytes.NewReader(bin))
var procErr = func(r io.Reader, w io.Writer) error {
io.Copy(w, r)
return errors.New("some error!")
}
p.Push(passProc, passProc, procErr, passProc, passProc)
var result bytes.Buffer
writer := bufio.NewWriter(&result)
p.To(writer)
if err := p.Exec(); err == nil {
t.Errorf("pipe should have an error")
}
}
func TestTee(t *testing.T) {
p := pipe.New(bytes.NewReader(bin))
p.Push(zip)
pTee := p.Tee()
p.Push(unzip)
pTee.Push(unzip)
var result bytes.Buffer
writer := bufio.NewWriter(&result)
p.To(writer)
var resultTee bytes.Buffer
writerTee := bufio.NewWriter(&resultTee)
pTee.To(writerTee)
if err := p.Exec(); err != nil {
t.Errorf("pipe should not have error %s", err)
}
if err := pTee.Exec(); err != nil {
t.Errorf("pipe should not have error %s", err)
}
if !bytes.Equal(result.Bytes(), bin) {
t.Errorf("result do not match")
}
if !bytes.Equal(resultTee.Bytes(), bin) {
t.Errorf("result of tee do not match")
}
}
func ExamplePipe() {
// some Filter transformation function
var zip = func(r io.Reader, w io.Writer) error {
gzw, err := gzip.NewWriterLevel(w, gzip.BestSpeed)
if err != nil {
return err
}
defer gzw.Close()
_, err = io.Copy(gzw, r)
return err
}
// pipe input
in, err := os.Open("test.txt")
if err != nil {
log.Fatal(err)
}
defer in.Close()
// create a new pipe with a io.Reader
p := pipe.New(in)
// Pushing transformation function
p.Push(zip)
// pipe output
out, err := os.Create("test.txt.tgz")
if err != nil {
log.Fatal(err)
}
defer out.Close()
// Set pipe output io.Writer
p.To(out)
// Wait for pipe process to complete
if err := p.Exec(); err != nil {
log.Fatal(err)
}
}