github.com/m3db/m3@v1.5.1-0.20231129193456-75a402aa583b/src/dbnode/storage/mediator_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  	"testing"
    25  	"time"
    26  
    27  	xclock "github.com/m3db/m3/src/x/clock"
    28  	xtest "github.com/m3db/m3/src/x/test"
    29  
    30  	"github.com/golang/mock/gomock"
    31  	"github.com/stretchr/testify/require"
    32  	"go.uber.org/atomic"
    33  )
    34  
    35  func TestDatabaseMediatorOpenClose(t *testing.T) {
    36  	ctrl := xtest.NewController(t)
    37  	defer ctrl.Finish()
    38  
    39  	opts := DefaultTestOptions().SetRepairEnabled(false)
    40  	now := time.Now()
    41  	opts = opts.
    42  		SetBootstrapProcessProvider(nil).
    43  		SetClockOptions(opts.ClockOptions().SetNowFn(func() time.Time {
    44  			return now
    45  		})).
    46  		SetInstrumentOptions(opts.InstrumentOptions().SetReportInterval(time.Millisecond))
    47  
    48  	db := NewMockdatabase(ctrl)
    49  	db.EXPECT().Options().Return(opts).AnyTimes()
    50  	db.EXPECT().OwnedNamespaces().Return(nil, nil).AnyTimes()
    51  	db.EXPECT().BootstrapState().Return(DatabaseBootstrapState{}).AnyTimes()
    52  	db.EXPECT().IsBootstrappedAndDurable().Return(true).AnyTimes()
    53  	m, err := newMediator(db, nil, opts)
    54  	require.NoError(t, err)
    55  
    56  	var started, reported atomic.Bool
    57  
    58  	backgroundProcess := NewMockBackgroundProcess(ctrl)
    59  	backgroundProcess.EXPECT().Report().Do(func() {
    60  		reported.Store(true)
    61  	}).AnyTimes()
    62  	gomock.InOrder(
    63  		backgroundProcess.EXPECT().Start().Do(func() {
    64  			started.Store(true)
    65  		}),
    66  		backgroundProcess.EXPECT().Stop(),
    67  	)
    68  
    69  	require.NoError(t, m.RegisterBackgroundProcess(backgroundProcess))
    70  
    71  	require.Equal(t, errMediatorNotOpen, m.Close())
    72  
    73  	require.NoError(t, m.Open())
    74  	require.Equal(t, errMediatorAlreadyOpen, m.Open())
    75  	require.Equal(t, errMediatorAlreadyOpen, m.RegisterBackgroundProcess(backgroundProcess))
    76  
    77  	xclock.WaitUntil(func() bool {
    78  		return started.Load() && reported.Load()
    79  	}, time.Second)
    80  	require.True(t, started.Load(), "failed to start")
    81  	require.True(t, reported.Load(), "failed to report")
    82  
    83  	require.NoError(t, m.Close())
    84  	require.Equal(t, errMediatorAlreadyClosed, m.Close())
    85  }
    86  
    87  func TestDatabaseMediatorDisableFileOpsAndWait(t *testing.T) {
    88  	ctrl := xtest.NewController(t)
    89  	defer ctrl.Finish()
    90  
    91  	opts := DefaultTestOptions().SetRepairEnabled(false)
    92  	now := time.Now()
    93  	opts = opts.
    94  		SetBootstrapProcessProvider(nil).
    95  		SetClockOptions(opts.ClockOptions().SetNowFn(func() time.Time {
    96  			return now
    97  		}))
    98  
    99  	db := NewMockdatabase(ctrl)
   100  	db.EXPECT().Options().Return(opts).AnyTimes()
   101  	med, err := newMediator(db, nil, opts)
   102  	require.NoError(t, err)
   103  
   104  	m := med.(*mediator)
   105  	fsm := NewMockdatabaseFileSystemManager(ctrl)
   106  	m.databaseFileSystemManager = fsm
   107  	var slept []time.Duration
   108  	m.sleepFn = func(d time.Duration) { slept = append(slept, d) }
   109  
   110  	gomock.InOrder(
   111  		fsm.EXPECT().Disable().Return(fileOpInProgress),
   112  		fsm.EXPECT().Status().Return(fileOpInProgress),
   113  		fsm.EXPECT().Status().Return(fileOpInProgress),
   114  		fsm.EXPECT().Status().Return(fileOpNotStarted),
   115  	)
   116  
   117  	m.DisableFileOpsAndWait()
   118  	require.Equal(t, 3, len(slept))
   119  }