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

     1  // Copyright (c) 2016 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  	"errors"
    25  	"testing"
    26  
    27  	"github.com/golang/mock/gomock"
    28  	"github.com/stretchr/testify/require"
    29  
    30  	"github.com/m3db/m3/src/x/instrument"
    31  	xtest "github.com/m3db/m3/src/x/test"
    32  	xtime "github.com/m3db/m3/src/x/time"
    33  )
    34  
    35  func TestFileSystemManagerShouldRunDuringBootstrap(t *testing.T) {
    36  	ctrl := xtest.NewController(t)
    37  	defer ctrl.Finish()
    38  
    39  	database := newMockdatabase(ctrl)
    40  	fsm := newFileSystemManager(database, nil, DefaultTestOptions())
    41  	mgr := fsm.(*fileSystemManager)
    42  
    43  	database.EXPECT().IsBootstrapped().Return(false).Times(2)
    44  	require.False(t, mgr.shouldRunWithLock())
    45  	require.False(t, mgr.Run(xtime.Now()))
    46  
    47  	database.EXPECT().IsBootstrapped().Return(true)
    48  	require.True(t, mgr.shouldRunWithLock())
    49  }
    50  
    51  func TestFileSystemManagerShouldRunWhileRunning(t *testing.T) {
    52  	ctrl := xtest.NewController(t)
    53  	defer ctrl.Finish()
    54  	database := newMockdatabase(ctrl)
    55  	fsm := newFileSystemManager(database, nil, DefaultTestOptions())
    56  	mgr := fsm.(*fileSystemManager)
    57  	database.EXPECT().IsBootstrapped().Return(true)
    58  	require.True(t, mgr.shouldRunWithLock())
    59  	mgr.status = fileOpInProgress
    60  	require.False(t, mgr.shouldRunWithLock())
    61  }
    62  
    63  func TestFileSystemManagerShouldRunEnableDisable(t *testing.T) {
    64  	ctrl := xtest.NewController(t)
    65  	defer ctrl.Finish()
    66  	database := newMockdatabase(ctrl)
    67  	fsm := newFileSystemManager(database, nil, DefaultTestOptions())
    68  	mgr := fsm.(*fileSystemManager)
    69  	database.EXPECT().IsBootstrapped().Return(true).AnyTimes()
    70  	require.True(t, mgr.shouldRunWithLock())
    71  	require.NotEqual(t, fileOpInProgress, mgr.Disable())
    72  	require.False(t, mgr.shouldRunWithLock())
    73  	mgr.Enable()
    74  	require.True(t, mgr.shouldRunWithLock())
    75  }
    76  
    77  func TestFileSystemManagerRunCleanupPanic(t *testing.T) {
    78  	ctrl := xtest.NewController(t)
    79  	defer ctrl.Finish()
    80  	database := newMockdatabase(ctrl)
    81  	database.EXPECT().IsBootstrapped().Return(true).AnyTimes()
    82  
    83  	fm := NewMockdatabaseFlushManager(ctrl)
    84  	cm := NewMockdatabaseCleanupManager(ctrl)
    85  	fsm := newFileSystemManager(database, nil, DefaultTestOptions())
    86  	mgr := fsm.(*fileSystemManager)
    87  	mgr.databaseFlushManager = fm
    88  	mgr.databaseCleanupManager = cm
    89  
    90  	ts := xtime.Now()
    91  	gomock.InOrder(
    92  		cm.EXPECT().WarmFlushCleanup(ts).Return(errors.New("foo")),
    93  	)
    94  
    95  	defer instrument.SetShouldPanicEnvironmentVariable(true)()
    96  	require.Panics(t, func() { mgr.Run(ts) })
    97  }
    98  
    99  func TestFileSystemManagerRunFlushPanic(t *testing.T) {
   100  	ctrl := xtest.NewController(t)
   101  	defer ctrl.Finish()
   102  	database := newMockdatabase(ctrl)
   103  	database.EXPECT().IsBootstrapped().Return(true).AnyTimes()
   104  
   105  	fm := NewMockdatabaseFlushManager(ctrl)
   106  	cm := NewMockdatabaseCleanupManager(ctrl)
   107  	fsm := newFileSystemManager(database, nil, DefaultTestOptions())
   108  	mgr := fsm.(*fileSystemManager)
   109  	mgr.databaseFlushManager = fm
   110  	mgr.databaseCleanupManager = cm
   111  
   112  	ts := xtime.Now()
   113  	gomock.InOrder(
   114  		cm.EXPECT().WarmFlushCleanup(ts).Return(nil),
   115  		fm.EXPECT().Flush(ts).Return(errors.New("flush error")),
   116  	)
   117  
   118  	defer instrument.SetShouldPanicEnvironmentVariable(true)()
   119  	require.Panics(t, func() { mgr.Run(ts) })
   120  }