github.com/hasnat/dolt/go@v0.0.0-20210628190320-9eb5d843fbb7/store/nbs/persisting_chunk_source_test.go (about)

     1  // Copyright 2019 Dolthub, Inc.
     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  // This file incorporates work covered by the following copyright and
    16  // permission notice:
    17  //
    18  // Copyright 2016 Attic Labs, Inc. All rights reserved.
    19  // Licensed under the Apache License, version 2.0:
    20  // http://www.apache.org/licenses/LICENSE-2.0
    21  
    22  package nbs
    23  
    24  import (
    25  	"context"
    26  	"testing"
    27  
    28  	"github.com/stretchr/testify/assert"
    29  	"github.com/stretchr/testify/require"
    30  )
    31  
    32  func TestPersistingChunkStoreEmpty(t *testing.T) {
    33  	mt := newMemTable(testMemTableSize)
    34  	ccs := newPersistingChunkSource(context.Background(), mt, nil, newFakeTablePersister(), make(chan struct{}, 1), &Stats{})
    35  
    36  	h, err := ccs.hash()
    37  	require.NoError(t, err)
    38  	assert.Equal(t, addr{}, h)
    39  	assert.Zero(t, mustUint32(ccs.count()))
    40  }
    41  
    42  type pausingFakeTablePersister struct {
    43  	tablePersister
    44  	trigger <-chan struct{}
    45  }
    46  
    47  func (ftp pausingFakeTablePersister) Persist(ctx context.Context, mt *memTable, haver chunkReader, stats *Stats) (chunkSource, error) {
    48  	<-ftp.trigger
    49  	return ftp.tablePersister.Persist(context.Background(), mt, haver, stats)
    50  }
    51  
    52  func TestPersistingChunkStore(t *testing.T) {
    53  	assert := assert.New(t)
    54  	mt := newMemTable(testMemTableSize)
    55  
    56  	for _, c := range testChunks {
    57  		assert.True(mt.addChunk(computeAddr(c), c))
    58  	}
    59  
    60  	trigger := make(chan struct{})
    61  	ccs := newPersistingChunkSource(context.Background(), mt, nil, pausingFakeTablePersister{newFakeTablePersister(), trigger}, make(chan struct{}, 1), &Stats{})
    62  
    63  	assertChunksInReader(testChunks, ccs, assert)
    64  	assert.EqualValues(mustUint32(mt.count()), mustUint32(ccs.getReader().count()))
    65  	close(trigger)
    66  
    67  	h, err := ccs.hash()
    68  	assert.NoError(err)
    69  	assert.NotEqual(addr{}, h)
    70  	assert.EqualValues(len(testChunks), mustUint32(ccs.count()))
    71  	assertChunksInReader(testChunks, ccs, assert)
    72  
    73  	assert.Nil(ccs.mt)
    74  
    75  	newChunk := []byte("additional")
    76  	mt.addChunk(computeAddr(newChunk), newChunk)
    77  	assert.NotEqual(mustUint32(mt.count()), mustUint32(ccs.count()))
    78  	assert.False(ccs.has(computeAddr(newChunk)))
    79  }