Skip to content

Commit

Permalink
tests/discovery: reduce TestRegister cost time (#7946)
Browse files Browse the repository at this point in the history
ref #7930

Signed-off-by: husharp <[email protected]>
  • Loading branch information
HuSharp authored Mar 27, 2024
1 parent 13188c9 commit 423e36c
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions pkg/mcs/discovery/register_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ package discovery

import (
"context"
"os"
"regexp"
"testing"
"time"

Expand Down Expand Up @@ -59,10 +61,21 @@ func TestRegister(t *testing.T) {
sr = NewServiceRegister(context.Background(), client, "12345", "test_service", "127.0.0.1:2", "127.0.0.1:2", 1)
err = sr.Register()
re.NoError(err)
fname := testutil.InitTempFileLogger("info")
defer os.Remove(fname)
for i := 0; i < 3; i++ {
re.Equal("127.0.0.1:2", getKeyAfterLeaseExpired(re, client, sr.key))
etcd.Server.HardStop() // close the etcd to make the keepalive failed
time.Sleep(etcdutil.DefaultDialTimeout) // ensure that the request is timeout
etcd.Server.HardStop() // close the etcd to make the keepalive failed
// ensure that the request is timeout
testutil.Eventually(re, func() bool {
content, _ := os.ReadFile(fname)
// check log in function `ServiceRegister.Register`
// ref https://github.com/tikv/pd/blob/6377b26e4e879e7623fbc1d0b7f1be863dea88ad/pkg/mcs/discovery/register.go#L77
// need to both contain `register.go` and `keep alive failed`
pattern := regexp.MustCompile(`register.go.*keep alive failed`)
matches := pattern.FindAll(content, -1)
return len(matches) >= i+1
})
etcd.Close()
etcd, err = embed.StartEtcd(&cfg)
re.NoError(err)
Expand Down

0 comments on commit 423e36c

Please sign in to comment.