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

Side by Side Diff: common/chunkstream/buffer_test.go

Issue 1413923013: Add `chunk` segmented data library. (Closed) Base URL: https://github.com/luci/luci-go@logdog-review-streamserver
Patch Set: Created 5 years, 1 month 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
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 chunkstream
6
7 import (
8 "fmt"
9 "testing"
10
11 . "github.com/smartystreets/goconvey/convey"
12 )
13
14 func TestBuffer(t *testing.T) {
15 Convey(`An empty Buffer instance`, t, func() {
16 b := Buffer{}
17
18 Convey(`Has a length of zero.`, func() {
19 So(b.Len(), ShouldEqual, 0)
20 })
21
22 Convey(`Has a FirstChunk of nil.`, func() {
23 So(b.FirstChunk(), ShouldEqual, nil)
24 })
25
26 Convey(`Can consume zero bytes without increasing generation.`, func() {
27 b.Consume(0)
28 So(b.gen, ShouldEqual, 0)
29 })
30
31 Convey(`Will panic if more than zero bytes are consumed.`, func( ) {
32 So(func() { b.Consume(1) }, ShouldPanic)
33 })
34
35 Convey(`When Appending an empty chunk`, func() {
36 c := tc()
37 b.Append(c)
38
39 Convey(`The generation doesn't change.`, func() {
40 So(b.gen, ShouldEqual, 0)
41 })
42
43 Convey(`Has a FirstChunk of nil.`, func() {
44 So(b.FirstChunk(), ShouldEqual, nil)
45 })
46
47 Convey(`The Chunk is released.`, func() {
48 So(c.released, ShouldBeTrue)
49 })
50 })
51
52 for _, chunks := range [][]*testChunk{
53 {},
54 {tc()},
55 {tc(0, 1, 2), tc(), tc(3, 4, 5)},
56 } {
57 Convey(fmt.Sprintf(`With chunks %v, can append.`, chunks ), func() {
58 size := int64(0)
59 coalesced := []byte(nil)
60 for _, c := range chunks {
61 b.Append(c)
62 coalesced = append(coalesced, c.Bytes(). ..)
63 size += int64(c.Len())
64 So(b.Len(), ShouldEqual, size)
65 }
66 So(b.Bytes(), ShouldResemble, coalesced)
67
68 Convey(`Can consume chunk-at-a-time.`, func() {
69 gen := b.gen
70 for i, c := range chunks {
71 if c.Len() > 0 {
72 So(b.FirstChunk(), Shoul dEqual, chunks[i])
73 }
74 So(b.Len(), ShouldEqual, size)
75 b.Consume(int64(c.Len()))
76 size -= int64(c.Len())
77
78 if c.Len() > 0 {
79 gen++
80 }
81 So(b.gen, ShouldEqual, gen)
82 }
83 So(b.Len(), ShouldEqual, 0)
84
85 Convey(`All chunks are released.`, func( ) {
86 for _, c := range chunks {
87 So(c.released, ShouldBeT rue)
88 }
89 })
90 })
91
92 Convey(`Can consume byte-at-a-time.`, func() {
93 gen := 0
94 for i := int64(0); i < size; i++ {
95 So(b.Len(), ShouldEqual, (size - i))
96 So(b.Bytes(), ShouldResemble, co alesced[i:])
97 b.Consume(1)
98
99 gen++
100 So(b.gen, ShouldEqual, gen)
101 }
102 So(b.Len(), ShouldEqual, 0)
103
104 Convey(`All chunks are released.`, func( ) {
105 for _, c := range chunks {
106 So(c.released, ShouldBeT rue)
107 }
108 })
109 })
110
111 Convey(`Can consume two bytes at a time.`, func( ) {
112 for i := int64(0); i < size; i += 2 {
113 // Final byte(s), make sure we d on't over-consume.
114 if b.Len() < 2 {
115 i = b.Len()
116 }
117
118 So(b.Len(), ShouldEqual, (size - i))
119 So(b.Bytes(), ShouldResemble, co alesced[i:])
120 b.Consume(2)
121 }
122 So(b.Len(), ShouldEqual, 0)
123
124 Convey(`All chunks are released.`, func( ) {
125 for _, c := range chunks {
126 So(c.released, ShouldBeT rue)
127 }
128 })
129 })
130
131 Convey(`Can consume all at once.`, func() {
132 b.Consume(size)
133 So(b.Len(), ShouldEqual, 0)
134
135 Convey(`All chunks are released.`, func( ) {
136 for _, c := range chunks {
137 So(c.released, ShouldBeT rue)
138 }
139 })
140 })
141 })
142 }
143 })
144 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698