go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/resultdb/bqutil/storagewrite_test.go (about)

     1  // Copyright 2024 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 bqutil
    16  
    17  import (
    18  	"strings"
    19  	"testing"
    20  
    21  	"google.golang.org/protobuf/proto"
    22  
    23  	bqpb "go.chromium.org/luci/resultdb/proto/bq"
    24  
    25  	. "github.com/smartystreets/goconvey/convey"
    26  	. "go.chromium.org/luci/common/testing/assertions"
    27  )
    28  
    29  func TestBatch(t *testing.T) {
    30  	Convey(`Batch`, t, func() {
    31  		Convey(`Non-empty`, func() {
    32  			var rows []proto.Message
    33  			for i := 0; i < 10; i++ {
    34  				// Rows of ~1 MB each.
    35  				row := &bqpb.TextArtifactRow{
    36  					TestId: strings.Repeat("a", 999950),
    37  				}
    38  				So(proto.Size(row), ShouldEqual, 999954)
    39  				rows = append(rows, row)
    40  			}
    41  
    42  			result, err := batch(rows)
    43  			So(err, ShouldBeNil)
    44  			// ~9 MB in the first batch.
    45  			So(result[0], ShouldHaveLength, 9)
    46  			// The rest of the rows in the remaining batch.
    47  			So(result[1], ShouldHaveLength, 1)
    48  		})
    49  		Convey(`Empty`, func() {
    50  			result, err := batch(nil)
    51  			So(err, ShouldBeNil)
    52  			So(result, ShouldHaveLength, 0)
    53  		})
    54  		Convey(`Single row too large`, func() {
    55  			// 10 MB row.
    56  			row := &bqpb.TextArtifactRow{
    57  				TestId: strings.Repeat("a", 10*1024*1024),
    58  			}
    59  			rows := []proto.Message{row}
    60  			_, err := batch(rows)
    61  			So(err, ShouldErrLike, "a single row exceeds the maximum BigQuery AppendRows request size of 9437184 bytes")
    62  		})
    63  	})
    64  }