github.com/matrixorigin/matrixone@v0.7.0/pkg/lockservice/txn_test.go (about) 1 // Copyright 2023 Matrix Origin 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package lockservice 16 17 import ( 18 "context" 19 "testing" 20 "time" 21 22 "github.com/stretchr/testify/assert" 23 ) 24 25 func TestLockAdded(t *testing.T) { 26 id := []byte("t1") 27 fsp := newFixedSlicePool(2) 28 txn := newActiveTxn(id, string(id), fsp) 29 30 txn.lockAdded(1, [][]byte{[]byte("k1")}) 31 txn.lockAdded(1, [][]byte{[]byte("k11")}) 32 txn.lockAdded(2, [][]byte{[]byte("k2"), []byte("k22")}) 33 34 assert.Equal(t, 2, len(txn.holdLocks)) 35 36 sp := txn.holdLocks[1] 37 s := sp.slice() 38 defer s.unref() 39 assert.Equal(t, 2, s.len()) 40 41 sp2 := txn.holdLocks[2] 42 s2 := sp2.slice() 43 defer s2.unref() 44 assert.Equal(t, 2, s2.len()) 45 } 46 47 func TestClose(t *testing.T) { 48 id := []byte("t1") 49 fsp := newFixedSlicePool(2) 50 txn := newActiveTxn(id, string(id), fsp) 51 tables := map[uint64]*lockTable{ 52 1: newLockTable(1, nil), 53 2: newLockTable(2, nil), 54 } 55 ctx, cancel := context.WithTimeout(context.Background(), time.Hour) 56 defer cancel() 57 assert.NoError(t, tables[1].lock(ctx, txn, [][]byte{[]byte("k1")}, LockOptions{})) 58 assert.NoError(t, tables[2].lock(ctx, txn, [][]byte{[]byte("k2")}, LockOptions{})) 59 60 lockTableFunc := func(table uint64) *lockTable { 61 return tables[table] 62 } 63 txn.close(lockTableFunc) 64 assert.Empty(t, txn.txnID) 65 assert.Empty(t, txn.txnKey) 66 assert.Nil(t, txn.blockedWaiter) 67 assert.Empty(t, txn.holdLocks) 68 assert.Equal(t, 0, tables[1].mu.store.Len()) 69 assert.Equal(t, 0, tables[2].mu.store.Len()) 70 }