| 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 ts := "2011-08-30T13:22:53.108Z" |
| 77 bundle := &protocol.ButlerLogBundle{ |
| 78 Source: &source, |
| 79 Timestamp: &protocol.Timestamp{ |
| 80 Value: &ts, |
| 81 }, |
| 82 } |
| 83 s := NewFastSizer(bundle).(*fastSizer) |
| 84 |
| 85 Convey(`Has a valid initial size.`, func() { |
| 86 So(s.Size(), ShouldEqual, proto.Size(bundle)) |
| 87 |
| 88 Convey(`Still has a valid size after Undo().`, func() { |
| 89 s.Undo() |
| 90 So(s.Size(), ShouldEqual, proto.Size(bundle)) |
| 91 }) |
| 92 }) |
| 93 |
| 94 Convey(`After appending a bundle entry`, func() { |
| 95 be := &protocol.ButlerLogBundle_Entry{ |
| 96 Logs: []*protocol.LogEntry{ |
| 97 { |
| 98 Data: []*protocol.LogEntry_Data{ |
| 99 { |
| 100 Value: []byte{0x
55, 0xAA}, |
| 101 }, |
| 102 }, |
| 103 }, |
| 104 }, |
| 105 } |
| 106 s.Append(be, nil) |
| 107 bundle.Entries = append(bundle.Entries, be) |
| 108 So(s.Size(), ShouldEqual, proto.Size(bundle)) |
| 109 |
| 110 Convey(`Can undo the Append().`, func() { |
| 111 s.Undo() |
| 112 bundle.Entries = nil |
| 113 So(s.Size(), ShouldEqual, proto.Size(bundle)) |
| 114 }) |
| 115 |
| 116 Convey(`After appending log entries`, func() { |
| 117 for i := 0; i < 10; i++ { |
| 118 log := &protocol.LogEntry{ |
| 119 Lines: []string{ |
| 120 strings.Repeat("A", i*10
), |
| 121 strings.Repeat("B", i*20
), |
| 122 }, |
| 123 Data: []*protocol.LogEntry_Data{ |
| 124 { |
| 125 Value: bytes.Rep
eat([]byte{0x55, 0xAA}, i*10), |
| 126 }, |
| 127 { |
| 128 Value: bytes.Rep
eat([]byte{0x10, 0x6d, 0x06}, i*20), |
| 129 }, |
| 130 }, |
| 131 } |
| 132 |
| 133 s.Append(be, log) |
| 134 be.Logs = append(be.Logs, log) |
| 135 So(s.Size(), ShouldEqual, proto.Size(bun
dle)) |
| 136 } |
| 137 |
| 138 Convey(`Whole bundle should equal the actual pro
tobuf size.`, func() { |
| 139 So(s.Size(), ShouldEqual, proto.Size(bun
dle)) |
| 140 }) |
| 141 |
| 142 Convey(`Can undo the Append().`, func() { |
| 143 s.Undo() |
| 144 be.Logs = be.Logs[:len(be.Logs)-1] |
| 145 So(s.Size(), ShouldEqual, proto.Size(bun
dle)) |
| 146 }) |
| 147 }) |
| 148 }) |
| 149 }) |
| 150 } |
| OLD | NEW |