github.com/grailbio/bigslice@v0.0.0-20230519005545-30c4c12152ad/archive/tarslice/tarslice_test.go (about)

     1  // Copyright 2019 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 tarslice_test
     6  
     7  import (
     8  	"archive/tar"
     9  	"bytes"
    10  	"fmt"
    11  	"io"
    12  	"io/ioutil"
    13  	"math/rand"
    14  	"sort"
    15  	"testing"
    16  
    17  	"github.com/grailbio/base/must"
    18  	"github.com/grailbio/bigslice/archive/tarslice"
    19  	"github.com/grailbio/bigslice/slicetest"
    20  )
    21  
    22  func TestReader(t *testing.T) {
    23  	const N = 1000
    24  
    25  	var buf bytes.Buffer
    26  	w := tar.NewWriter(&buf)
    27  
    28  	rnd := rand.New(rand.NewSource(1))
    29  	p := make([]byte, 256)
    30  	for i := 0; i < N; i++ {
    31  		n := rnd.Intn(256)
    32  		must.Nil(w.WriteHeader(&tar.Header{
    33  			Name: fmt.Sprintf("%03d", i),
    34  			Size: int64(n),
    35  		}))
    36  		for j := 0; j < n; j++ {
    37  			p[j] = byte(n)
    38  		}
    39  		_, err := w.Write(p[:n])
    40  		must.Nil(err)
    41  	}
    42  	must.Nil(w.Close())
    43  
    44  	slice := tarslice.Reader(10, func() (io.ReadCloser, error) { return ioutil.NopCloser(bytes.NewReader(buf.Bytes())), nil })
    45  	var entries []tarslice.Entry
    46  	slicetest.RunAndScan(t, slice, &entries)
    47  	if got, want := len(entries), N; got != want {
    48  		t.Fatalf("got %v, want %v", got, want)
    49  	}
    50  	sort.Slice(entries, func(i, j int) bool { return entries[i].Name < entries[j].Name })
    51  	for i, entry := range entries {
    52  		if got, want := entry.Name, fmt.Sprintf("%03d", i); got != want {
    53  			t.Errorf("entry %d: got %v, want %v", i, got, want)
    54  		}
    55  		n := len(entry.Body)
    56  		for _, b := range entry.Body {
    57  			if got, want := b, byte(n); got != want {
    58  				t.Errorf("got %v, want %v", got, want)
    59  			}
    60  		}
    61  	}
    62  }