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

     1  // Copyright (c) 2021 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 permits
    22  
    23  import (
    24  	stdctx "context"
    25  	"testing"
    26  
    27  	"github.com/stretchr/testify/require"
    28  
    29  	"github.com/m3db/m3/src/x/context"
    30  	"github.com/m3db/m3/src/x/instrument"
    31  )
    32  
    33  func TestFixedPermits(t *testing.T) {
    34  	ctx := context.NewBackground()
    35  	iOpts := instrument.NewOptions()
    36  	fp, err := NewFixedPermitsManager(3, 1, iOpts).NewPermits(ctx)
    37  	require.NoError(t, err)
    38  
    39  	expectedP := NewPermit(1, iOpts)
    40  	expectedP.(*permit).refCount.Inc()
    41  
    42  	r, err := fp.Acquire(ctx)
    43  	require.NoError(t, err)
    44  	p := r.Permit
    45  	require.Equal(t, expectedP, p)
    46  
    47  	r, err = fp.Acquire(ctx)
    48  	require.NoError(t, err)
    49  	p = r.Permit
    50  	require.Equal(t, expectedP, p)
    51  
    52  	r, err = fp.Acquire(ctx)
    53  	require.NoError(t, err)
    54  	p = r.Permit
    55  	require.Equal(t, expectedP, p)
    56  
    57  	tryP, err := fp.TryAcquire(ctx)
    58  	require.NoError(t, err)
    59  	require.Nil(t, tryP)
    60  
    61  	fp.Release(p)
    62  
    63  	r, err = fp.Acquire(ctx)
    64  	require.NoError(t, err)
    65  	p = r.Permit
    66  	require.Equal(t, expectedP, p)
    67  }
    68  
    69  func TestPanics(t *testing.T) {
    70  	ctx := context.NewBackground()
    71  	iOpts := instrument.NewOptions()
    72  
    73  	fp, err := NewFixedPermitsManager(3, 1, iOpts).NewPermits(ctx)
    74  	require.NoError(t, err)
    75  
    76  	r, err := fp.Acquire(ctx)
    77  	require.NoError(t, err)
    78  	p := r.Permit
    79  
    80  	fp.Release(p)
    81  
    82  	defer instrument.SetShouldPanicEnvironmentVariable(true)()
    83  	require.Panics(t, func() { fp.Release(p) })
    84  }
    85  
    86  func TestFixedPermitsTimeouts(t *testing.T) {
    87  	ctx := context.NewBackground()
    88  	iOpts := instrument.NewOptions()
    89  
    90  	fp, err := NewFixedPermitsManager(1, 1, iOpts).NewPermits(ctx)
    91  	require.NoError(t, err)
    92  	expectedP := NewPermit(1, iOpts)
    93  	expectedP.(*permit).refCount.Inc()
    94  
    95  	r, err := fp.Acquire(ctx)
    96  	require.NoError(t, err)
    97  	p := r.Permit
    98  	require.Equal(t, expectedP, p)
    99  
   100  	tryP, err := fp.TryAcquire(ctx)
   101  	require.NoError(t, err)
   102  	require.Nil(t, tryP)
   103  
   104  	stdCtx, cancel := stdctx.WithCancel(stdctx.Background())
   105  	cancel()
   106  	ctx = context.NewWithGoContext(stdCtx)
   107  
   108  	fp.Release(p)
   109  
   110  	_, err = fp.Acquire(ctx)
   111  	require.Error(t, err)
   112  
   113  	_, err = fp.TryAcquire(ctx)
   114  	require.Error(t, err)
   115  }