Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tests/discovery: reduce TestRegister cost time #7946

Merged
merged 5 commits into from
Mar 27, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading