Skip to content

Commit 0d3d6e1

Browse files
committed
tests
1 parent 2bcd102 commit 0d3d6e1

File tree

2 files changed

+80
-4
lines changed

2 files changed

+80
-4
lines changed

internal/transport/http_util.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ import (
2525
"fmt"
2626
"io"
2727
"math"
28-
"net"
2928
"net/http"
3029
"net/url"
3130
"strconv"
@@ -301,11 +300,11 @@ type bufWriter struct {
301300
buf []byte
302301
offset int
303302
batchSize int
304-
conn net.Conn
303+
conn io.ReadWriter
305304
err error
306305
}
307306

308-
func newBufWriter(conn net.Conn, batchSize int, pool *sync.Pool) *bufWriter {
307+
func newBufWriter(conn io.ReadWriter, batchSize int, pool *sync.Pool) *bufWriter {
309308
w := &bufWriter{
310309
batchSize: batchSize,
311310
conn: conn,
@@ -410,7 +409,7 @@ type framer struct {
410409
var writeBufferPoolMap = make(map[int]*sync.Pool)
411410
var writeBufferMutex sync.Mutex
412411

413-
func newFramer(conn net.Conn, writeBufferSize, readBufferSize int, sharedWriteBuffer bool, maxHeaderListSize uint32, memPool mem.BufferPool) *framer {
412+
func newFramer(conn io.ReadWriter, writeBufferSize, readBufferSize int, sharedWriteBuffer bool, maxHeaderListSize uint32, memPool mem.BufferPool) *framer {
414413
if memPool == nil {
415414
// Note that this is only supposed to be nil in tests. Otherwise, stream
416415
// is always initialized with a BufferPool.

internal/transport/http_util_test.go

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
package transport
2020

2121
import (
22+
"bytes"
2223
"errors"
2324
"fmt"
2425
"io"
@@ -27,6 +28,8 @@ import (
2728
"reflect"
2829
"testing"
2930
"time"
31+
32+
"google.golang.org/grpc/mem"
3033
)
3134

3235
func (s) TestDecodeTimeout(t *testing.T) {
@@ -295,3 +298,77 @@ func BenchmarkEncodeGrpcMessage(b *testing.B) {
295298
}
296299
}
297300
}
301+
302+
func (s) TestFrame_ReadDataFrame(t *testing.T) {
303+
tests := []struct {
304+
name string
305+
w func(fr *framer) error
306+
wantData []byte
307+
wantErr bool
308+
}{
309+
{
310+
name: "good_padded",
311+
w: func(fr *framer) error {
312+
return fr.fr.WriteDataPadded(1, false, []byte("foo"), []byte{0, 0})
313+
},
314+
wantData: []byte("foo"),
315+
},
316+
{
317+
name: "good_unpadded",
318+
w: func(fr *framer) error {
319+
return fr.fr.WriteData(1, false, []byte("foo"))
320+
},
321+
wantData: []byte("foo"),
322+
},
323+
{
324+
name: "padded_zero_data_some_padding",
325+
w: func(fr *framer) error {
326+
return fr.fr.WriteDataPadded(1, false, []byte{}, []byte{0, 0})
327+
},
328+
wantData: []byte{},
329+
},
330+
{
331+
name: "stream_id_0",
332+
w: func(fr *framer) error {
333+
fr.fr.AllowIllegalWrites = true
334+
return fr.fr.WriteData(0, false, []byte("foo"))
335+
},
336+
wantErr: true,
337+
},
338+
}
339+
340+
for _, tt := range tests {
341+
t.Run(tt.name, func(t *testing.T) {
342+
fr, _ := testFramer()
343+
// Write the frame using the provided function.
344+
writeErr := tt.w(fr)
345+
if writeErr != nil {
346+
t.Fatalf("tt.w() returned unexpected error: %v", writeErr)
347+
}
348+
fr.writer.Flush()
349+
350+
// Read the frame back.
351+
f, err := fr.readFrame()
352+
if gotErr := err != nil; gotErr != tt.wantErr {
353+
t.Fatalf("ReadFrame() err = %v; want %t", err, tt.wantErr)
354+
}
355+
if tt.wantErr {
356+
return
357+
}
358+
359+
df, ok := f.(*parsedDataFrame)
360+
if !ok {
361+
t.Fatalf("ReadFrame() returned %T, want *parsedDataFrame", f)
362+
}
363+
if gotData := df.data.ReadOnlyData(); !bytes.Equal(gotData, tt.wantData) {
364+
t.Fatalf("parsedDataFrame.Data() = %q, want %q", gotData, tt.wantData)
365+
}
366+
df.data.Free()
367+
})
368+
}
369+
}
370+
371+
func testFramer() (*framer, *bytes.Buffer) {
372+
buf := new(bytes.Buffer)
373+
return newFramer(buf, defaultWriteBufSize, defaultReadBufSize, false, defaultClientMaxHeaderListSize, mem.DefaultBufferPool()), buf
374+
}

0 commit comments

Comments
 (0)