Skip to content

Commit

Permalink
test(pkg/config): migrate test-infra to testify for pkg/config pkg (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
CharlesCheung96 authored Oct 8, 2021
1 parent 2acd011 commit e3e8329
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 49 deletions.
87 changes: 38 additions & 49 deletions pkg/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,50 +16,43 @@ package config
import (
"testing"

"github.com/pingcap/check"
"github.com/pingcap/ticdc/pkg/util/testleak"
"github.com/stretchr/testify/require"
)

func Test(t *testing.T) { check.TestingT(t) }

type replicaConfigSuite struct{}

var _ = check.Suite(&replicaConfigSuite{})

func (s *replicaConfigSuite) TestMarshal(c *check.C) {
defer testleak.AfterTest(c)()
func TestReplicaConfigMarshal(t *testing.T) {
t.Parallel()
conf := GetDefaultReplicaConfig()
conf.CaseSensitive = false
conf.ForceReplicate = true
conf.Filter.Rules = []string{"1.1"}
conf.Mounter.WorkerNum = 3
b, err := conf.Marshal()
c.Assert(err, check.IsNil)
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}}`)
require.Nil(t, err)
require.Equal(t, `{"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}}`, b)
conf2 := new(ReplicaConfig)
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}}`))
c.Assert(err, check.IsNil)
c.Assert(conf2, check.DeepEquals, conf)
require.Nil(t, err)
require.Equal(t, conf, conf2)
}

func (s *replicaConfigSuite) TestClone(c *check.C) {
defer testleak.AfterTest(c)()
func TestReplicaConfigClone(t *testing.T) {
t.Parallel()
conf := GetDefaultReplicaConfig()
conf.CaseSensitive = false
conf.ForceReplicate = true
conf.Filter.Rules = []string{"1.1"}
conf.Mounter.WorkerNum = 3
conf2 := conf.Clone()
c.Assert(conf2, check.DeepEquals, conf)
require.Equal(t, conf, conf2)
conf2.Mounter.WorkerNum = 4
c.Assert(conf.Mounter.WorkerNum, check.Equals, 3)
require.Equal(t, 3, conf.Mounter.WorkerNum)
}

func (s *replicaConfigSuite) TestOutDated(c *check.C) {
defer testleak.AfterTest(c)()
func TestReplicaConfigOutDated(t *testing.T) {
t.Parallel()
conf2 := new(ReplicaConfig)
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}}`))
c.Assert(err, check.IsNil)
require.Nil(t, err)

conf := GetDefaultReplicaConfig()
conf.CaseSensitive = false
Expand All @@ -71,62 +64,58 @@ func (s *replicaConfigSuite) TestOutDated(c *check.C) {
{Matcher: []string{"a.c"}, Dispatcher: "r2"},
{Matcher: []string{"a.d"}, Dispatcher: "r2"},
}
c.Assert(conf2, check.DeepEquals, conf)
require.Equal(t, conf, conf2)
}

type serverConfigSuite struct{}

var _ = check.Suite(&serverConfigSuite{})

