From ac9775cfc9a3e57009ec06da1d3f17cbd167b50f Mon Sep 17 00:00:00 2001 From: CharlesCheung Date: Wed, 9 Mar 2022 17:07:42 +0800 Subject: [PATCH] add unit test --- cdc/capture/capture_test.go | 79 +++++++++++++++++++++++++++++++++++++ cdc/capture/main_test.go | 31 +++++++++++++++ 2 files changed, 110 insertions(+) create mode 100644 cdc/capture/capture_test.go create mode 100644 cdc/capture/main_test.go diff --git a/cdc/capture/capture_test.go b/cdc/capture/capture_test.go new file mode 100644 index 00000000000..975380d43a4 --- /dev/null +++ b/cdc/capture/capture_test.go @@ -0,0 +1,79 @@ +// 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 capture + +import ( + "context" + "sync" + "testing" + "time" + + "github.com/pingcap/tiflow/pkg/etcd" + "github.com/stretchr/testify/require" + "go.etcd.io/etcd/clientv3" + "go.etcd.io/etcd/pkg/logutil" + "go.uber.org/zap" + "go.uber.org/zap/zapcore" + "google.golang.org/grpc" + "google.golang.org/grpc/backoff" +) + +func TestReset(t *testing.T) { + ctx, cancel := context.WithCancel(context.Background()) + + // init etcd mocker + clientURL, etcdServer, err := etcd.SetupEmbedEtcd(t.TempDir()) + require.Nil(t, err) + logConfig := logutil.DefaultZapLoggerConfig + logConfig.Level = zap.NewAtomicLevelAt(zapcore.ErrorLevel) + + etcdCli, err := clientv3.New(clientv3.Config{ + Endpoints: []string{clientURL.String()}, + Context: ctx, + LogConfig: &logConfig, + DialTimeout: 3 * time.Second, + DialOptions: []grpc.DialOption{ + grpc.WithBlock(), + grpc.WithConnectParams(grpc.ConnectParams{ + Backoff: backoff.Config{ + BaseDelay: time.Second, + Multiplier: 1.1, + Jitter: 0.1, + MaxDelay: 3 * time.Second, + }, + MinConnectTimeout: 3 * time.Second, + }), + }, + }) + require.NoError(t, err) + client := etcd.NewCDCEtcdClient(context.TODO(), etcdCli) + + cp := NewCapture4Test(nil) + cp.EtcdClient = &client + + // simulate network isolation scenarios + etcdServer.Close() + wg := sync.WaitGroup{} + wg.Add(1) + go func() { + err = cp.reset(ctx) + require.Regexp(t, ".*context canceled.*", err) + wg.Done() + }() + time.Sleep(100 * time.Millisecond) + info := cp.Info() + require.NotNil(t, info) + cancel() + wg.Wait() +} diff --git a/cdc/capture/main_test.go b/cdc/capture/main_test.go new file mode 100644 index 00000000000..b4b60acdccb --- /dev/null +++ b/cdc/capture/main_test.go @@ -0,0 +1,31 @@ +// Copyright 2022 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 capture + +import ( + "testing" + + "github.com/pingcap/tiflow/pkg/leakutil" + "go.uber.org/goleak" +) + +func TestMain(m *testing.M) { + opts := []goleak.Option{ + // TODO: temporarily ignore leaking goroutines + goleak.IgnoreTopFunction("google.golang.org/grpc.(*ccBalancerWrapper).watcher"), + goleak.IgnoreTopFunction("google.golang.org/grpc.(*addrConn).resetTransport"), + } + + leakutil.SetUpLeakTest(m, opts...) +}