| 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..e149e547c892aa4c7d41cb2f9ef71d4036996f11
|
| --- /dev/null
|
| +++ b/client/internal/logdog/butler/bundler/sizer_fast_test.go
|
| @@ -0,0 +1,150 @@
|
| +// 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"
|
| + ts := "2011-08-30T13:22:53.108Z"
|
| + bundle := &protocol.ButlerLogBundle{
|
| + Source: &source,
|
| + Timestamp: &protocol.Timestamp{
|
| + Value: &ts,
|
| + },
|
| + }
|
| + s := NewFastSizer(bundle).(*fastSizer)
|
| +
|
| + Convey(`Has a valid initial size.`, func() {
|
| + So(s.Size(), ShouldEqual, proto.Size(bundle))
|
| +
|
| + Convey(`Still has a valid size after Undo().`, func() {
|
| + s.Undo()
|
| + So(s.Size(), ShouldEqual, proto.Size(bundle))
|
| + })
|
| + })
|
| +
|
| + Convey(`After appending a bundle entry`, func() {
|
| + be := &protocol.ButlerLogBundle_Entry{
|
| + Logs: []*protocol.LogEntry{
|
| + {
|
| + Data: []*protocol.LogEntry_Data{
|
| + {
|
| + Value: []byte{0x55, 0xAA},
|
| + },
|
| + },
|
| + },
|
| + },
|
| + }
|
| + s.Append(be, nil)
|
| + bundle.Entries = append(bundle.Entries, be)
|
| + So(s.Size(), ShouldEqual, proto.Size(bundle))
|
| +
|
| + Convey(`Can undo the Append().`, func() {
|
| + s.Undo()
|
| + bundle.Entries = nil
|
| + 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: []*protocol.LogEntry_Data{
|
| + {
|
| + Value: bytes.Repeat([]byte{0x55, 0xAA}, i*10),
|
| + },
|
| + {
|
| + Value: bytes.Repeat([]byte{0x10, 0x6d, 0x06}, i*20),
|
| + },
|
| + },
|
| + }
|
| +
|
| + s.Append(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))
|
| + })
|
| +
|
| + Convey(`Can undo the Append().`, func() {
|
| + s.Undo()
|
| + be.Logs = be.Logs[:len(be.Logs)-1]
|
| + So(s.Size(), ShouldEqual, proto.Size(bundle))
|
| + })
|
| + })
|
| + })
|
| + })
|
| +}
|
|
|