github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/cdc/redo/reader/file_test.go (about) 1 // Copyright 2021 PingCAP, 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 reader 15 16 import ( 17 "context" 18 "fmt" 19 "io" 20 "net/url" 21 "testing" 22 23 "github.com/pingcap/log" 24 "github.com/pingcap/tiflow/pkg/redo" 25 "github.com/stretchr/testify/require" 26 ) 27 28 func TestReaderNewReader(t *testing.T) { 29 _, err := newReaders(context.Background(), nil) 30 require.NotNil(t, err) 31 32 dir := t.TempDir() 33 require.Panics(t, func() { 34 _, err = newReaders(context.Background(), &readerConfig{dir: dir}) 35 }) 36 } 37 38 func TestFileReaderRead(t *testing.T) { 39 dir := t.TempDir() 40 ctx, cancel := context.WithCancel(context.Background()) 41 defer cancel() 42 43 uri, err := url.Parse(fmt.Sprintf("file://%s", dir)) 44 require.NoError(t, err) 45 cfg := &readerConfig{ 46 dir: t.TempDir(), 47 startTs: 10, 48 endTs: 12, 49 fileType: redo.RedoRowLogFileType, 50 uri: *uri, 51 useExternalStorage: true, 52 } 53 // log file with maxCommitTs<=startTs, fileter when download file 54 genLogFile(ctx, t, dir, redo.RedoRowLogFileType, 1, cfg.startTs) 55 // normal log file, include [10, 11, 12] and [11, 12, ... 20] 56 genLogFile(ctx, t, dir, redo.RedoRowLogFileType, cfg.startTs, cfg.endTs+2) 57 genLogFile(ctx, t, dir, redo.RedoRowLogFileType, cfg.endTs-1, 20) 58 // log file with minCommitTs>endTs, filtered when sort file 59 genLogFile(ctx, t, dir, redo.RedoRowLogFileType, 2000, 2023) 60 61 log.Info("start to read redo log files") 62 readers, err := newReaders(ctx, cfg) 63 require.NoError(t, err) 64 require.Equal(t, 2, len(readers)) 65 defer readers[0].Close() //nolint:errcheck 66 67 for _, r := range readers { 68 log, err := r.Read() 69 require.NoError(t, err) 70 require.EqualValues(t, 11, log.RedoRow.Row.CommitTs) 71 log, err = r.Read() 72 require.NoError(t, err) 73 require.EqualValues(t, 12, log.RedoRow.Row.CommitTs) 74 log, err = r.Read() 75 require.Nil(t, log) 76 require.ErrorIs(t, err, io.EOF) 77 require.NoError(t, r.Close()) 78 } 79 }