forked from go-openapi/runtime
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bytestream_test.go
220 lines (179 loc) · 5.63 KB
/
bytestream_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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
package runtime
import (
"bytes"
"errors"
"fmt"
"sync/atomic"
"testing"
"github.com/stretchr/testify/assert"
)
func TestByteStreamConsumer(t *testing.T) {
cons := ByteStreamConsumer()
expected := "the data for the stream to be sent over the wire"
// can consume as a Writer
var b bytes.Buffer
if assert.NoError(t, cons.Consume(bytes.NewBufferString(expected), &b)) {
assert.Equal(t, expected, b.String())
}
// can consume as an UnmarshalBinary
var bu binaryUnmarshalDummy
if assert.NoError(t, cons.Consume(bytes.NewBufferString(expected), &bu)) {
assert.Equal(t, expected, bu.str)
}
// can consume as a binary slice
var bs []byte
if assert.NoError(t, cons.Consume(bytes.NewBufferString(expected), &bs)) {
assert.Equal(t, expected, string(bs))
}
type binarySlice []byte
var bs2 binarySlice
if assert.NoError(t, cons.Consume(bytes.NewBufferString(expected), &bs2)) {
assert.Equal(t, expected, string(bs2))
}
// passing in a nilslice wil result in an error
var ns *[]byte
assert.Error(t, cons.Consume(bytes.NewBufferString(expected), &ns))
// passing in nil wil result in an error as well
assert.Error(t, cons.Consume(bytes.NewBufferString(expected), nil))
// a reader who results in an error, will make it fail
assert.Error(t, cons.Consume(new(nopReader), &bu))
assert.Error(t, cons.Consume(new(nopReader), &bs))
// the readers can also not be nil
assert.Error(t, cons.Consume(nil, &bs))
}
type binaryUnmarshalDummy struct {
str string
}
func (b *binaryUnmarshalDummy) UnmarshalBinary(bytes []byte) error {
if len(bytes) == 0 {
return errors.New("no text given")
}
b.str = string(bytes)
return nil
}
func TestByteStreamProducer(t *testing.T) {
cons := ByteStreamProducer()
expected := "the data for the stream to be sent over the wire"
var rdr bytes.Buffer
// can produce using a reader
if assert.NoError(t, cons.Produce(&rdr, bytes.NewBufferString(expected))) {
assert.Equal(t, expected, rdr.String())
rdr.Reset()
}
// can produce using a binary marshaller
if assert.NoError(t, cons.Produce(&rdr, &binaryMarshalDummy{expected})) {
assert.Equal(t, expected, rdr.String())
rdr.Reset()
}
// binary slices can also be used to produce
if assert.NoError(t, cons.Produce(&rdr, []byte(expected))) {
assert.Equal(t, expected, rdr.String())
rdr.Reset()
}
// errors can also be used to produce
if assert.NoError(t, cons.Produce(&rdr, errors.New(expected))) {
assert.Equal(t, expected, rdr.String())
rdr.Reset()
}
// structs can also be used to produce
if assert.NoError(t, cons.Produce(&rdr, Error{Message: expected})) {
assert.Equal(t, fmt.Sprintf(`{"message":%q}`, expected), rdr.String())
rdr.Reset()
}
// struct pointers can also be used to produce
if assert.NoError(t, cons.Produce(&rdr, &Error{Message: expected})) {
assert.Equal(t, fmt.Sprintf(`{"message":%q}`, expected), rdr.String())
rdr.Reset()
}
// slices can also be used to produce
if assert.NoError(t, cons.Produce(&rdr, []string{expected})) {
assert.Equal(t, fmt.Sprintf(`[%q]`, expected), rdr.String())
rdr.Reset()
}
type binarySlice []byte
if assert.NoError(t, cons.Produce(&rdr, binarySlice(expected))) {
assert.Equal(t, expected, rdr.String())
rdr.Reset()
}
// when binaryMarshal data is used, its potential error gets propagated
assert.Error(t, cons.Produce(&rdr, new(binaryMarshalDummy)))
// nil data should never be accepted either
assert.Error(t, cons.Produce(&rdr, nil))
// nil readers should also never be acccepted
assert.Error(t, cons.Produce(nil, bytes.NewBufferString(expected)))
}
type binaryMarshalDummy struct {
str string
}
func (b *binaryMarshalDummy) MarshalBinary() ([]byte, error) {
if len(b.str) == 0 {
return nil, errors.New("no text set")
}
return []byte(b.str), nil
}
type closingWriter struct {
calledClose int64
calledWrite int64
b bytes.Buffer
}
func (c *closingWriter) Close() error {
atomic.AddInt64(&c.calledClose, 1)
return nil
}
func (c *closingWriter) Write(p []byte) (n int, err error) {
atomic.AddInt64(&c.calledWrite, 1)
return c.b.Write(p)
}
func (c *closingWriter) String() string {
return c.b.String()
}
type closingReader struct {
calledClose int64
calledRead int64
b *bytes.Buffer
}
func (c *closingReader) Close() error {
atomic.AddInt64(&c.calledClose, 1)
return nil
}
func (c *closingReader) Read(p []byte) (n int, err error) {
atomic.AddInt64(&c.calledRead, 1)
return c.b.Read(p)
}
func TestBytestreamConsumer_Close(t *testing.T) {
cons := ByteStreamConsumer(ClosesStream)
expected := "the data for the stream to be sent over the wire"
// can consume as a Writer
var b bytes.Buffer
r := &closingReader{b: bytes.NewBufferString(expected)}
if assert.NoError(t, cons.Consume(r, &b)) {
assert.Equal(t, expected, b.String())
assert.EqualValues(t, 1, r.calledClose)
}
// can consume as a Writer
cons = ByteStreamConsumer()
b.Reset()
r = &closingReader{b: bytes.NewBufferString(expected)}
if assert.NoError(t, cons.Consume(r, &b)) {
assert.Equal(t, expected, b.String())
assert.EqualValues(t, 0, r.calledClose)
}
}
func TestBytestreamProducer_Close(t *testing.T) {
cons := ByteStreamProducer(ClosesStream)
expected := "the data for the stream to be sent over the wire"
// can consume as a Writer
r := &closingWriter{}
// can produce using a reader
if assert.NoError(t, cons.Produce(r, bytes.NewBufferString(expected))) {
assert.Equal(t, expected, r.String())
assert.EqualValues(t, 1, r.calledClose)
}
cons = ByteStreamProducer()
r = &closingWriter{}
// can produce using a reader
if assert.NoError(t, cons.Produce(r, bytes.NewBufferString(expected))) {
assert.Equal(t, expected, r.String())
assert.EqualValues(t, 0, r.calledClose)
}
}