Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nolouch committed Jun 4, 2018
1 parent 564e7b9 commit 7303f02
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 7 deletions.
2 changes: 1 addition & 1 deletion pd-client/leader_change_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ func (s *testLeaderChangeSuite) prepareClusterN(c *C, n int) (svrs map[string]*s

for i := 0; i < n; i++ {
cfg := cfgs[i]

go func() {
svr, err := server.CreateServer(cfg, api.NewHandler)
c.Assert(err, IsNil)
svr.SetUpTestMode()
err = svr.Run()
c.Assert(err, IsNil)
ch <- svr
Expand Down
4 changes: 3 additions & 1 deletion server/join.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,10 @@ func PrepareJoinCluster(cfg *Config) error {
}

// Cases with data directory.
// The path change from "member" to "member/wal"
// Reference: https://github.com/coreos/etcd/pull/9733
initialCluster := ""
if wal.Exist(path.Join(cfg.DataDir, "member")) {
if wal.Exist(path.Join(cfg.DataDir, "member/wal")) {
cfg.InitialCluster = initialCluster
cfg.InitialClusterState = embed.ClusterStateFlagExisting
return nil
Expand Down
1 change: 1 addition & 0 deletions server/join_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ func startPdWith(cfg *Config) (*Server, error) {
errCh <- errors.Trace(err)
return
}
svr.SetUpTestMode()
err = svr.Run()
if err != nil {
errCh <- errors.Trace(err)
Expand Down
9 changes: 4 additions & 5 deletions server/leader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,15 @@ type testGetLeaderSuite struct {
func (s *testGetLeaderSuite) SetUpSuite(c *C) {
cfg := NewTestSingleConfig()

// Send requests before server has started.
s.wg.Add(1)
s.done = make(chan bool)
go s.sendRequest(c, cfg.ClientUrls)
time.Sleep(100 * time.Millisecond)

svr, err := CreateServer(cfg, nil)
c.Assert(err, IsNil)

svr.SetUpTestMode()
err = svr.Run()
// Send requests after server has started.
go s.sendRequest(c, cfg.ClientUrls)
time.Sleep(100 * time.Millisecond)
c.Assert(err, IsNil)

s.svr = svr
Expand Down
10 changes: 10 additions & 0 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,16 @@ func (s *Server) Run() error {
return nil
}

// SetUpTestMode for testing
func (s *Server) SetUpTestMode() {
// The etcd master version has removed embed.Config.SetupLogging.
// Now logger is set up automatically based on embed.Config.Logger, embed.Config.LogOutputs, embed.Config.Debug fields.
// Use zap logger in the test, otherwise will panic. Reference: https://github.com/coreos/etcd/blob/master/embed/config_logging.go#L45
s.etcdCfg.Logger = "zap"
s.etcdCfg.LogOutputs = []string{"stdout"}
s.etcdCfg.Debug = false
}

func (s *Server) bootstrapCluster(req *pdpb.BootstrapRequest) (*pdpb.BootstrapResponse, error) {
clusterID := s.clusterID

Expand Down
5 changes: 5 additions & 0 deletions server/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ func newTestServer(c *C) (*Server, cleanUpFunc) {
cfg := NewTestSingleConfig()

svr, err := CreateServer(cfg, nil)
svr.SetUpTestMode()
c.Assert(err, IsNil)

cleanup := func() {
Expand Down Expand Up @@ -62,6 +63,7 @@ func newMultiTestServers(c *C, count int) ([]*Server, cleanupFunc) {
go func(cfg *Config) {
svr, err := CreateServer(cfg, nil)
c.Assert(err, IsNil)
svr.SetUpTestMode()
err = svr.Run()
c.Assert(err, IsNil)
ch <- svr
Expand Down Expand Up @@ -130,6 +132,7 @@ func (s *testLeaderServerSuite) SetUpSuite(c *C) {
go func() {
svr, err := CreateServer(cfg, nil)
c.Assert(err, IsNil)
svr.SetUpTestMode()
err = svr.Run()
c.Assert(err, IsNil)
ch <- svr
Expand Down Expand Up @@ -185,6 +188,7 @@ func newTestServersWithCfgs(c *C, cfgs []*Config) ([]*Server, cleanupFunc) {
go func(cfg *Config) {
svr, err := CreateServer(cfg, nil)
c.Assert(err, IsNil)
svr.SetUpTestMode()
err = svr.Run()
c.Assert(err, IsNil)
ch <- svr
Expand Down Expand Up @@ -304,6 +308,7 @@ func (s *testServerSuite) TestCheckClusterID(c *C) {
cfgA.InitialCluster = originInitial
svr, err := CreateServer(cfgA, nil)
c.Assert(err, IsNil)
svr.SetUpTestMode()
err = svr.Run()
c.Assert(err, NotNil)
}
1 change: 1 addition & 0 deletions server/testutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ func NewTestServer() (*Config, *Server, CleanupFunc, error) {
if err != nil {
return nil, nil, nil, errors.Trace(err)
}
s.SetUpTestMode()
if err = s.Run(); err != nil {
return nil, nil, nil, errors.Trace(err)
}
Expand Down

0 comments on commit 7303f02

Please sign in to comment.