github.com/grailbio/bigslice@v0.0.0-20230519005545-30c4c12152ad/exec/buffer_test.go (about)

     1  // Copyright 2018 GRAIL, Inc. All rights reserved.
     2  // Use of this source code is governed by the Apache 2.0
     3  // license that can be found in the LICENSE file.
     4  
     5  package exec
     6  
     7  import (
     8  	"context"
     9  	"reflect"
    10  	"testing"
    11  
    12  	fuzz "github.com/google/gofuzz"
    13  	"github.com/grailbio/bigslice/frame"
    14  	"github.com/grailbio/bigslice/sliceio"
    15  	"github.com/grailbio/bigslice/slicetype"
    16  )
    17  
    18  var typeOfString = reflect.TypeOf("")
    19  
    20  func TestTaskBuffer(t *testing.T) {
    21  	var batches [][]string
    22  	fz := fuzz.New()
    23  	fz.NilChance(0)
    24  	fz.Fuzz(&batches)
    25  	b := make(taskBuffer, 1)
    26  	for _, batch := range batches {
    27  		b[0] = append(b[0], frame.Slices(batch))
    28  	}
    29  	s := sliceio.NewScanner(slicetype.New(typeOfString), b.Reader(0))
    30  	var (
    31  		i   int
    32  		str string
    33  		q   = batches
    34  	)
    35  	ctx := context.Background()
    36  	for s.Scan(ctx, &str) {
    37  		for len(q) > 0 && len(q[0]) == 0 {
    38  			q = q[1:]
    39  		}
    40  		if len(q) == 0 {
    41  			t.Error("long read")
    42  			break
    43  		}
    44  		if got, want := str, q[0][0]; got != want {
    45  			t.Errorf("element %d: got %v, want %v", i, got, want)
    46  		}
    47  		q[0] = q[0][1:]
    48  		i++
    49  	}
    50  	if err := s.Err(); err != nil {
    51  		t.Error(err)
    52  	}
    53  }