github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/pkg/config/large_message_test.go (about) 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 // See the License for the specific language governing permissions and 12 // limitations under the License. 13 14 package config 15 16 import ( 17 "testing" 18 19 "github.com/pingcap/tiflow/pkg/compression" 20 cerror "github.com/pingcap/tiflow/pkg/errors" 21 "github.com/stretchr/testify/require" 22 ) 23 24 func TestLargeMessageHandle4Compression(t *testing.T) { 25 t.Parallel() 26 27 largeMessageHandle := NewDefaultLargeMessageHandleConfig() 28 29 // unsupported compression, return error 30 largeMessageHandle.LargeMessageHandleCompression = "zstd" 31 32 err := largeMessageHandle.AdjustAndValidate(ProtocolCanalJSON, false) 33 require.ErrorIs(t, err, cerror.ErrInvalidReplicaConfig) 34 35 largeMessageHandle.LargeMessageHandleCompression = compression.LZ4 36 err = largeMessageHandle.AdjustAndValidate(ProtocolCanalJSON, false) 37 require.NoError(t, err) 38 39 largeMessageHandle.LargeMessageHandleCompression = compression.Snappy 40 err = largeMessageHandle.AdjustAndValidate(ProtocolCanalJSON, false) 41 require.NoError(t, err) 42 43 largeMessageHandle.LargeMessageHandleCompression = compression.None 44 err = largeMessageHandle.AdjustAndValidate(ProtocolCanalJSON, false) 45 require.NoError(t, err) 46 } 47 48 func TestLargeMessageHandle4NotSupportedProtocol(t *testing.T) { 49 t.Parallel() 50 51 largeMessageHandle := NewDefaultLargeMessageHandleConfig() 52 53 err := largeMessageHandle.AdjustAndValidate(ProtocolCanal, true) 54 require.NoError(t, err) 55 56 largeMessageHandle.LargeMessageHandleOption = LargeMessageHandleOptionHandleKeyOnly 57 err = largeMessageHandle.AdjustAndValidate(ProtocolCanal, true) 58 require.ErrorIs(t, err, cerror.ErrInvalidReplicaConfig) 59 } 60 61 func TestLargeMessageHandle4CanalJSON(t *testing.T) { 62 t.Parallel() 63 64 // large-message-handle not set, always no error 65 largeMessageHandle := NewDefaultLargeMessageHandleConfig() 66 67 err := largeMessageHandle.AdjustAndValidate(ProtocolCanalJSON, false) 68 require.NoError(t, err) 69 require.True(t, largeMessageHandle.Disabled()) 70 71 for _, option := range []string{ 72 LargeMessageHandleOptionHandleKeyOnly, 73 LargeMessageHandleOptionClaimCheck, 74 } { 75 largeMessageHandle.LargeMessageHandleOption = option 76 if option == LargeMessageHandleOptionClaimCheck { 77 largeMessageHandle.ClaimCheckStorageURI = "file:///tmp/claim-check" 78 } 79 80 // `enable-tidb-extension` is false, return error 81 err := largeMessageHandle.AdjustAndValidate(ProtocolCanalJSON, false) 82 require.ErrorIs(t, err, cerror.ErrInvalidReplicaConfig) 83 84 // `enable-tidb-extension` is true, no error 85 err = largeMessageHandle.AdjustAndValidate(ProtocolCanalJSON, true) 86 require.NoError(t, err) 87 require.Equal(t, option, largeMessageHandle.LargeMessageHandleOption) 88 } 89 } 90 91 func TestLargeMessageHandle4OpenProtocol(t *testing.T) { 92 t.Parallel() 93 94 // large-message-handle not set, always no error 95 largeMessageHandle := NewDefaultLargeMessageHandleConfig() 96 97 err := largeMessageHandle.AdjustAndValidate(ProtocolOpen, false) 98 require.NoError(t, err) 99 require.True(t, largeMessageHandle.Disabled()) 100 101 for _, o := range []string{ 102 LargeMessageHandleOptionHandleKeyOnly, 103 LargeMessageHandleOptionClaimCheck, 104 } { 105 largeMessageHandle.LargeMessageHandleOption = o 106 if o == LargeMessageHandleOptionClaimCheck { 107 largeMessageHandle.ClaimCheckStorageURI = "file:///tmp/claim-check" 108 } 109 110 // `enable-tidb-extension` is false, return error 111 err := largeMessageHandle.AdjustAndValidate(ProtocolOpen, false) 112 require.NoError(t, err) 113 114 // `enable-tidb-extension` is true, no error 115 err = largeMessageHandle.AdjustAndValidate(ProtocolOpen, true) 116 require.NoError(t, err) 117 require.Equal(t, o, largeMessageHandle.LargeMessageHandleOption) 118 119 } 120 }