github.com/matrixorigin/matrixone@v1.2.0/pkg/txn/client/leak_checker_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 client 16 17 import ( 18 "context" 19 "testing" 20 "time" 21 22 "github.com/matrixorigin/matrixone/pkg/common/runtime" 23 "github.com/stretchr/testify/assert" 24 "github.com/stretchr/testify/require" 25 ) 26 27 func TestLeakCheck(t *testing.T) { 28 ctx, cancel := context.WithTimeout(context.Background(), time.Second) 29 defer cancel() 30 31 runtime.SetupProcessLevelRuntime(runtime.DefaultRuntime()) 32 ts := newTestTxnSender() 33 34 cc := make(chan struct{}) 35 c := NewTxnClient(ts, 36 WithEnableLeakCheck( 37 time.Millisecond*200, 38 func([]ActiveTxn) { 39 close(cc) 40 })) 41 c.Resume() 42 _, err := c.New(ctx, newTestTimestamp(0)) 43 assert.Nil(t, err) 44 <-cc 45 require.NoError(t, c.Close()) 46 } 47 48 func TestLeakCheckWithNoLeak(t *testing.T) { 49 ctx, cancel := context.WithTimeout(context.Background(), time.Second) 50 defer cancel() 51 52 runtime.SetupProcessLevelRuntime(runtime.DefaultRuntime()) 53 ts := newTestTxnSender() 54 55 n := 0 56 c := NewTxnClient(ts, 57 WithEnableLeakCheck( 58 time.Millisecond*200, 59 func([]ActiveTxn) { 60 n++ 61 })) 62 c.Resume() 63 op, err := c.New(ctx, newTestTimestamp(0)) 64 require.NoError(t, err) 65 require.NoError(t, op.Rollback(ctx)) 66 time.Sleep(time.Millisecond * 200 * 3) 67 assert.Equal(t, 0, n) 68 lc := c.(*txnClient).leakChecker 69 lc.Lock() 70 assert.Equal(t, 0, len(lc.actives)) 71 lc.Unlock() 72 require.NoError(t, c.Close()) 73 }