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

Unified Diff: client/internal/logdog/butler/bundler/sizer_fast_test.go

Issue 1276923003: logdog: Add bundler library. (Closed) Base URL: https://github.com/luci/luci-go@logdog-review-streamserver
Patch Set: More documentation, simplified "bundlerStream" struct. 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 side-by-side diff with in-line comments
Download patch
Index: client/internal/logdog/butler/bundler/sizer_fast_test.go
diff --git a/client/internal/logdog/butler/bundler/sizer_fast_test.go b/client/internal/logdog/butler/bundler/sizer_fast_test.go
new file mode 100644
index 0000000000000000000000000000000000000000..9ec38cb576a8fa3128e740dc56e745a5a0c1e799
--- /dev/null
+++ b/client/internal/logdog/butler/bundler/sizer_fast_test.go
@@ -0,0 +1,122 @@
+// 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 bundler
+
+import (
+ "bytes"
+ "fmt"
+ "strings"
+ "testing"
+
+ "github.com/golang/protobuf/proto"
+ "github.com/luci/luci-go/common/logdog/protocol"
+ . "github.com/smartystreets/goconvey/convey"
+)
+
+// A proto.Message implementation with test fields.
+type testMessage struct {
+ U64 *uint64 `protobuf:"varint,1,opt,name=u64"`
+}
+
+func (t *testMessage) Reset() {}
+func (t *testMessage) String() string { return "" }
+func (t *testMessage) ProtoMessage() {}
+
+func TestFastSizerVarintLength(t *testing.T) {
+ Convey(`A test message`, t, func() {
+ for _, threshold := range []uint64{
+ 0,
+ 0x80,
+ 0x4000,
+ 0x200000,
+ 0x100000000,
+ 0x800000000,
+ 0x40000000000,
+ 0x2000000000000,
+ 0x100000000000000,
+ 0x8000000000000000,
+ } {
+
+ for _, delta := range []int64{
+ -2,
+ -1,
+ 0,
+ 1,
+ 2,
+ } {
+ // Add "delta" to "threshold" in a uint64-aware manner.
+ u64 := threshold
+ if delta >= 0 {
+ u64 += uint64(delta)
+ } else {
+ if u64 < uint64(-delta) {
+ continue
+ }
+ u64 -= uint64(-delta)
+ }
+
+ expected := varintLength(u64)
+ Convey(fmt.Sprintf(`Testing threshold 0x%x should encode to varint size %d`, u64, expected), func() {
+ m := &testMessage{
+ U64: &u64,
+ }
+ // Note: "-1" is for the "U64" field tag, "1", which has encoded size of 1.
+ So(expected, ShouldEqual, proto.Size(m)-1)
+ })
+ }
+ }
+ })
+}
+
+func TestFastSizer(t *testing.T) {
+ Convey(`A fastSizer instance`, t, func() {
+ source := "test source"
+ u64 := uint64(0x1337d06)
+ bundle := &protocol.ButlerLogBundle{
+ Source: &source,
+ Timestamp: &protocol.Timestamp{
+ UsFromEpoch: &u64,
+ },
+ }
+ s := NewFastSizer(bundle)
+ initSize := s.Size()
+
+ Convey(`Has a valid initial size.`, func() {
+ So(initSize, ShouldEqual, proto.Size(bundle))
+ })
+
+ Convey(`After appending a bundle entry`, func() {
+ be := &protocol.ButlerLogBundle_Entry{
+ Secret: bytes.Repeat([]byte{0x55, 0xAA}, 16),
+ }
+ s.AppendBundleEntry(be)
+ bundle.Entries = append(bundle.Entries, be)
+ So(s.Size(), ShouldEqual, proto.Size(bundle))
+
+ Convey(`After appending log entries`, func() {
+ for i := 0; i < 10; i++ {
+ log := &protocol.LogEntry{
+ Lines: []string{
+ strings.Repeat("A", i*10),
+ strings.Repeat("B", i*20),
+ },
+ Data: [][]byte{
+ bytes.Repeat([]byte{0x55, 0xAA}, i*10),
+ bytes.Repeat([]byte{0x10, 0x6d, 0x06}, i*20),
+ },
+ }
+
+ s.AppendLogEntry(be, log)
+ be.Logs = append(be.Logs, log)
+ So(s.Size(), ShouldEqual, proto.Size(bundle))
+ }
+
+ Convey(`Whole bundle should equal the actual protobuf size.`, func() {
+ So(s.Size(), ShouldEqual, proto.Size(bundle))
+ })
+ })
+ })
+ })
+}

Powered by Google App Engine
This is Rietveld 408576698