go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/logdog/client/butler/bundler/parser_test.go (about)

     1  // Copyright 2015 The LUCI Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package bundler
    16  
    17  import (
    18  	"fmt"
    19  	"time"
    20  
    21  	. "github.com/smartystreets/goconvey/convey"
    22  	"google.golang.org/protobuf/types/known/durationpb"
    23  
    24  	"go.chromium.org/luci/logdog/api/logpb"
    25  )
    26  
    27  type testData struct {
    28  	Data
    29  	released bool
    30  }
    31  
    32  func (d *testData) Release() {
    33  	d.released = true
    34  }
    35  
    36  func data(ts time.Time, b ...byte) *testData {
    37  	return &testData{
    38  		Data: &streamData{
    39  			buffer: b,
    40  			ts:     ts,
    41  		},
    42  	}
    43  }
    44  
    45  func dstr(ts time.Time, s string) *testData {
    46  	return data(ts, []byte(s)...)
    47  }
    48  
    49  func shouldMatchLogEntry(actual any, expected ...any) string {
    50  	if actual == (*logpb.LogEntry)(nil) {
    51  		return fmt.Sprintf("actual should not be nil")
    52  	}
    53  	if len(expected) != 1 || expected[0] == (*logpb.LogEntry)(nil) {
    54  		return fmt.Sprintf("expected should not be nil")
    55  	}
    56  
    57  	return ShouldResemble(actual, expected[0])
    58  }
    59  
    60  type parserTestStream struct {
    61  	now         time.Time
    62  	prefixIndex int64
    63  	streamIndex int64
    64  	offset      time.Duration
    65  }
    66  
    67  func (s *parserTestStream) base() baseParser {
    68  	return baseParser{
    69  		counter: &counter{
    70  			current: s.prefixIndex,
    71  		},
    72  		timeBase: s.now,
    73  	}
    74  }
    75  
    76  func (s *parserTestStream) next(d any) *logpb.LogEntry {
    77  	le := &logpb.LogEntry{
    78  		TimeOffset:  durationpb.New(s.offset),
    79  		PrefixIndex: uint64(s.prefixIndex),
    80  		StreamIndex: uint64(s.streamIndex),
    81  	}
    82  
    83  	switch t := d.(type) {
    84  	case logpb.Text:
    85  		le.Content = &logpb.LogEntry_Text{Text: &t}
    86  
    87  	case logpb.Binary:
    88  		le.Content = &logpb.LogEntry_Binary{Binary: &t}
    89  
    90  	case logpb.Datagram:
    91  		le.Content = &logpb.LogEntry_Datagram{Datagram: &t}
    92  
    93  	default:
    94  		panic(fmt.Errorf("unknown content type: %T", t))
    95  	}
    96  
    97  	s.prefixIndex++
    98  	s.streamIndex++
    99  	return le
   100  }
   101  
   102  func (s *parserTestStream) add(d time.Duration) *parserTestStream {
   103  	s.offset += d
   104  	return s
   105  }
   106  
   107  func (s *parserTestStream) le(seq int64, d any) *logpb.LogEntry {
   108  	le := s.next(d)
   109  	le.Sequence = uint64(seq)
   110  	return le
   111  }