|
| 1 | +// Copyright 2023 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 | +// 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 kv |
| 16 | + |
| 17 | +import ( |
| 18 | + "os" |
| 19 | + "path/filepath" |
| 20 | + "strings" |
| 21 | + "testing" |
| 22 | + |
| 23 | + "github.com/pingcap/tidb/br/pkg/lightning/backend/encode" |
| 24 | + "github.com/pingcap/tidb/br/pkg/lightning/log" |
| 25 | + "github.com/pingcap/tidb/parser/model" |
| 26 | + "github.com/pingcap/tidb/parser/mysql" |
| 27 | + "github.com/pingcap/tidb/table" |
| 28 | + "github.com/pingcap/tidb/table/tables" |
| 29 | + "github.com/pingcap/tidb/types" |
| 30 | + "github.com/stretchr/testify/require" |
| 31 | +) |
| 32 | + |
| 33 | +func TestLogKVConvertFailed(t *testing.T) { |
| 34 | + tempPath := filepath.Join(t.TempDir(), "/temp.txt") |
| 35 | + logCfg := &log.Config{File: tempPath, FileMaxSize: 1} |
| 36 | + err := log.InitLogger(logCfg, "info") |
| 37 | + require.NoError(t, err) |
| 38 | + |
| 39 | + modelName := model.NewCIStr("c1") |
| 40 | + modelState := model.StatePublic |
| 41 | + modelFieldType := *types.NewFieldType(mysql.TypeTiny) |
| 42 | + c1 := &model.ColumnInfo{ID: 1, Name: modelName, State: modelState, Offset: 0, FieldType: modelFieldType} |
| 43 | + cols := []*model.ColumnInfo{c1} |
| 44 | + tblInfo := &model.TableInfo{ID: 1, Columns: cols, PKIsHandle: false, State: model.StatePublic} |
| 45 | + var tbl table.Table |
| 46 | + tbl, err = tables.TableFromMeta(NewPanickingAllocators(0), tblInfo) |
| 47 | + require.NoError(t, err) |
| 48 | + |
| 49 | + var baseKVEncoder *BaseKVEncoder |
| 50 | + baseKVEncoder, err = NewBaseKVEncoder(&encode.EncodingConfig{ |
| 51 | + Table: tbl, |
| 52 | + SessionOptions: encode.SessionOptions{ |
| 53 | + SQLMode: mysql.ModeStrictAllTables, |
| 54 | + Timestamp: 1234567890, |
| 55 | + }, |
| 56 | + Logger: log.L(), |
| 57 | + }) |
| 58 | + var newString strings.Builder |
| 59 | + for i := 0; i < 100000; i++ { |
| 60 | + newString.WriteString("test_test_test_test_") |
| 61 | + } |
| 62 | + newDatum := types.NewStringDatum(newString.String()) |
| 63 | + rows := []types.Datum{} |
| 64 | + for i := 0; i <= 10; i++ { |
| 65 | + rows = append(rows, newDatum) |
| 66 | + } |
| 67 | + err = baseKVEncoder.LogKVConvertFailed(rows, 6, c1, err) |
| 68 | + require.NoError(t, err) |
| 69 | + |
| 70 | + var content []byte |
| 71 | + content, err = os.ReadFile(tempPath) |
| 72 | + require.NoError(t, err) |
| 73 | + require.LessOrEqual(t, 500, len(string(content))) |
| 74 | + require.NotContains(t, content, "exceeds maximum file size") |
| 75 | +} |
0 commit comments