Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(120)

Side by Side Diff: common/logdog/frame/writer_test.go

Issue 1253353008: logdog: Add frame read/write library. (Closed) Base URL: https://github.com/luci/luci-go@logdog-review-output
Patch Set: Created 5 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« common/logdog/frame/writer.go ('K') | « common/logdog/frame/writer.go ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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 frame
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 type veryLargeArray [1024 * 1024 * 1024]byte
34
35 func TestWriteFrame(t *testing.T) {
36 t.Parallel()
37
38 Convey(`Using a buffered test writer`, t, func() {
39 tw := &testWriter{}
40
41 Convey(`WriteFrame will successfully encode a zero-byte frame.`, func() {
42 count, err := WriteFrame(tw, []byte{})
43 So(count, ShouldEqual, 1)
44 So(err, ShouldEqual, nil)
45 So(tw.buf.Bytes(), ShouldResemble, []byte{0x00})
46 })
47
48 Convey(`WriteFrame will successfully encode a 129-byte frame.`, func() {
49 data := bytes.Repeat([]byte{0x5A}, 129)
50
51 count, err := WriteFrame(tw, data)
52 So(count, ShouldEqual, 131)
53 So(err, ShouldEqual, nil)
54 So(tw.buf.Bytes(), ShouldResemble, append([]byte{0x81, 0 x01}, data...))
55 })
56
57 Convey(`Will return an error when failing to write the size head er.`, func() {
58 failErr := errors.New("test: test-induced size error")
59 tw.errQ = []error{
60 failErr,
61 }
62
63 count, err := WriteFrame(tw, []byte{0xd0, 0x65})
64 So(count, ShouldEqual, 0)
65 So(err, ShouldEqual, failErr)
66 })
67
68 Convey(`Will return an error when failing to write the frame dat a.`, func() {
69 failErr := errors.New("test: test-induced size error")
70 tw.errQ = []error{
71 nil,
72 failErr,
73 }
74
75 count, err := WriteFrame(tw, []byte{0xd0, 0x65})
76 So(count, ShouldEqual, 1)
77 So(err, ShouldEqual, failErr)
78 })
79 })
80 }
81
82 func TestWriter(t *testing.T) {
83 t.Parallel()
84
85 Convey(`A Writer, configured to write to a buffer`, t, func() {
86 tw := &testWriter{}
87 w := NewWriter(tw)
88
89 Convey(`Can write consecutive frames in 3-byte chunks.`, func() {
90 expected := []byte{}
91 var sizeBuf [binary.MaxVarintLen64]byte
92 for _, size := range []int{
93 1,
94 0,
95 1025,
96 129,
97 11,
98 } {
99 b := bytes.Repeat([]byte{0x55}, size)
100 expected = append(expected, sizeBuf[:binary.PutU varint(sizeBuf[:], uint64(size))]...)
101 expected = append(expected, b...)
102
103 for len(b) > 0 {
104 count := 3
105 if count > len(b) {
106 count = len(b)
107 }
108
109 c, err := w.Write(b[:count])
110 So(err, ShouldBeNil)
111 So(c, ShouldEqual, count)
112
113 b = b[count:]
114 }
115 So(w.Flush(), ShouldBeNil)
116 }
117
118 So(tw.buf.Bytes(), ShouldResemble, expected)
119 })
120
121 Convey(`Will write empty frames if Flush()ed.`, func() {
122 So(w.Flush(), ShouldBeNil)
123 So(w.Flush(), ShouldBeNil)
124 So(w.Flush(), ShouldBeNil)
125 So(tw.buf.Bytes(), ShouldResemble, []byte{0x00, 0x00, 0x 00})
126 })
127
128 Convey(`Will fail to Flush() if the Write fails.`, func() {
129 failErr := errors.New("test: test-induced size error")
130 tw.errQ = []error{
131 failErr,
132 }
133 So(w.Flush(), ShouldEqual, failErr)
134 })
135 })
136 }
OLDNEW
« common/logdog/frame/writer.go ('K') | « common/logdog/frame/writer.go ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698