github.com/pingcap/ticdc@v0.0.0-20220526033649-485a10ef2652/cdc/server_test.go (about)

     1  // Copyright 2020 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 cdc
    15  
    16  import (
    17  	"context"
    18  	"net/url"
    19  	"path/filepath"
    20  	"time"
    21  
    22  	"github.com/pingcap/check"
    23  	"github.com/pingcap/ticdc/pkg/config"
    24  	"github.com/pingcap/ticdc/pkg/etcd"
    25  	"github.com/pingcap/ticdc/pkg/util"
    26  	"github.com/pingcap/ticdc/pkg/util/testleak"
    27  	"go.etcd.io/etcd/embed"
    28  	"golang.org/x/sync/errgroup"
    29  )
    30  
    31  type serverSuite struct {
    32  	e         *embed.Etcd
    33  	clientURL *url.URL
    34  	ctx       context.Context
    35  	cancel    context.CancelFunc
    36  	errg      *errgroup.Group
    37  }
    38  
    39  func (s *serverSuite) SetUpTest(c *check.C) {
    40  	dir := c.MkDir()
    41  	var err error
    42  	s.clientURL, s.e, err = etcd.SetupEmbedEtcd(dir)
    43  	c.Assert(err, check.IsNil)
    44  	s.ctx, s.cancel = context.WithCancel(context.Background())
    45  	s.errg = util.HandleErrWithErrGroup(s.ctx, s.e.Err(), func(e error) { c.Log(e) })
    46  }
    47  
    48  func (s *serverSuite) TearDownTest(c *check.C) {
    49  	s.e.Close()
    50  	s.cancel()
    51  	err := s.errg.Wait()
    52  	if err != nil {
    53  		c.Errorf("Error group error: %s", err)
    54  	}
    55  }
    56  
    57  var _ = check.Suite(&serverSuite{})
    58  
    59  func (s *serverSuite) TestEtcdHealthChecker(c *check.C) {
    60  	defer testleak.AfterTest(c)()
    61  	defer s.TearDownTest(c)
    62  
    63  	ctx, cancel := context.WithCancel(context.Background())
    64  	pdEndpoints := []string{
    65  		"http://" + s.clientURL.Host,
    66  		"http://invalid-pd-host:2379",
    67  	}
    68  	conf := config.GetDefaultServerConfig()
    69  	conf.Addr = advertiseAddr4Test
    70  	conf.AdvertiseAddr = advertiseAddr4Test
    71  	config.StoreGlobalServerConfig(conf)
    72  	server, err := NewServer(pdEndpoints)
    73  	c.Assert(err, check.IsNil)
    74  	c.Assert(server, check.NotNil)
    75  
    76  	s.errg.Go(func() error {
    77  		err := server.etcdHealthChecker(ctx)
    78  		c.Assert(err, check.Equals, context.Canceled)
    79  		return nil
    80  	})
    81  	// longer than one check tick 3s
    82  	time.Sleep(time.Second * 4)
    83  	cancel()
    84  }
    85  
    86  func (s *serverSuite) TestInitDataDir(c *check.C) {
    87  	defer testleak.AfterTest(c)()
    88  	defer s.TearDownTest(c)
    89  
    90  	ctx, cancel := context.WithCancel(context.Background())
    91  	pdEndpoints := []string{
    92  		"http://" + s.clientURL.Host,
    93  		"http://invalid-pd-host:2379",
    94  	}
    95  	server, err := NewServer(pdEndpoints)
    96  	c.Assert(err, check.IsNil)
    97  	c.Assert(server, check.NotNil)
    98  
    99  	conf := config.GetGlobalServerConfig()
   100  	conf.DataDir = c.MkDir()
   101  
   102  	err = server.initDataDir(ctx)
   103  	c.Assert(err, check.IsNil)
   104  	c.Assert(conf.DataDir, check.Not(check.Equals), "")
   105  	c.Assert(conf.Sorter.SortDir, check.Equals, filepath.Join(conf.DataDir, "/tmp/sorter"))
   106  	config.StoreGlobalServerConfig(conf)
   107  
   108  	server.etcdClient = nil
   109  	conf.DataDir = ""
   110  	err = server.initDataDir(ctx)
   111  	c.Assert(err, check.IsNil)
   112  	c.Assert(conf.DataDir, check.Not(check.Equals), "")
   113  
   114  	cancel()
   115  }