go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/logdog/client/butler/bundler/binaryParser_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  	"testing"
    19  	"time"
    20  
    21  	. "github.com/smartystreets/goconvey/convey"
    22  	"go.chromium.org/luci/logdog/api/logpb"
    23  )
    24  
    25  func TestBinaryParser(t *testing.T) {
    26  	Convey(`A binaryParser with a threshold of 2.`, t, func() {
    27  		s := &parserTestStream{
    28  			now:         time.Date(2015, 1, 1, 0, 0, 0, 0, time.UTC),
    29  			prefixIndex: 1337,
    30  		}
    31  		p := &binaryParser{
    32  			baseParser: s.base(),
    33  			threshold:  2,
    34  		}
    35  		c := &constraints{
    36  			limit: 32,
    37  		}
    38  
    39  		Convey(`Loaded with data below the threshold`, func() {
    40  			p.Append(data(s.now, 1))
    41  			Convey(`Returns nil when reading data smaller than the threshold.`, func() {
    42  				le, err := p.nextEntry(c)
    43  				So(err, ShouldBeNil)
    44  				So(le, ShouldBeNil)
    45  			})
    46  
    47  			Convey(`Returns a LogEntry when truncating.`, func() {
    48  				c.allowSplit = true
    49  				le, err := p.nextEntry(c)
    50  				So(err, ShouldBeNil)
    51  				So(le, shouldMatchLogEntry, s.le(0, logpb.Binary{
    52  					Data: []byte{1},
    53  				}))
    54  
    55  				le, err = p.nextEntry(c)
    56  				So(err, ShouldBeNil)
    57  				So(le, ShouldBeNil)
    58  			})
    59  		})
    60  
    61  		Convey(`Loaded with 10 bytes of data`, func() {
    62  			p.Append(data(s.now, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9))
    63  
    64  			Convey(`Should yield all data with a limit of 32.`, func() {
    65  				le, err := p.nextEntry(c)
    66  				So(err, ShouldBeNil)
    67  				So(le, shouldMatchLogEntry, s.le(0, logpb.Binary{
    68  					Data: []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9},
    69  				}))
    70  
    71  				le, err = p.nextEntry(c)
    72  				So(err, ShouldBeNil)
    73  				So(le, ShouldBeNil)
    74  			})
    75  
    76  			Convey(`Should yield [0..5], [6..9] with a limit of 6.`, func() {
    77  				c.limit = 6
    78  				le, err := p.nextEntry(c)
    79  				So(err, ShouldBeNil)
    80  				So(le, shouldMatchLogEntry, s.le(0, logpb.Binary{
    81  					Data: []byte{0, 1, 2, 3, 4, 5},
    82  				}))
    83  
    84  				le, err = p.nextEntry(c)
    85  				So(err, ShouldBeNil)
    86  				So(le, shouldMatchLogEntry, s.le(6, logpb.Binary{
    87  					Data: []byte{6, 7, 8, 9},
    88  				}))
    89  
    90  				le, err = p.nextEntry(c)
    91  				So(err, ShouldBeNil)
    92  				So(le, ShouldBeNil)
    93  			})
    94  		})
    95  
    96  		Convey(`Loaded with 8 bytes of data from different times.`, func() {
    97  			for i := 0; i < 8; i++ {
    98  				p.Append(data(s.now.Add(time.Duration(i)*time.Second), byte(i)))
    99  			}
   100  
   101  			Convey(`Ignores the time boundary and returns all 8 bytes.`, func() {
   102  				le, err := p.nextEntry(c)
   103  				So(err, ShouldBeNil)
   104  				So(le, shouldMatchLogEntry, s.le(0, logpb.Binary{
   105  					Data: []byte{0, 1, 2, 3, 4, 5, 6, 7},
   106  				}))
   107  
   108  				le, err = p.nextEntry(c)
   109  				So(err, ShouldBeNil)
   110  				So(le, ShouldBeNil)
   111  			})
   112  		})
   113  	})
   114  }