| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 package recordio |
| 6 |
| 7 import ( |
| 8 "bytes" |
| 9 "encoding/binary" |
| 10 "errors" |
| 11 "testing" |
| 12 |
| 13 . "github.com/smartystreets/goconvey/convey" |
| 14 ) |
| 15 |
| 16 type testWriter struct { |
| 17 buf bytes.Buffer |
| 18 errQ []error |
| 19 } |
| 20 |
| 21 func (w *testWriter) Write(data []byte) (int, error) { |
| 22 if len(w.errQ) > 0 { |
| 23 err := w.errQ[0] |
| 24 w.errQ = w.errQ[1:] |
| 25 |
| 26 if err != nil { |
| 27 return 0, err |
| 28 } |
| 29 } |
| 30 return w.buf.Write(data) |
| 31 } |
| 32 |
| 33 func TestWriteFrame(t *testing.T) { |
| 34 t.Parallel() |
| 35 |
| 36 Convey(`Using a buffered test writer`, t, func() { |
| 37 tw := &testWriter{} |
| 38 |
| 39 Convey(`WriteFrame will successfully encode a zero-byte frame.`,
func() { |
| 40 count, err := WriteFrame(tw, []byte{}) |
| 41 So(count, ShouldEqual, 1) |
| 42 So(err, ShouldEqual, nil) |
| 43 So(tw.buf.Bytes(), ShouldResemble, []byte{0x00}) |
| 44 }) |
| 45 |
| 46 Convey(`WriteFrame will successfully encode a 129-byte frame.`,
func() { |
| 47 data := bytes.Repeat([]byte{0x5A}, 129) |
| 48 |
| 49 count, err := WriteFrame(tw, data) |
| 50 So(count, ShouldEqual, 131) |
| 51 So(err, ShouldEqual, nil) |
| 52 So(tw.buf.Bytes(), ShouldResemble, append([]byte{0x81, 0
x01}, data...)) |
| 53 }) |
| 54 |
| 55 Convey(`Will return an error when failing to write the size head
er.`, func() { |
| 56 failErr := errors.New("test: test-induced size error") |
| 57 tw.errQ = []error{ |
| 58 failErr, |
| 59 } |
| 60 |
| 61 count, err := WriteFrame(tw, []byte{0xd0, 0x65}) |
| 62 So(count, ShouldEqual, 0) |
| 63 So(err, ShouldEqual, failErr) |
| 64 }) |
| 65 |
| 66 Convey(`Will return an error when failing to write the frame dat
a.`, func() { |
| 67 failErr := errors.New("test: test-induced size error") |
| 68 tw.errQ = []error{ |
| 69 nil, |
| 70 failErr, |
| 71 } |
| 72 |
| 73 count, err := WriteFrame(tw, []byte{0xd0, 0x65}) |
| 74 So(count, ShouldEqual, 1) |
| 75 So(err, ShouldEqual, failErr) |
| 76 }) |
| 77 }) |
| 78 } |
| 79 |
| 80 func TestWriter(t *testing.T) { |
| 81 t.Parallel() |
| 82 |
| 83 Convey(`A Writer, configured to write to a buffer`, t, func() { |
| 84 tw := &testWriter{} |
| 85 w := NewWriter(tw) |
| 86 |
| 87 Convey(`Can write consecutive frames in 3-byte chunks.`, func()
{ |
| 88 expected := []byte{} |
| 89 var sizeBuf [binary.MaxVarintLen64]byte |
| 90 for _, size := range []int{ |
| 91 1, |
| 92 0, |
| 93 1025, |
| 94 129, |
| 95 11, |
| 96 } { |
| 97 b := bytes.Repeat([]byte{0x55}, size) |
| 98 expected = append(expected, sizeBuf[:binary.PutU
varint(sizeBuf[:], uint64(size))]...) |
| 99 expected = append(expected, b...) |
| 100 |
| 101 for len(b) > 0 { |
| 102 count := 3 |
| 103 if count > len(b) { |
| 104 count = len(b) |
| 105 } |
| 106 |
| 107 c, err := w.Write(b[:count]) |
| 108 So(err, ShouldBeNil) |
| 109 So(c, ShouldEqual, count) |
| 110 |
| 111 b = b[count:] |
| 112 } |
| 113 So(w.Flush(), ShouldBeNil) |
| 114 } |
| 115 |
| 116 So(tw.buf.Bytes(), ShouldResemble, expected) |
| 117 }) |
| 118 |
| 119 Convey(`Will write empty frames if Flush()ed.`, func() { |
| 120 So(w.Flush(), ShouldBeNil) |
| 121 So(w.Flush(), ShouldBeNil) |
| 122 So(w.Flush(), ShouldBeNil) |
| 123 So(tw.buf.Bytes(), ShouldResemble, []byte{0x00, 0x00, 0x
00}) |
| 124 }) |
| 125 |
| 126 Convey(`Will fail to Flush() if the Write fails.`, func() { |
| 127 failErr := errors.New("test: test-induced size error") |
| 128 tw.errQ = []error{ |
| 129 failErr, |
| 130 } |
| 131 So(w.Flush(), ShouldEqual, failErr) |
| 132 }) |
| 133 }) |
| 134 } |
| OLD | NEW |