From a63d42e96b9553ff00ea74c66bc4c4460a5bfd56 Mon Sep 17 00:00:00 2001 From: Weizhen Wang Date: Wed, 25 Sep 2024 20:30:04 +0800 Subject: [PATCH] *: fix flaky test TestConnectThrough636 (#56310) close pingcap/tidb#56309 --- pkg/privilege/privileges/ldap/ldap_common_test.go | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/pkg/privilege/privileges/ldap/ldap_common_test.go b/pkg/privilege/privileges/ldap/ldap_common_test.go index 2d84982d7ffa0..e5e1d11f69a32 100644 --- a/pkg/privilege/privileges/ldap/ldap_common_test.go +++ b/pkg/privilege/privileges/ldap/ldap_common_test.go @@ -22,6 +22,7 @@ import ( "fmt" "math/rand" "net" + "strconv" "sync" "testing" "time" @@ -52,7 +53,7 @@ func TestConnectThrough636(t *testing.T) { startListen := make(chan struct{}) // this test only tests whether the LDAP with LTS enabled will fallback from StartTLS - randomTLSServicePort := rand.Int()%10000 + 10000 + var randomTLSServiceAddress string serverWg := &sync.WaitGroup{} serverWg.Add(1) go func() { @@ -64,8 +65,11 @@ func TestConnectThrough636(t *testing.T) { tlsConfig := &tls.Config{ Certificates: []tls.Certificate{cert}, } - ln, err = tls.Listen("tcp", fmt.Sprintf("localhost:%d", randomTLSServicePort), tlsConfig) + ln, err = tls.Listen("tcp", ":0", tlsConfig) + require.NoError(t, err) + + randomTLSServiceAddress = ln.Addr().String() startListen <- struct{}{} for { @@ -100,7 +104,11 @@ func TestConnectThrough636(t *testing.T) { impl := &ldapAuthImpl{} impl.SetEnableTLS(true) impl.SetLDAPServerHost("localhost") - impl.SetLDAPServerPort(randomTLSServicePort) + _, port, err := net.SplitHostPort(randomTLSServiceAddress) + require.NoError(t, err) + p, err := strconv.Atoi(port) + require.NoError(t, err) + impl.SetLDAPServerPort(p) impl.caPool = x509.NewCertPool() require.True(t, impl.caPool.AppendCertsFromPEM(tlsCAStr))