| 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 "fmt" |
| 9 "time" |
| 10 |
| 11 "github.com/luci/luci-go/common/logdog/protocol" |
| 12 "github.com/luci/luci-go/common/proto/google" |
| 13 "github.com/luci/luci-go/common/testing/assertions" |
| 14 ) |
| 15 |
| 16 type testData struct { |
| 17 Data |
| 18 released bool |
| 19 } |
| 20 |
| 21 func (d *testData) Release() { |
| 22 d.released = true |
| 23 } |
| 24 |
| 25 func data(ts time.Time, b ...byte) *testData { |
| 26 return &testData{ |
| 27 Data: &streamData{ |
| 28 buffer: b, |
| 29 bufferBase: b, |
| 30 ts: ts, |
| 31 }, |
| 32 } |
| 33 } |
| 34 |
| 35 func dstr(ts time.Time, s string) *testData { |
| 36 return data(ts, []byte(s)...) |
| 37 } |
| 38 |
| 39 func shouldMatchLogEntry(actual interface{}, expected ...interface{}) string { |
| 40 if actual == (*protocol.LogEntry)(nil) { |
| 41 return fmt.Sprintf("actual should not be nil") |
| 42 } |
| 43 if len(expected) != 1 || expected[0] == (*protocol.LogEntry)(nil) { |
| 44 return fmt.Sprintf("expected should not be nil") |
| 45 } |
| 46 |
| 47 return assertions.ShouldResembleV(actual, expected[0]) |
| 48 } |
| 49 |
| 50 type parserTestStream struct { |
| 51 now time.Time |
| 52 prefixIndex int64 |
| 53 streamIndex int64 |
| 54 offset time.Duration |
| 55 sequence int64 |
| 56 } |
| 57 |
| 58 func (s *parserTestStream) base() baseParser { |
| 59 return baseParser{ |
| 60 counter: &counter{ |
| 61 current: s.prefixIndex, |
| 62 }, |
| 63 timeBase: s.now, |
| 64 } |
| 65 } |
| 66 |
| 67 func (s *parserTestStream) next(d interface{}) *protocol.LogEntry { |
| 68 le := &protocol.LogEntry{ |
| 69 TimeOffset: google.NewDuration(s.offset), |
| 70 PrefixIndex: uint64(s.prefixIndex), |
| 71 StreamIndex: uint64(s.streamIndex), |
| 72 } |
| 73 |
| 74 switch t := d.(type) { |
| 75 case protocol.Text: |
| 76 le.Content = &protocol.LogEntry_Text{Text: &t} |
| 77 |
| 78 case protocol.Binary: |
| 79 le.Content = &protocol.LogEntry_Binary{Binary: &t} |
| 80 |
| 81 case protocol.Datagram: |
| 82 le.Content = &protocol.LogEntry_Datagram{Datagram: &t} |
| 83 |
| 84 default: |
| 85 panic(fmt.Errorf("unknown content type: %T", t)) |
| 86 } |
| 87 |
| 88 s.prefixIndex++ |
| 89 s.streamIndex++ |
| 90 return le |
| 91 } |
| 92 |
| 93 func (s *parserTestStream) add(d time.Duration) *parserTestStream { |
| 94 s.offset += d |
| 95 return s |
| 96 } |
| 97 |
| 98 func (s *parserTestStream) le(seq int64, d interface{}) *protocol.LogEntry { |
| 99 le := s.next(d) |
| 100 le.Sequence = uint64(seq) |
| 101 return le |
| 102 } |
| OLD | NEW |