github.com/matrixorigin/matrixone@v1.2.0/pkg/txn/storage/mem/log_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 mem
    16  
    17  import (
    18  	"context"
    19  	"testing"
    20  
    21  	"github.com/matrixorigin/matrixone/pkg/logservice"
    22  	logpb "github.com/matrixorigin/matrixone/pkg/pb/logservice"
    23  	"github.com/stretchr/testify/assert"
    24  )
    25  
    26  func TestAppendLog(t *testing.T) {
    27  	l := NewMemLog()
    28  
    29  	n := 100
    30  	for i := 0; i < n; i++ {
    31  		lsn, err := l.Append(context.Background(), logpb.LogRecord{Data: []byte{byte(i)}})
    32  		assert.NoError(t, err)
    33  		assert.Equal(t, uint64(i+1), lsn)
    34  	}
    35  }
    36  
    37  func TestReadLog(t *testing.T) {
    38  	l := NewMemLog()
    39  
    40  	n := 100
    41  	var logs []logpb.LogRecord
    42  	for i := 0; i < n; i++ {
    43  		log := logpb.LogRecord{Data: []byte{byte(i)}}
    44  		lsn, err := l.Append(context.Background(), log)
    45  		assert.NoError(t, err)
    46  		assert.Equal(t, uint64(i+1), lsn)
    47  		log.Lsn = logservice.Lsn(i + 1)
    48  		logs = append(logs, log)
    49  	}
    50  
    51  	readed, lsn, err := l.Read(context.Background(), 1, 0)
    52  	assert.NoError(t, err)
    53  	assert.Equal(t, uint64(1), lsn)
    54  	assert.Equal(t, logs, readed)
    55  
    56  	readed, lsn, err = l.Read(context.Background(), logservice.Lsn(n-1), 0)
    57  	assert.NoError(t, err)
    58  	assert.Equal(t, logservice.Lsn(n-1), lsn)
    59  	assert.Equal(t, logs[n-2:], readed)
    60  
    61  	readed, lsn, err = l.Read(context.Background(), logservice.Lsn(n), 0)
    62  	assert.NoError(t, err)
    63  	assert.Equal(t, logservice.Lsn(n), lsn)
    64  	assert.Equal(t, logs[n-1:], readed)
    65  
    66  	readed, lsn, err = l.Read(context.Background(), logservice.Lsn(n+1), 0)
    67  	assert.NoError(t, err)
    68  	assert.Equal(t, logservice.Lsn(n+1), lsn)
    69  	assert.Empty(t, readed)
    70  }
    71  
    72  func TestTruncateLog(t *testing.T) {
    73  	l := NewMemLog()
    74  
    75  	n := 100
    76  	var logs []logpb.LogRecord
    77  	for i := 0; i < n; i++ {
    78  		log := logpb.LogRecord{Data: []byte{byte(i)}}
    79  		lsn, err := l.Append(context.Background(), log)
    80  		assert.NoError(t, err)
    81  		assert.Equal(t, uint64(i+1), lsn)
    82  		log.Lsn = logservice.Lsn(i + 1)
    83  		logs = append(logs, log)
    84  	}
    85  
    86  	assert.NoError(t, l.Truncate(context.Background(), 10))
    87  	readed, lsn, err := l.Read(context.Background(), 1, 0)
    88  	assert.NoError(t, err)
    89  	assert.Equal(t, uint64(1), lsn)
    90  	assert.Equal(t, logs[10:], readed)
    91  }