github.com/pingcap/ticdc@v0.0.0-20220526033649-485a10ef2652/pkg/config/config_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 config 15 16 import ( 17 "testing" 18 19 "github.com/pingcap/check" 20 "github.com/pingcap/ticdc/pkg/util/testleak" 21 ) 22 23 func Test(t *testing.T) { check.TestingT(t) } 24 25 type replicaConfigSuite struct{} 26 27 var _ = check.Suite(&replicaConfigSuite{}) 28 29 func (s *replicaConfigSuite) TestMarshal(c *check.C) { 30 defer testleak.AfterTest(c)() 31 conf := GetDefaultReplicaConfig() 32 conf.CaseSensitive = false 33 conf.ForceReplicate = true 34 conf.Filter.Rules = []string{"1.1"} 35 conf.Mounter.WorkerNum = 3 36 b, err := conf.Marshal() 37 c.Assert(err, check.IsNil) 38 c.Assert(b, check.Equals, `{"case-sensitive":false,"enable-old-value":true,"force-replicate":true,"check-gc-safe-point":true,"filter":{"rules":["1.1"],"ignore-txn-start-ts":null},"mounter":{"worker-num":3},"sink":{"dispatchers":null,"protocol":"default"},"cyclic-replication":{"enable":false,"replica-id":0,"filter-replica-ids":null,"id-buckets":0,"sync-ddl":false},"scheduler":{"type":"table-number","polling-time":-1}}`) 39 conf2 := new(ReplicaConfig) 40 err = conf2.Unmarshal([]byte(`{"case-sensitive":false,"enable-old-value":true,"force-replicate":true,"check-gc-safe-point":true,"filter":{"rules":["1.1"],"ignore-txn-start-ts":null},"mounter":{"worker-num":3},"sink":{"dispatchers":null,"protocol":"default"},"cyclic-replication":{"enable":false,"replica-id":0,"filter-replica-ids":null,"id-buckets":0,"sync-ddl":false},"scheduler":{"type":"table-number","polling-time":-1}}`)) 41 c.Assert(err, check.IsNil) 42 c.Assert(conf2, check.DeepEquals, conf) 43 } 44 45 func (s *replicaConfigSuite) TestClone(c *check.C) { 46 defer testleak.AfterTest(c)() 47 conf := GetDefaultReplicaConfig() 48 conf.CaseSensitive = false 49 conf.ForceReplicate = true 50 conf.Filter.Rules = []string{"1.1"} 51 conf.Mounter.WorkerNum = 3 52 conf2 := conf.Clone() 53 c.Assert(conf2, check.DeepEquals, conf) 54 conf2.Mounter.WorkerNum = 4 55 c.Assert(conf.Mounter.WorkerNum, check.Equals, 3) 56 } 57 58 func (s *replicaConfigSuite) TestOutDated(c *check.C) { 59 defer testleak.AfterTest(c)() 60 conf2 := new(ReplicaConfig) 61 err := conf2.Unmarshal([]byte(`{"case-sensitive":false,"enable-old-value":true,"force-replicate":true,"check-gc-safe-point":true,"filter":{"rules":["1.1"],"ignore-txn-start-ts":null,"ddl-allow-list":null},"mounter":{"worker-num":3},"sink":{"dispatch-rules":[{"db-name":"a","tbl-name":"b","rule":"r1"},{"db-name":"a","tbl-name":"c","rule":"r2"},{"db-name":"a","tbl-name":"d","rule":"r2"}],"protocol":"default"},"cyclic-replication":{"enable":false,"replica-id":0,"filter-replica-ids":null,"id-buckets":0,"sync-ddl":false},"scheduler":{"type":"table-number","polling-time":-1}}`)) 62 c.Assert(err, check.IsNil) 63 64 conf := GetDefaultReplicaConfig() 65 conf.CaseSensitive = false 66 conf.ForceReplicate = true 67 conf.Filter.Rules = []string{"1.1"} 68 conf.Mounter.WorkerNum = 3 69 conf.Sink.DispatchRules = []*DispatchRule{ 70 {Matcher: []string{"a.b"}, Dispatcher: "r1"}, 71 {Matcher: []string{"a.c"}, Dispatcher: "r2"}, 72 {Matcher: []string{"a.d"}, Dispatcher: "r2"}, 73 } 74 c.Assert(conf2, check.DeepEquals, conf) 75 } 76 77 type serverConfigSuite struct{} 78 79 var _ = check.Suite(&serverConfigSuite{}) 80 81 func (s *serverConfigSuite) TestMarshal(c *check.C) { 82 defer testleak.AfterTest(c)() 83 rawConfig := `{"addr":"192.155.22.33:8887","advertise-addr":"","log-file":"","log-level":"info","log":{"file":{"max-size":300,"max-days":0,"max-backups":0}},"data-dir":"","gc-ttl":86400,"tz":"System","capture-session-ttl":10,"owner-flush-interval":200000000,"processor-flush-interval":100000000,"sorter":{"num-concurrent-worker":4,"chunk-size-limit":999,"max-memory-percentage":30,"max-memory-consumption":17179869184,"num-workerpool-goroutine":16,"sort-dir":"/tmp/sorter"},"security":{"ca-path":"","cert-path":"","key-path":"","cert-allowed-cn":null},"per-table-memory-quota":20971520,"kv-client":{"worker-concurrent":8,"worker-pool-size":0,"region-scan-limit":40}}` 84 85 conf := GetDefaultServerConfig() 86 conf.Addr = "192.155.22.33:8887" 87 conf.Sorter.ChunkSizeLimit = 999 88 b, err := conf.Marshal() 89 c.Assert(err, check.IsNil) 90 91 c.Assert(b, check.Equals, rawConfig) 92 conf2 := new(ServerConfig) 93 err = conf2.Unmarshal([]byte(rawConfig)) 94 c.Assert(err, check.IsNil) 95 c.Assert(conf2, check.DeepEquals, conf) 96 } 97 98 func (s *serverConfigSuite) TestClone(c *check.C) { 99 defer testleak.AfterTest(c)() 100 conf := GetDefaultServerConfig() 101 conf.Addr = "192.155.22.33:8887" 102 conf.Sorter.ChunkSizeLimit = 999 103 conf2 := conf.Clone() 104 c.Assert(conf2, check.DeepEquals, conf) 105 conf.Sorter.ChunkSizeLimit = 99 106 c.Assert(conf.Sorter.ChunkSizeLimit, check.Equals, uint64(99)) 107 } 108 109 func (s *serverConfigSuite) TestValidateAndAdjust(c *check.C) { 110 defer testleak.AfterTest(c)() 111 conf := new(ServerConfig) 112 113 c.Assert(conf.ValidateAndAdjust(), check.ErrorMatches, ".*empty address") 114 conf.Addr = "cdc:1234" 115 c.Assert(conf.ValidateAndAdjust(), check.ErrorMatches, ".*empty GC TTL is not allowed") 116 conf.GcTTL = 60 117 c.Assert(conf.ValidateAndAdjust(), check.IsNil) 118 c.Assert(conf.AdvertiseAddr, check.Equals, conf.Addr) 119 conf.AdvertiseAddr = "advertise:1234" 120 c.Assert(conf.ValidateAndAdjust(), check.IsNil) 121 c.Assert(conf.Addr, check.Equals, "cdc:1234") 122 c.Assert(conf.AdvertiseAddr, check.Equals, "advertise:1234") 123 conf.AdvertiseAddr = "0.0.0.0:1234" 124 c.Assert(conf.ValidateAndAdjust(), check.ErrorMatches, ".*must be specified.*") 125 conf.Addr = "0.0.0.0:1234" 126 c.Assert(conf.ValidateAndAdjust(), check.ErrorMatches, ".*must be specified.*") 127 conf.AdvertiseAddr = "advertise" 128 c.Assert(conf.ValidateAndAdjust(), check.ErrorMatches, ".*does not contain a port") 129 conf.AdvertiseAddr = "advertise:1234" 130 conf.PerTableMemoryQuota = 1 131 c.Assert(conf.ValidateAndAdjust(), check.ErrorMatches, ".*should be at least.*") 132 }