github.com/matrixorigin/matrixone@v1.2.0/pkg/vm/engine/tae/rpc/handle_test.go (about)

     1  // Copyright 2021 - 2022 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 rpc
    16  
    17  import (
    18  	"testing"
    19  	"time"
    20  
    21  	"github.com/matrixorigin/matrixone/pkg/vm/engine/tae/common"
    22  	"github.com/stretchr/testify/require"
    23  )
    24  
    25  func TestHandleGCCache(t *testing.T) {
    26  	now := time.Now()
    27  	expired := now.Add(-MAX_TXN_COMMIT_LATENCY).Add(-time.Second)
    28  
    29  	handle := Handle{}
    30  	handle.txnCtxs = common.NewMap[string, *txnContext](10)
    31  	handle.txnCtxs.Store("now", &txnContext{
    32  		deadline: now,
    33  	})
    34  	handle.txnCtxs.Store("expired", &txnContext{
    35  		deadline: expired,
    36  	})
    37  	handle.GCCache(now)
    38  
    39  	cnt := 0
    40  	handle.txnCtxs.Range(func(key string, value *txnContext) bool {
    41  		cnt++
    42  		return true
    43  	})
    44  
    45  	require.Equal(t, 1, cnt)
    46  	_, ok := handle.txnCtxs.Load("expired")
    47  	require.False(t, ok)
    48  	_, ok = handle.txnCtxs.Load("now")
    49  	require.True(t, ok)
    50  }