func (s *serverConfigSuite) TestMarshal(c *check.C) {
defer testleak.AfterTest(c)()
func TestServerConfigMarshal(t *testing.T) {
t.Parallel()
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}}`

conf := GetDefaultServerConfig()
conf.Addr = "192.155.22.33:8887"
conf.Sorter.ChunkSizeLimit = 999
b, err := conf.Marshal()
c.Assert(err, check.IsNil)
require.Nil(t, err)

c.Assert(b, check.Equals, rawConfig)
require.Equal(t, rawConfig, b)
conf2 := new(ServerConfig)
err = conf2.Unmarshal([]byte(rawConfig))
c.Assert(err, check.IsNil)
c.Assert(conf2, check.DeepEquals, conf)
require.Nil(t, err)
require.Equal(t, conf, conf2)
}

func (s *serverConfigSuite) TestClone(c *check.C) {
defer testleak.AfterTest(c)()
func TestServerConfigClone(t *testing.T) {
t.Parallel()
conf := GetDefaultServerConfig()
conf.Addr = "192.155.22.33:8887"
conf.Sorter.ChunkSizeLimit = 999
conf2 := conf.Clone()
c.Assert(conf2, check.DeepEquals, conf)
require.Equal(t, conf, conf2)
conf.Sorter.ChunkSizeLimit = 99
c.Assert(conf.Sorter.ChunkSizeLimit, check.Equals, uint64(99))
require.Equal(t, uint64(99), conf.Sorter.ChunkSizeLimit)
}

func (s *serverConfigSuite) TestValidateAndAdjust(c *check.C) {
defer testleak.AfterTest(c)()
func TestServerConfigValidateAndAdjust(t *testing.T) {
t.Parallel()
conf := new(ServerConfig)

c.Assert(conf.ValidateAndAdjust(), check.ErrorMatches, ".*empty address")
require.Regexp(t, ".*empty address", conf.ValidateAndAdjust())
conf.Addr = "cdc:1234"
c.Assert(conf.ValidateAndAdjust(), check.ErrorMatches, ".*empty GC TTL is not allowed")
require.Regexp(t, ".*empty GC TTL is not allowed", conf.ValidateAndAdjust())
conf.GcTTL = 60
c.Assert(conf.ValidateAndAdjust(), check.IsNil)
c.Assert(conf.AdvertiseAddr, check.Equals, conf.Addr)
require.Nil(t, conf.ValidateAndAdjust())
require.Equal(t, conf.Addr, conf.AdvertiseAddr)
conf.AdvertiseAddr = "advertise:1234"
c.Assert(conf.ValidateAndAdjust(), check.IsNil)
c.Assert(conf.Addr, check.Equals, "cdc:1234")
c.Assert(conf.AdvertiseAddr, check.Equals, "advertise:1234")
require.Nil(t, conf.ValidateAndAdjust())
require.Equal(t, "cdc:1234", conf.Addr)
require.Equal(t, "advertise:1234", conf.AdvertiseAddr)
conf.AdvertiseAddr = "0.0.0.0:1234"
c.Assert(conf.ValidateAndAdjust(), check.ErrorMatches, ".*must be specified.*")
require.Regexp(t, ".*must be specified.*", conf.ValidateAndAdjust())
conf.Addr = "0.0.0.0:1234"
c.Assert(conf.ValidateAndAdjust(), check.ErrorMatches, ".*must be specified.*")
require.Regexp(t, ".*must be specified.*", conf.ValidateAndAdjust())
conf.AdvertiseAddr = "advertise"
c.Assert(conf.ValidateAndAdjust(), check.ErrorMatches, ".*does not contain a port")
require.Regexp(t, ".*does not contain a port", conf.ValidateAndAdjust())
conf.AdvertiseAddr = "advertise:1234"
conf.PerTableMemoryQuota = 1
c.Assert(conf.ValidateAndAdjust(), check.ErrorMatches, ".*should be at least.*")
require.Regexp(t, ".*should be at least.*", conf.ValidateAndAdjust())
}
24 changes: 24 additions & 0 deletions pkg/config/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Copyright 2021 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// See the License for the specific language governing permissions and
// limitations under the License.

package config

import (
"testing"

"github.com/pingcap/ticdc/pkg/leakutil"
)

func TestMain(m *testing.M) {
leakutil.SetUpLeakTest(m)
}
1 change: 1 addition & 0 deletions pkg/config/sorter.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ type SorterConfig struct {
SortDir string `toml:"sort-dir" json:"sort-dir"`
}

// ValidateAndAdjust validates and adjusts the sorter configuration
func (c *SorterConfig) ValidateAndAdjust() error {
if c.ChunkSizeLimit < 1*1024*1024 {
return cerror.ErrIllegalUnifiedSorterParameter.GenWithStackByArgs("chunk-size-limit should be at least 1MB")
Expand Down

0 comments on commit e3e8329

Please sign in to comment.