go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/logdog/client/butler/bundler/data_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  )
    23  
    24  func TestDataPool(t *testing.T) {
    25  	Convey(`A data pool registry`, t, func() {
    26  		reg := dataPoolRegistry{}
    27  
    28  		Convey(`Can retrieve a 1024-byte data pool.`, func() {
    29  			pool := reg.getPool(1024)
    30  			So(pool.size, ShouldEqual, 1024)
    31  
    32  			Convey(`Subsequent requests return the same pool.`, func() {
    33  				So(reg.getPool(1024), ShouldEqual, pool)
    34  			})
    35  		})
    36  	})
    37  }
    38  
    39  func TestData(t *testing.T) {
    40  	Convey(`A 512-byte data pool`, t, func() {
    41  		pool := (&dataPoolRegistry{}).getPool(512)
    42  
    43  		Convey(`Will allocate a clean 512-byte Data`, func() {
    44  			d := pool.getData().(*streamData)
    45  			defer d.Release()
    46  
    47  			So(d, ShouldNotBeNil)
    48  			So(len(d.Bytes()), ShouldEqual, 512)
    49  			So(cap(d.Bytes()), ShouldEqual, 512)
    50  
    51  			Convey(`When bound, adjusts its byte size and retains a timestamp.`, func() {
    52  				now := time.Date(2015, 1, 1, 0, 0, 0, 0, time.UTC)
    53  				data := [64]byte{}
    54  				for i := range data {
    55  					data[i] = byte(i)
    56  				}
    57  
    58  				copy(d.Bytes(), data[:])
    59  				So(d.Bind(len(data), now), ShouldEqual, d)
    60  				So(d.Bytes(), ShouldResemble, data[:])
    61  				So(d.Len(), ShouldEqual, len(data))
    62  				So(d.Timestamp().Equal(now), ShouldBeTrue)
    63  			})
    64  		})
    65  	})
    66  }