github.com/m3db/m3@v1.5.0/src/dbnode/storage/lease_verifier_test.go (about) 1 // Copyright (c) 2019 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 "time" 27 28 "github.com/stretchr/testify/require" 29 30 "github.com/m3db/m3/src/dbnode/storage/block" 31 "github.com/m3db/m3/src/x/ident" 32 xtest "github.com/m3db/m3/src/x/test" 33 xtime "github.com/m3db/m3/src/x/time" 34 ) 35 36 var ( 37 testBlockDescriptor = block.LeaseDescriptor{ 38 Namespace: ident.StringID("test-ns"), 39 Shard: 0, 40 BlockStart: xtime.Now().Truncate(2 * time.Hour), 41 } 42 testBlockLeaseState = block.LeaseState{Volume: 0} 43 ) 44 45 func TestLeaseVerifierVerifyLeaseHandlesErrors(t *testing.T) { 46 ctrl := xtest.NewController(t) 47 defer ctrl.Finish() 48 49 var ( 50 mockDB = NewMockDatabase(ctrl) 51 leaseVerifier = NewLeaseVerifier(mockDB) 52 ) 53 mockDB.EXPECT().FlushState( 54 testBlockDescriptor.Namespace, 55 uint32(testBlockDescriptor.Shard), 56 testBlockDescriptor.BlockStart, 57 ).Return(fileOpState{}, errors.New("some-error")) 58 59 err := leaseVerifier.VerifyLease(testBlockDescriptor, testBlockLeaseState) 60 require.Error(t, err) 61 } 62 63 func TestLeaseVerifierVerifyLeaseReturnsErrorIfNotLatestVolume(t *testing.T) { 64 ctrl := xtest.NewController(t) 65 defer ctrl.Finish() 66 67 var ( 68 mockDB = NewMockDatabase(ctrl) 69 leaseVerifier = NewLeaseVerifier(mockDB) 70 ) 71 mockDB.EXPECT().FlushState( 72 testBlockDescriptor.Namespace, 73 uint32(testBlockDescriptor.Shard), 74 testBlockDescriptor.BlockStart, 75 ).Return(fileOpState{ColdVersionFlushed: testBlockLeaseState.Volume + 1}, nil) 76 77 err := leaseVerifier.VerifyLease(testBlockDescriptor, testBlockLeaseState) 78 require.Error(t, err) 79 } 80 81 func TestLeaseVerifierVerifyLeaseSuccessIfVolumeIsLatest(t *testing.T) { 82 ctrl := xtest.NewController(t) 83 defer ctrl.Finish() 84 85 var ( 86 volumeNum = 1 87 state = block.LeaseState{Volume: volumeNum} 88 mockDB = NewMockDatabase(ctrl) 89 leaseVerifier = NewLeaseVerifier(mockDB) 90 ) 91 mockDB.EXPECT().FlushState( 92 testBlockDescriptor.Namespace, 93 uint32(testBlockDescriptor.Shard), 94 testBlockDescriptor.BlockStart, 95 ).Return(fileOpState{ColdVersionFlushed: volumeNum}, nil) 96 97 require.NoError(t, leaseVerifier.VerifyLease(testBlockDescriptor, state)) 98 } 99 100 func TestLeaseVerifierLatestStateHandlesErrors(t *testing.T) { 101 ctrl := xtest.NewController(t) 102 defer ctrl.Finish() 103 104 var ( 105 mockDB = NewMockDatabase(ctrl) 106 leaseVerifier = NewLeaseVerifier(mockDB) 107 ) 108 mockDB.EXPECT().FlushState( 109 testBlockDescriptor.Namespace, 110 uint32(testBlockDescriptor.Shard), 111 testBlockDescriptor.BlockStart, 112 ).Return(fileOpState{}, errors.New("some-error")) 113 114 _, err := leaseVerifier.LatestState(testBlockDescriptor) 115 require.Error(t, err) 116 } 117 118 func TestLeaseVerifierLatestStateSuccess(t *testing.T) { 119 ctrl := xtest.NewController(t) 120 defer ctrl.Finish() 121 122 var ( 123 mockDB = NewMockDatabase(ctrl) 124 leaseVerifier = NewLeaseVerifier(mockDB) 125 ) 126 mockDB.EXPECT().FlushState( 127 testBlockDescriptor.Namespace, 128 uint32(testBlockDescriptor.Shard), 129 testBlockDescriptor.BlockStart, 130 ).Return(fileOpState{ColdVersionFlushed: 1}, nil) 131 132 state, err := leaseVerifier.LatestState(testBlockDescriptor) 133 require.NoError(t, err) 134 require.Equal(t, block.LeaseState{Volume: 1}, state) 135 }