github.com/matrixorigin/matrixone@v0.7.0/pkg/vm/engine/tae/buffer/buffer_test.go (about)

     1  // Copyright 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 buffer
    16  
    17  import (
    18  	"testing"
    19  
    20  	"github.com/matrixorigin/matrixone/pkg/vm/engine/tae/common"
    21  	"github.com/matrixorigin/matrixone/pkg/vm/engine/tae/testutils"
    22  	"github.com/stretchr/testify/assert"
    23  )
    24  
    25  type testNodeHandle struct {
    26  	Node
    27  	t *testing.T
    28  }
    29  
    30  func (h *testNodeHandle) load() {
    31  	id := h.key.(common.ID)
    32  	h.t.Logf("Load %s", id.BlockString())
    33  }
    34  func (h *testNodeHandle) unload() {
    35  	id := h.key.(common.ID)
    36  	h.t.Logf("Unload %s", id.BlockString())
    37  }
    38  func (h *testNodeHandle) destroy() {
    39  	id := h.key.(common.ID)
    40  	h.t.Logf("Destroy %s", id.BlockString())
    41  }
    42  
    43  func newTestNodeHandle(mgr *nodeManager, id common.ID, size uint64, t *testing.T) *testNodeHandle {
    44  	n := &testNodeHandle{
    45  		t: t,
    46  	}
    47  	n.Node = *NewNode(n, mgr, id, size)
    48  	n.LoadFunc = n.load
    49  	n.UnloadFunc = n.unload
    50  	n.DestroyFunc = n.destroy
    51  	return n
    52  }
    53  
    54  func TestHandle(t *testing.T) {
    55  	defer testutils.AfterTest(t)()
    56  	maxsize := uint64(100)
    57  	evicter := NewSimpleEvictHolder()
    58  	mgr := NewNodeManager(maxsize, evicter)
    59  	baseId := common.ID{}
    60  	id1 := baseId.NextBlock()
    61  	id2 := baseId.NextBlock()
    62  	id3 := baseId.NextBlock()
    63  	id4 := baseId.NextBlock()
    64  	sz1, sz2, sz3, sz4 := uint64(30), uint64(30), uint64(50), uint64(80)
    65  	n1 := newTestNodeHandle(mgr, id1, sz1, t)
    66  	n2 := newTestNodeHandle(mgr, id2, sz2, t)
    67  	n3 := newTestNodeHandle(mgr, id3, sz3, t)
    68  	n4 := newTestNodeHandle(mgr, id4, sz4, t)
    69  	mgr.RegisterNode(n1)
    70  	mgr.RegisterNode(n2)
    71  	mgr.RegisterNode(n3)
    72  	mgr.RegisterNode(n4)
    73  	assert.Equal(t, 4, mgr.Count())
    74  
    75  	h1 := mgr.Pin(n1)
    76  	assert.NotNil(t, h1)
    77  	h2 := mgr.Pin(n2)
    78  	assert.NotNil(t, h2)
    79  	assert.Equal(t, sz1+sz2, mgr.Total())
    80  	h3 := mgr.Pin(n3)
    81  	assert.Nil(t, h3)
    82  	h1.Close()
    83  	h3 = mgr.Pin(n3)
    84  	assert.NotNil(t, h3)
    85  	assert.Equal(t, sz2+sz3, mgr.Total())
    86  
    87  	h2.Close()
    88  	h3.Close()
    89  	h4 := mgr.Pin(n4)
    90  	assert.NotNil(t, h4)
    91  	assert.Equal(t, sz4, mgr.Total())
    92  
    93  	t.Log(mgr.String())
    94  
    95  	h4.Close()
    96  	h3 = mgr.Pin(n3)
    97  	assert.NotNil(t, h3)
    98  	h3.Close()
    99  	h1 = mgr.Pin(n1)
   100  	err := h1.GetNode().Expand(uint64(100), nil)
   101  	assert.NotNil(t, err)
   102  	err = h1.GetNode().Expand(uint64(60), nil)
   103  	assert.Nil(t, err)
   104  	h2 = mgr.Pin(n2)
   105  	assert.Nil(t, h2)
   106  	t.Log(mgr.String())
   107  
   108  	n1.Close()
   109  	n2.Close()
   110  	n3.Close()
   111  	n4.Close()
   112  	assert.Equal(t, uint64(0), mgr.Total())
   113  	t.Log(mgr.String())
   114  }