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

Side by Side 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: Rewrote bundle logic (and associated updates). 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 unified diff | Download patch
OLDNEW
(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).(*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 Secret: bytes.Repeat([]byte{0x55, 0xAA}, 16),
97 }
98 s.Append(be, nil)
99 bundle.Entries = append(bundle.Entries, be)
100 So(s.Size(), ShouldEqual, proto.Size(bundle))
101
102 Convey(`Can undo the Append().`, func() {
103 s.Undo()
104 bundle.Entries = nil
105 So(s.Size(), ShouldEqual, proto.Size(bundle))
106 })
107
108 Convey(`After appending log entries`, func() {
109 for i := 0; i < 10; i++ {
110 log := &protocol.LogEntry{
111 Lines: []string{
112 strings.Repeat("A", i*10 ),
113 strings.Repeat("B", i*20 ),
114 },
115 Data: [][]byte{
116 bytes.Repeat([]byte{0x55 , 0xAA}, i*10),
117 bytes.Repeat([]byte{0x10 , 0x6d, 0x06}, i*20),
118 },
119 }
120
121 s.Append(be, log)
122 be.Logs = append(be.Logs, log)
123 So(s.Size(), ShouldEqual, proto.Size(bun dle))
124 }
125
126 Convey(`Whole bundle should equal the actual pro tobuf size.`, func() {
127 So(s.Size(), ShouldEqual, proto.Size(bun dle))
128 })
129
130 Convey(`Can undo the Append().`, func() {
131 s.Undo()
132 be.Logs = be.Logs[:len(be.Logs)-1]
133 So(s.Size(), ShouldEqual, proto.Size(bun dle))
134 })
135 })
136 })
137 })
138 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698