github.com/grailbio/base@v0.0.11/mapio/merged_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 mapio
     6  
     7  import (
     8  	"bytes"
     9  	"testing"
    10  )
    11  
    12  type mergedSeeker struct{ Merged }
    13  
    14  func (m mergedSeeker) Seek(key []byte) Scanner { return m.Merged.Seek(key) }
    15  
    16  func TestMerged(t *testing.T) {
    17  	const (
    18  		N = 10000
    19  		M = 10
    20  	)
    21  	var (
    22  		entries = makeEntries(N)
    23  		buffers = make([]bytes.Buffer, M)
    24  		writers = make([]*Writer, M)
    25  	)
    26  	for i := range writers {
    27  		writers[i] = NewWriter(&buffers[i], BlockSize(1024))
    28  	}
    29  	for i := range entries {
    30  		writers[i%M].Append(entries[i].Key, entries[i].Value)
    31  	}
    32  	for i := range writers {
    33  		if err := writers[i].Close(); err != nil {
    34  			t.Fatal(i, err)
    35  		}
    36  	}
    37  	merged := make(Merged, M)
    38  	for i := range buffers {
    39  		var err error
    40  		merged[i], err = New(bytes.NewReader(buffers[i].Bytes()))
    41  		if err != nil {
    42  			t.Fatal(i, err)
    43  		}
    44  	}
    45  
    46  	testSeeker(t, entries, mergedSeeker{merged})
    47  }