github.com/whtcorpsinc/milevadb-prod@v0.0.0-20211104133533-f57f4be3b597/interlock/apply_cache_test.go (about)

     1  // Copyright 2020 WHTCORPS INC, Inc.
     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  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  package interlock
    15  
    16  import (
    17  	"strconv"
    18  	"strings"
    19  
    20  	. "github.com/whtcorpsinc/check"
    21  	"github.com/whtcorpsinc/BerolinaSQL/allegrosql"
    22  	"github.com/whtcorpsinc/milevadb/types"
    23  	"github.com/whtcorpsinc/milevadb/soliton/chunk"
    24  	"github.com/whtcorpsinc/milevadb/soliton/mock"
    25  )
    26  
    27  var _ = SerialSuites(&testApplyCacheSuite{})
    28  
    29  type testApplyCacheSuite struct {
    30  }
    31  
    32  func (s *testApplyCacheSuite) TestApplyCache(c *C) {
    33  	ctx := mock.NewContext()
    34  	ctx.GetStochastikVars().NestedLoopJoinCacheCapacity = 100
    35  	applyCache, err := newApplyCache(ctx)
    36  	c.Assert(err, IsNil)
    37  
    38  	fields := []*types.FieldType{types.NewFieldType(allegrosql.TypeLonglong)}
    39  	value := make([]*chunk.List, 3)
    40  	key := make([][]byte, 3)
    41  	for i := 0; i < 3; i++ {
    42  		value[i] = chunk.NewList(fields, 1, 1)
    43  		srcChunk := chunk.NewChunkWithCapacity(fields, 1)
    44  		srcChunk.AppendInt64(0, int64(i))
    45  		srcEvent := srcChunk.GetEvent(0)
    46  		value[i].AppendEvent(srcEvent)
    47  		key[i] = []byte(strings.Repeat(strconv.Itoa(i), 100))
    48  
    49  		// TODO: *chunk.List.GetMemTracker().BytesConsumed() is not accurate, fix it later.
    50  		c.Assert(applyCacheKVMem(key[i], value[i]), Equals, int64(100))
    51  	}
    52  
    53  	ok, err := applyCache.Set(key[0], value[0])
    54  	c.Assert(err, IsNil)
    55  	c.Assert(ok, Equals, true)
    56  	result, err := applyCache.Get(key[0])
    57  	c.Assert(err, IsNil)
    58  	c.Assert(result, NotNil)
    59  
    60  	ok, err = applyCache.Set(key[1], value[1])
    61  	c.Assert(err, IsNil)
    62  	c.Assert(ok, Equals, true)
    63  	result, err = applyCache.Get(key[1])
    64  	c.Assert(err, IsNil)
    65  	c.Assert(result, NotNil)
    66  
    67  	ok, err = applyCache.Set(key[2], value[2])
    68  	c.Assert(err, IsNil)
    69  	c.Assert(ok, Equals, true)
    70  	result, err = applyCache.Get(key[2])
    71  	c.Assert(err, IsNil)
    72  	c.Assert(result, NotNil)
    73  
    74  	// Both key[0] and key[1] are not in the cache
    75  	result, err = applyCache.Get(key[0])
    76  	c.Assert(err, IsNil)
    77  	c.Assert(result, IsNil)
    78  
    79  	result, err = applyCache.Get(key[1])
    80  	c.Assert(err, IsNil)
    81  	c.Assert(result, IsNil)
    82  }