github.com/m3db/m3@v1.5.1-0.20231129193456-75a402aa583b/src/dbnode/storage/tick_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  	"sync"
    26  	"testing"
    27  	"time"
    28  
    29  	"github.com/m3db/m3/src/x/context"
    30  	xtest "github.com/m3db/m3/src/x/test"
    31  	xtime "github.com/m3db/m3/src/x/time"
    32  
    33  	"github.com/golang/mock/gomock"
    34  	"github.com/stretchr/testify/require"
    35  )
    36  
    37  func TestTickManagerTickNormalFlow(t *testing.T) {
    38  	ctrl := xtest.NewController(t)
    39  	defer ctrl.Finish()
    40  
    41  	opts := DefaultTestOptions()
    42  	c := context.NewCancellable()
    43  
    44  	namespace := NewMockdatabaseNamespace(ctrl)
    45  	namespace.EXPECT().Tick(c, gomock.Any())
    46  	db := newMockdatabase(ctrl, namespace)
    47  
    48  	tm := newTickManager(db, opts).(*tickManager)
    49  	tm.c = c
    50  	tm.sleepFn = func(time.Duration) {}
    51  
    52  	require.NoError(t, tm.Tick(noForce, xtime.Now()))
    53  	require.Equal(t, 1, len(tm.tokenCh))
    54  }
    55  
    56  func TestTickManagerTickCancelled(t *testing.T) {
    57  	ctrl := xtest.NewController(t)
    58  	defer ctrl.Finish()
    59  
    60  	var wg sync.WaitGroup
    61  	ch1 := make(chan struct{})
    62  	ch2 := make(chan struct{})
    63  	opts := DefaultTestOptions()
    64  	c := context.NewCancellable()
    65  
    66  	namespace := NewMockdatabaseNamespace(ctrl)
    67  	namespace.EXPECT().Tick(c, gomock.Any()).Do(func(context.Cancellable, xtime.UnixNano) {
    68  		ch1 <- struct{}{}
    69  		<-ch2
    70  	})
    71  	db := newMockdatabase(ctrl, namespace)
    72  
    73  	tm := newTickManager(db, opts).(*tickManager)
    74  	tm.c = c
    75  	tm.sleepFn = func(time.Duration) {}
    76  
    77  	wg.Add(1)
    78  	go func() {
    79  		defer wg.Done()
    80  
    81  		require.Equal(t, errTickCancelled, tm.Tick(noForce, xtime.Now()))
    82  		require.Equal(t, 1, len(tm.tokenCh))
    83  	}()
    84  
    85  	// Wait for tick to start
    86  	<-ch1
    87  	c.Cancel()
    88  	ch2 <- struct{}{}
    89  	wg.Wait()
    90  }
    91  
    92  func TestTickManagerTickErrorFlow(t *testing.T) {
    93  	ctrl := xtest.NewController(t)
    94  	defer ctrl.Finish()
    95  
    96  	opts := DefaultTestOptions()
    97  	c := context.NewCancellable()
    98  
    99  	fakeErr := errors.New("fake error")
   100  	namespace := NewMockdatabaseNamespace(ctrl)
   101  	namespace.EXPECT().Tick(c, gomock.Any()).Return(fakeErr)
   102  	db := newMockdatabase(ctrl, namespace)
   103  
   104  	tm := newTickManager(db, opts).(*tickManager)
   105  	tm.c = c
   106  	tm.sleepFn = func(time.Duration) {}
   107  
   108  	err := tm.Tick(noForce, xtime.Now())
   109  	require.Error(t, err)
   110  	require.Equal(t, fakeErr.Error(), err.Error())
   111  	require.Equal(t, 1, len(tm.tokenCh))
   112  }
   113  
   114  func TestTickManagerNonForcedTickDuringOngoingTick(t *testing.T) {
   115  	ctrl := xtest.NewController(t)
   116  	defer ctrl.Finish()
   117  
   118  	var wg sync.WaitGroup
   119  	ch1 := make(chan struct{})
   120  	ch2 := make(chan struct{})
   121  	opts := DefaultTestOptions()
   122  	c := context.NewCancellable()
   123  
   124  	namespace := NewMockdatabaseNamespace(ctrl)
   125  	namespace.EXPECT().Tick(c, gomock.Any()).Do(func(context.Cancellable, xtime.UnixNano) {
   126  		ch1 <- struct{}{}
   127  		<-ch2
   128  	})
   129  	db := newMockdatabase(ctrl, namespace)
   130  
   131  	tm := newTickManager(db, opts).(*tickManager)
   132  	tm.c = c
   133  	tm.sleepFn = func(time.Duration) {}
   134  
   135  	wg.Add(1)
   136  	go func() {
   137  		defer wg.Done()
   138  
   139  		require.NoError(t, tm.Tick(noForce, xtime.Now()))
   140  	}()
   141  
   142  	// Wait for tick to start
   143  	<-ch1
   144  	require.Equal(t, errTickInProgress, tm.Tick(noForce, xtime.Now()))
   145  
   146  	ch2 <- struct{}{}
   147  	wg.Wait()
   148  
   149  	require.Equal(t, 1, len(tm.tokenCh))
   150  }
   151  
   152  func TestTickManagerForcedTickDuringOngoingTick(t *testing.T) {
   153  	ctrl := xtest.NewController(t)
   154  	defer ctrl.Finish()
   155  
   156  	var wg sync.WaitGroup
   157  	ch1 := make(chan struct{})
   158  	ch2 := make(chan struct{})
   159  	opts := DefaultTestOptions()
   160  	c := context.NewCancellable()
   161  
   162  	namespace := NewMockdatabaseNamespace(ctrl)
   163  	gomock.InOrder(
   164  		namespace.EXPECT().Tick(c, gomock.Any()).Do(func(context.Cancellable, xtime.UnixNano) {
   165  			ch1 <- struct{}{}
   166  			<-ch2
   167  		}),
   168  		namespace.EXPECT().Tick(c, gomock.Any()),
   169  	)
   170  	db := newMockdatabase(ctrl, namespace)
   171  
   172  	tm := newTickManager(db, opts).(*tickManager)
   173  	tm.c = c
   174  	tm.sleepFn = func(time.Duration) {}
   175  
   176  	wg.Add(3)
   177  	go func() {
   178  		defer wg.Done()
   179  
   180  		require.Equal(t, errTickCancelled, tm.Tick(noForce, xtime.Now()))
   181  	}()
   182  
   183  	go func() {
   184  		defer wg.Done()
   185  
   186  		// Wait for tick to start
   187  		<-ch1
   188  		require.NoError(t, tm.Tick(force, xtime.Now()))
   189  	}()
   190  
   191  	go func() {
   192  		defer wg.Done()
   193  
   194  		for !c.IsCancelled() {
   195  		}
   196  		ch2 <- struct{}{}
   197  	}()
   198  
   199  	wg.Wait()
   200  	require.Equal(t, 1, len(tm.tokenCh))
   201  }