Skip to content

Commit ffaeac9

Browse files
authored
test: fix flaky test TestForwardTestSuite in next-gen (#10203)
close #10200 Signed-off-by: okjiang <819421878@qq.com>
1 parent a0758a7 commit ffaeac9

File tree

2 files changed

+33
-2
lines changed

2 files changed

+33
-2
lines changed

server/server.go

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -330,14 +330,42 @@ func CreateServer(ctx context.Context, cfg *config.Config, services []string, le
330330
return s, nil
331331
}
332332

333-
func (s *Server) startEtcd(ctx context.Context) error {
333+
func (s *Server) startEtcd(ctx context.Context) (retErr error) {
334334
newCtx, cancel := context.WithTimeout(ctx, EtcdStartTimeout)
335335
defer cancel()
336336

337337
etcd, err := embed.StartEtcd(s.etcdCfg)
338338
if err != nil {
339339
return errs.ErrStartEtcd.Wrap(err).GenWithStackByCause()
340340
}
341+
cleanup := func() {
342+
// NOTE: `embed.Etcd.Close()` can block for a long time in some failure paths
343+
// (e.g. when starting a removed member that can never become ready). Avoid
344+
// blocking the caller (tests may wait for the start error) by stopping the
345+
// server synchronously and closing the embedded etcd asynchronously.
346+
if etcd.Server != nil {
347+
etcd.Server.Stop()
348+
}
349+
go etcd.Close()
350+
if s.client != nil {
351+
if cerr := s.client.Close(); cerr != nil {
352+
log.Error("close etcd client meet error", errs.ZapError(errs.ErrCloseEtcdClient, cerr))
353+
}
354+
}
355+
if s.electionClient != nil {
356+
if cerr := s.electionClient.Close(); cerr != nil {
357+
log.Error("close election client meet error", errs.ZapError(errs.ErrCloseEtcdClient, cerr))
358+
}
359+
}
360+
if s.httpClient != nil {
361+
s.httpClient.CloseIdleConnections()
362+
}
363+
}
364+
defer func() {
365+
if retErr != nil {
366+
cleanup()
367+
}
368+
}()
341369

342370
// Check cluster ID
343371
urlMap, err := etcdtypes.NewURLsMap(s.cfg.InitialCluster)

tests/cluster.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -691,7 +691,10 @@ func (c *TestCluster) runInitialServersWithRetry(maxRetries int) error {
691691

692692
errMsg := lastErr.Error()
693693
switch {
694-
case strings.Contains(errMsg, "address already in use"):
694+
case strings.Contains(errMsg, "address already in use") || strings.Contains(errMsg, "Etcd cluster ID mismatch"):
695+
// `Etcd cluster ID mismatch` can happen when the allocated peer URL happens to
696+
// connect to another test's etcd cluster (port reuse across concurrent `go test`
697+
// processes). Treat it as a port conflict and recreate servers with new ports.
695698
log.Warn("port conflict detected, recreating servers with new ports",
696699
zap.Int("attempt", i+1),
697700
zap.Int("maxRetries", maxRetries),

0 commit comments

Comments
 (0)