| OLD | NEW |
| (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 bundler |
| 6 |
| 7 import ( |
| 8 "bytes" |
| 9 "fmt" |
| 10 "strings" |
| 11 "testing" |
| 12 |
| 13 "github.com/golang/protobuf/proto" |
| 14 "github.com/luci/luci-go/common/logdog/protocol" |
| 15 . "github.com/smartystreets/goconvey/convey" |
| 16 ) |
| 17 |
| 18 // A proto.Message implementation with test fields. |
| 19 type testMessage struct { |
| 20 U64 *uint64 `protobuf:"varint,1,opt,name=u64"` |
| 21 } |
| 22 |
| 23 func (t *testMessage) Reset() {} |
| 24 func (t *testMessage) String() string { return "" } |
| 25 func (t *testMessage) ProtoMessage() {} |
| 26 |
| 27 func TestFastSizerVarintLength(t *testing.T) { |
| 28 Convey(`A test message`, t, func() { |
| 29 for _, threshold := range []uint64{ |
| 30 0, |
| 31 0x80, |
| 32 0x4000, |
| 33 0x200000, |
| 34 0x100000000, |
| 35 0x800000000, |
| 36 0x40000000000, |
| 37 0x2000000000000, |
| 38 0x100000000000000, |
| 39 0x8000000000000000, |
| 40 } { |
| 41 |
| 42 for _, delta := range []int64{ |
| 43 -2, |
| 44 -1, |
| 45 0, |
| 46 1, |
| 47 2, |
| 48 } { |
| 49 // Add "delta" to "threshold" in a uint64-aware
manner. |
| 50 u64 := threshold |
| 51 if delta >= 0 { |
| 52 u64 += uint64(delta) |
| 53 } else { |
| 54 if u64 < uint64(-delta) { |
| 55 continue |
| 56 } |
| 57 u64 -= uint64(-delta) |
| 58 } |
| 59 |
| 60 expected := varintLength(u64) |
| 61 Convey(fmt.Sprintf(`Testing threshold 0x%x shoul
d encode to varint size %d`, u64, expected), func() { |
| 62 m := &testMessage{ |
| 63 U64: &u64, |
| 64 } |
| 65 // Note: "-1" is for the "U64" field tag
, "1", which has encoded size of 1. |
| 66 So(expected, ShouldEqual, proto.Size(m)-
1) |
| 67 }) |
| 68 } |
| 69 } |
| 70 }) |
| 71 } |
| 72 |
| 73 func TestFastSizer(t *testing.T) { |
| 74 Convey(`A fastSizer instance`, t, func() { |
| 75 source := "test source" |
| 76 u64 := uint64(0x1337d06) |
| 77 bundle := &protocol.ButlerLogBundle{ |
| 78 Source: &source, |
| 79 Timestamp: &protocol.Timestamp{ |
| 80 UsFromEpoch: &u64, |
| 81 }, |
| 82 } |
| 83 s := NewFastSizer(bundle) |
| 84 initSize := s.Size() |
| 85 |
| 86 Convey(`Has a valid initial size.`, func() { |
| 87 So(initSize, ShouldEqual, proto.Size(bundle)) |
| 88 }) |
| 89 |
| 90 Convey(`After appending a bundle entry`, func() { |
| 91 be := &protocol.ButlerLogBundle_Entry{ |
| 92 Secret: bytes.Repeat([]byte{0x55, 0xAA}, 16), |
| 93 } |
| 94 s.AppendBundleEntry(be) |
| 95 bundle.Entries = append(bundle.Entries, be) |
| 96 So(s.Size(), ShouldEqual, proto.Size(bundle)) |
| 97 |
| 98 Convey(`After appending log entries`, func() { |
| 99 for i := 0; i < 10; i++ { |
| 100 log := &protocol.LogEntry{ |
| 101 Lines: []string{ |
| 102 strings.Repeat("A", i*10
), |
| 103 strings.Repeat("B", i*20
), |
| 104 }, |
| 105 Data: [][]byte{ |
| 106 bytes.Repeat([]byte{0x55
, 0xAA}, i*10), |
| 107 bytes.Repeat([]byte{0x10
, 0x6d, 0x06}, i*20), |
| 108 }, |
| 109 } |
| 110 |
| 111 s.AppendLogEntry(be, log) |
| 112 be.Logs = append(be.Logs, log) |
| 113 So(s.Size(), ShouldEqual, proto.Size(bun
dle)) |
| 114 } |
| 115 |
| 116 Convey(`Whole bundle should equal the actual pro
tobuf size.`, func() { |
| 117 So(s.Size(), ShouldEqual, proto.Size(bun
dle)) |
| 118 }) |
| 119 }) |
| 120 }) |
| 121 }) |
| 122 } |
| OLD | NEW |