github.com/m3db/m3@v1.5.1-0.20231129193456-75a402aa583b/src/dbnode/namespace/namespace_watch_test.go (about)

     1  // Copyright (c) 2017 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 namespace
    22  
    23  import (
    24  	"testing"
    25  	"time"
    26  
    27  	"github.com/m3db/m3/src/x/instrument"
    28  
    29  	"github.com/fortytw2/leaktest"
    30  	"github.com/golang/mock/gomock"
    31  	"github.com/stretchr/testify/require"
    32  	"github.com/uber-go/tally"
    33  )
    34  
    35  func newTestNamespaceWatch(t *testing.T, ctrl *gomock.Controller, updater NamespaceUpdater) (
    36  	NamespaceWatch,
    37  	*MockWatch,
    38  ) {
    39  	testScope := tally.NewTestScope("", nil)
    40  	iopts := instrument.NewOptions().
    41  		SetMetricsScope(testScope).
    42  		SetReportInterval(10 * time.Millisecond)
    43  
    44  	mockWatch := NewMockWatch(ctrl)
    45  
    46  	return NewNamespaceWatch(updater, mockWatch, iopts), mockWatch
    47  }
    48  
    49  func TestNamespaceWatchStartStop(t *testing.T) {
    50  	ctrl := gomock.NewController(t)
    51  	ch := make(chan struct{}, 1)
    52  	defer func() {
    53  		ctrl.Finish()
    54  		close(ch)
    55  		leaktest.CheckTimeout(t, time.Second)()
    56  	}()
    57  
    58  	noopUpdater := func(Map) error {return nil}
    59  	dbWatch, mockWatch := newTestNamespaceWatch(t, ctrl, noopUpdater)
    60  
    61  	mockWatch.EXPECT().C().Return(ch).AnyTimes()
    62  	// start and stop
    63  	require.NoError(t, dbWatch.Start())
    64  	require.NoError(t, dbWatch.Stop())
    65  
    66  	// start and close
    67  	require.NoError(t, dbWatch.Start())
    68  	require.NoError(t, dbWatch.Close())
    69  }
    70  
    71  func TestNamespaceWatchStopWithoutStart(t *testing.T) {
    72  	ctrl := gomock.NewController(t)
    73  	ch := make(chan struct{}, 1)
    74  	defer func() {
    75  		ctrl.Finish()
    76  		close(ch)
    77  		leaktest.CheckTimeout(t, time.Second)()
    78  	}()
    79  
    80  	noopUpdater := func(Map) error {return nil}
    81  	dbWatch, _ := newTestNamespaceWatch(t, ctrl, noopUpdater)
    82  	require.Error(t, dbWatch.Stop())
    83  	require.NoError(t, dbWatch.Close())
    84  }
    85  
    86  func TestNamespaceWatchUpdatePropagation(t *testing.T) {
    87  	ctrl := gomock.NewController(t)
    88  	ch := make(chan struct{}, 1)
    89  	defer func() {
    90  		ctrl.Finish()
    91  		close(ch)
    92  		leaktest.CheckTimeout(t, time.Second)()
    93  	}()
    94  
    95  	mockMap := NewMockMap(ctrl)
    96  	mockMap.EXPECT().Metadatas().Return([]Metadata{}).AnyTimes()
    97  
    98  	noopUpdater := func(Map) error {return nil}
    99  	dbWatch, mockWatch := newTestNamespaceWatch(t, ctrl, noopUpdater)
   100  	mockWatch.EXPECT().Get().Return(mockMap).AnyTimes()
   101  	mockWatch.EXPECT().C().Return(ch).AnyTimes()
   102  	require.NoError(t, dbWatch.Start())
   103  
   104  	ch <- struct{}{}
   105  	time.Sleep(100 * time.Millisecond) // give test a chance to schedule pending go-routines
   106  	require.NoError(t, dbWatch.Stop())
   107  }