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

Unified Diff: common/chunkstream/chunk_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 side-by-side diff with in-line comments
Download patch
Index: common/chunkstream/chunk_test.go
diff --git a/common/chunkstream/chunk_test.go b/common/chunkstream/chunk_test.go
new file mode 100644
index 0000000000000000000000000000000000000000..734177c8bb9fdc6fcc6a342339fcf19a48a5a226
--- /dev/null
+++ b/common/chunkstream/chunk_test.go
@@ -0,0 +1,70 @@
+// Copyright 2015 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+package chunkstream
+
+import (
+ "fmt"
+ "strings"
+ "testing"
+
+ . "github.com/smartystreets/goconvey/convey"
+)
+
+type testChunk struct {
+ data []byte
+ released bool
+}
+
+var _ Chunk = (*testChunk)(nil)
+
+func tc(d ...byte) *testChunk {
+ return &testChunk{
+ data: d,
+ }
+}
+
+func (c *testChunk) String() string {
+ pieces := make([]string, len(c.data))
+ for i, d := range c.data {
+ pieces[i] = fmt.Sprintf("0x%02x", d)
+ }
+ return fmt.Sprintf("{%s}", strings.Join(pieces, ", "))
+}
+
+func (c *testChunk) Bytes() []byte {
+ return c.data
+}
+
+func (c *testChunk) Len() int {
+ return len(c.data)
+}
+
+func (c *testChunk) Release() {
+ if c.released {
+ panic("double-free")
+ }
+ c.released = true
+}
+
+func TestChunkNode(t *testing.T) {
+ Convey(`A chunkNode wrapping a testing Chunk implementation`, t, func() {
+ c := tc(0, 1, 2)
+ n := newChunkNode(c)
+
+ Convey(`Should call Chunk methods.`, func() {
+ So(n.Bytes(), ShouldResemble, []byte{0, 1, 2})
+ So(n.Len(), ShouldEqual, 3)
+ })
+
+ Convey(`When released, releases the wrapped Chunk.`, func() {
+ n.release()
+ So(c.released, ShouldBeTrue)
+
+ Convey(`If released again, panics.`, func() {
+ So(func() { n.release() }, ShouldPanic)
+ })
+ })
+ })
+}

Powered by Google App Engine
This is Rietveld 408576698