/
stream_reder_test.go
157 lines (141 loc) · 2.79 KB
/
stream_reder_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
package io
import (
"context"
"encoding/base64"
"io"
"io/ioutil"
"testing"
"github.com/google/go-cmp/cmp"
)
func newReply(data string, base64, eof bool) *ReadReply {
return &ReadReply{
Data: data,
Base64Encoded: &base64,
EOF: eof,
}
}
func TestStreamReader_replyTooBig(t *testing.T) {
want := "helloworld"
data := [][]byte{
[]byte("hello"),
[]byte("world"),
}
r := &StreamReader{
next: func(pos, size int) (*ReadReply, error) {
if len(data) == 0 {
return newReply("", false, true), nil
}
eof := false
d := data[0]
data = data[1:]
if len(data) == 0 {
eof = true
}
return newReply(base64.StdEncoding.EncodeToString(d), true, eof), nil
},
}
b := make([]byte, 1)
var got string
for {
n, err := r.Read(b)
if err != nil {
if err == io.EOF {
break
}
t.Error(err)
}
got += string(b[:n])
}
if got != want {
t.Errorf("want %q, got %q", want, got)
}
}
type streamReaderTestClient struct {
reply []ReadReply
close error
}
func (c *streamReaderTestClient) Read(context.Context, *ReadArgs) (*ReadReply, error) {
if len(c.reply) == 0 {
return nil, io.EOF
}
reply := c.reply[0]
c.reply = c.reply[1:]
return &reply, nil
}
func (c *streamReaderTestClient) Close(context.Context, *CloseArgs) error {
return c.close
}
func TestNewStreamReader_Read(t *testing.T) {
trueBool := true
type args struct {
ctx context.Context
reply []ReadReply
handle StreamHandle
}
tests := []struct {
name string
args args
want string
}{
{
name: "Hello world",
args: args{
ctx: context.Background(),
reply: []ReadReply{
{Data: "Hello ", EOF: false},
{Data: "world!", EOF: true},
},
handle: "",
},
want: "Hello world!",
},
{
name: "Hello world empty EOF",
args: args{
ctx: context.Background(),
reply: []ReadReply{
{Data: "Hello ", EOF: false},
{Data: "world!", EOF: false},
{Data: "", EOF: true},
},
},
want: "Hello world!",
},
{
name: "Hello world base64 encoded",
args: args{
ctx: context.Background(),
reply: []ReadReply{
{
Data: base64.StdEncoding.EncodeToString([]byte("Hello world!")),
Base64Encoded: &trueBool,
EOF: false,
},
{Data: "", EOF: true},
},
},
want: "Hello world!",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
c := &streamReaderTestClient{
reply: tt.args.reply,
close: nil,
}
r := NewStreamReader(tt.args.ctx, c, tt.args.handle)
b, err := ioutil.ReadAll(r)
if err != nil {
t.Error(err)
}
got := string(b)
if diff := cmp.Diff(tt.want, got); diff != "" {
t.Errorf("StreamReader.Read() failed (-want +got)\n%s", diff)
}
err = r.Close()
if err != nil {
t.Error(err)
}
})
}
}