github.com/matrixorigin/matrixone@v0.7.0/pkg/fileservice/rc_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 fileservice
    16  
    17  import (
    18  	"testing"
    19  
    20  	"github.com/stretchr/testify/assert"
    21  )
    22  
    23  func TestRC(t *testing.T) {
    24  	l := NewLRU(1)
    25  
    26  	r := NewRC(42)
    27  	r.IncRef()
    28  	l.Set(1, r, 1)
    29  	_, ok := l.kv[1]
    30  	assert.True(t, ok)
    31  
    32  	l.Set(2, 42, 1)
    33  	_, ok = l.kv[1]
    34  	assert.True(t, ok)
    35  	_, ok = l.kv[2]
    36  	assert.False(t, ok)
    37  
    38  	r.DecRef()
    39  	l.Set(2, 42, 1)
    40  	_, ok = l.kv[1]
    41  	assert.False(t, ok)
    42  	_, ok = l.kv[2]
    43  	assert.True(t, ok)
    44  
    45  	r2 := NewRC(42)
    46  	r2.IncRef()
    47  	l.Set(3, r2, 1)
    48  	_, ok = l.kv[3]
    49  	assert.True(t, ok)
    50  	_, ok = l.kv[2]
    51  	assert.False(t, ok)
    52  
    53  }