github.com/m3db/m3@v1.5.0/src/dbnode/storage/entry_whitebox_test.go (about)

     1  // Copyright (c) 2018 Uber Technologies, Inc.
     2  //
     3  // Permission is hereby granted, free of charge, to any person obtaining a copy
     4  // of this software and associated documentation files (the "Software"), to deal
     5  // in the Software without restriction, including without limitation the rights
     6  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     7  // copies of the Software, and to permit persons to whom the Software is
     8  // furnished to do so, subject to the following conditions:
     9  //
    10  // The above copyright notice and this permission notice shall be included in
    11  // all copies or substantial portions of the Software.
    12  //
    13  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    14  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    15  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    16  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    17  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    18  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    19  // THE SOFTWARE.
    20  
    21  package storage
    22  
    23  import (
    24  	"testing"
    25  	"time"
    26  
    27  	"github.com/golang/mock/gomock"
    28  	"github.com/stretchr/testify/require"
    29  
    30  	"github.com/m3db/m3/src/dbnode/storage/index"
    31  	"github.com/m3db/m3/src/dbnode/storage/series"
    32  	"github.com/m3db/m3/src/dbnode/ts/writes"
    33  	"github.com/m3db/m3/src/m3ninx/doc"
    34  	"github.com/m3db/m3/src/x/context"
    35  	"github.com/m3db/m3/src/x/ident"
    36  	xtest "github.com/m3db/m3/src/x/test"
    37  	xtime "github.com/m3db/m3/src/x/time"
    38  )
    39  
    40  func TestEntryIndexAttemptRotatesSlice(t *testing.T) {
    41  	ctrl := xtest.NewController(t)
    42  	defer ctrl.Finish()
    43  
    44  	e := NewEntry(NewEntryOptions{Series: newMockSeries(ctrl)})
    45  	for i := 0; i < 10; i++ {
    46  		ti := newTime(i)
    47  		require.True(t, e.NeedsIndexUpdate(ti))
    48  		require.Equal(t, i+1, e.IndexedBlockCount())
    49  	}
    50  
    51  	// ensure only the latest ones are held on to
    52  	for i := 9; i >= 7; i-- {
    53  		ti := newTime(i)
    54  		require.False(t, e.NeedsIndexUpdate(ti))
    55  	}
    56  }
    57  
    58  func TestEntryIndexSeriesRef(t *testing.T) {
    59  	ctrl := gomock.NewController(t)
    60  	defer ctrl.Finish()
    61  
    62  	now := time.Now()
    63  	blockStart := newTime(0)
    64  	mockIndexWriter := NewMockIndexWriter(ctrl)
    65  	mockIndexWriter.EXPECT().BlockStartForWriteTime(blockStart).
    66  		Return(blockStart).
    67  		Times(2)
    68  
    69  	mockSeries := series.NewMockDatabaseSeries(ctrl)
    70  	mockSeries.EXPECT().ID().Return(ident.StringID("foo"))
    71  	mockSeries.EXPECT().Metadata().Return(doc.Metadata{})
    72  	mockSeries.EXPECT().Write(
    73  		context.NewBackground(),
    74  		blockStart,
    75  		1.0,
    76  		xtime.Second,
    77  		nil,
    78  		series.WriteOptions{},
    79  	).Return(true, series.WarmWrite, nil)
    80  
    81  	e := NewEntry(NewEntryOptions{
    82  		Series:      mockSeries,
    83  		IndexWriter: mockIndexWriter,
    84  		NowFn: func() time.Time {
    85  			return now
    86  		},
    87  	})
    88  
    89  	mockIndexWriter.EXPECT().WritePending([]writes.PendingIndexInsert{
    90  		{
    91  			Entry: index.WriteBatchEntry{
    92  				Timestamp:     blockStart,
    93  				OnIndexSeries: e,
    94  				EnqueuedAt:    now,
    95  			},
    96  			Document: doc.Metadata{},
    97  		},
    98  	}).Return(nil)
    99  
   100  	ok, _, err := e.Write(
   101  		context.NewBackground(),
   102  		blockStart,
   103  		1.0,
   104  		xtime.Second,
   105  		nil,
   106  		series.WriteOptions{},
   107  	)
   108  	require.True(t, ok)
   109  	require.NoError(t, err)
   110  }