Skip to content

Commit cac2301

Browse files
committed
tests: cover MCS listener address updates
Signed-off-by: Ryan Leung <rleungx@gmail.com>
1 parent d08263d commit cac2301

4 files changed

Lines changed: 224 additions & 0 deletions

File tree

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// Copyright 2026 TiKV Project Authors.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package server
16+
17+
import (
18+
"context"
19+
"net/url"
20+
"testing"
21+
22+
"github.com/stretchr/testify/require"
23+
)
24+
25+
func TestInitListenerAndUpdateConfigWithKernelSelectedPort(t *testing.T) {
26+
re := require.New(t)
27+
cfg := NewConfig()
28+
cfg.BackendEndpoints = "http://127.0.0.1:2379"
29+
cfg.ListenAddr = "http://127.0.0.1:0"
30+
cfg.AdvertiseListenAddr = "http://10.0.0.5:0"
31+
cfg.Name = cfg.ListenAddr
32+
33+
cfg, err := GenerateConfig(cfg)
34+
re.NoError(err)
35+
svr := CreateServer(context.Background(), cfg)
36+
re.NoError(svr.initListenerAndUpdateConfig())
37+
defer func() {
38+
re.NoError(svr.GetListener().Close())
39+
}()
40+
41+
actualURL, err := url.Parse(svr.GetActualListenAddr())
42+
re.NoError(err)
43+
re.Equal("127.0.0.1", actualURL.Hostname())
44+
re.NotEmpty(actualURL.Port())
45+
re.NotEqual("0", actualURL.Port())
46+
re.Equal(svr.GetActualListenAddr(), cfg.ListenAddr)
47+
48+
advertiseURL, err := url.Parse(cfg.AdvertiseListenAddr)
49+
re.NoError(err)
50+
re.Equal("10.0.0.5", advertiseURL.Hostname())
51+
re.Equal(actualURL.Port(), advertiseURL.Port())
52+
re.Equal(cfg.AdvertiseListenAddr, cfg.Name)
53+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// Copyright 2026 TiKV Project Authors.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package server
16+
17+
import (
18+
"context"
19+
"net/url"
20+
"testing"
21+
22+
"github.com/stretchr/testify/require"
23+
"go.uber.org/goleak"
24+
25+
"github.com/tikv/pd/pkg/mcs/router/server/config"
26+
"github.com/tikv/pd/pkg/utils/testutil"
27+
)
28+
29+
func TestMain(m *testing.M) {
30+
goleak.VerifyTestMain(m, testutil.LeakOptions...)
31+
}
32+
33+
func TestInitListenerAndUpdateConfigWithKernelSelectedPort(t *testing.T) {
34+
re := require.New(t)
35+
cfg := config.NewConfig()
36+
cfg.BackendEndpoints = "http://127.0.0.1:2379"
37+
cfg.ListenAddr = "http://127.0.0.1:0"
38+
cfg.AdvertiseListenAddr = "http://10.0.0.5:0"
39+
cfg.Name = cfg.ListenAddr
40+
41+
cfg, err := GenerateConfig(cfg)
42+
re.NoError(err)
43+
svr := CreateServer(context.Background(), cfg)
44+
re.NoError(svr.initListenerAndUpdateConfig())
45+
defer func() {
46+
re.NoError(svr.GetListener().Close())
47+
}()
48+
49+
actualURL, err := url.Parse(svr.GetActualListenAddr())
50+
re.NoError(err)
51+
re.Equal("127.0.0.1", actualURL.Hostname())
52+
re.NotEmpty(actualURL.Port())
53+
re.NotEqual("0", actualURL.Port())
54+
re.Equal(svr.GetActualListenAddr(), cfg.ListenAddr)
55+
56+
advertiseURL, err := url.Parse(cfg.AdvertiseListenAddr)
57+
re.NoError(err)
58+
re.Equal("10.0.0.5", advertiseURL.Hostname())
59+
re.Equal(actualURL.Port(), advertiseURL.Port())
60+
re.Equal(cfg.AdvertiseListenAddr, cfg.Name)
61+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// Copyright 2026 TiKV Project Authors.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package server
16+
17+
import (
18+
"context"
19+
"net/url"
20+
"testing"
21+
22+
"github.com/stretchr/testify/require"
23+
24+
"github.com/tikv/pd/pkg/mcs/scheduling/server/config"
25+
)
26+
27+
func TestInitListenerAndUpdateConfigWithKernelSelectedPort(t *testing.T) {
28+
re := require.New(t)
29+
cfg := config.NewConfig()
30+
cfg.BackendEndpoints = "http://127.0.0.1:2379"
31+
cfg.ListenAddr = "http://127.0.0.1:0"
32+
cfg.AdvertiseListenAddr = "http://10.0.0.5:0"
33+
cfg.Name = cfg.ListenAddr
34+
35+
cfg, err := GenerateConfig(cfg)
36+
re.NoError(err)
37+
svr := CreateServer(context.Background(), cfg)
38+
re.NoError(svr.initListenerAndUpdateConfig())
39+
defer func() {
40+
re.NoError(svr.GetListener().Close())
41+
}()
42+
43+
actualURL, err := url.Parse(svr.GetActualListenAddr())
44+
re.NoError(err)
45+
re.Equal("127.0.0.1", actualURL.Hostname())
46+
re.NotEmpty(actualURL.Port())
47+
re.NotEqual("0", actualURL.Port())
48+
re.Equal(svr.GetActualListenAddr(), cfg.ListenAddr)
49+
50+
advertiseURL, err := url.Parse(cfg.AdvertiseListenAddr)
51+
re.NoError(err)
52+
re.Equal("10.0.0.5", advertiseURL.Hostname())
53+
re.Equal(actualURL.Port(), advertiseURL.Port())
54+
re.Equal(cfg.AdvertiseListenAddr, cfg.Name)
55+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// Copyright 2026 TiKV Project Authors.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package server
16+
17+
import (
18+
"context"
19+
"net"
20+
"net/url"
21+
"testing"
22+
23+
"github.com/stretchr/testify/require"
24+
)
25+
26+
func TestInitListenerAndUpdateConfigWithKernelSelectedPort(t *testing.T) {
27+
re := require.New(t)
28+
cfg := NewConfig()
29+
cfg.BackendEndpoints = "http://127.0.0.1:2379"
30+
cfg.ListenAddr = "http://127.0.0.1:0"
31+
cfg.AdvertiseListenAddr = "http://10.0.0.5:0"
32+
cfg.Name = cfg.ListenAddr
33+
34+
cfg, err := GenerateConfig(cfg)
35+
re.NoError(err)
36+
svr := CreateServer(context.Background(), cfg)
37+
re.NoError(svr.initListenerAndUpdateConfig())
38+
defer func() {
39+
re.NoError(svr.GetListener().Close())
40+
}()
41+
42+
actualURL, err := url.Parse(svr.GetActualListenAddr())
43+
re.NoError(err)
44+
re.Equal("127.0.0.1", actualURL.Hostname())
45+
re.NotEmpty(actualURL.Port())
46+
re.NotEqual("0", actualURL.Port())
47+
re.Equal(svr.GetActualListenAddr(), cfg.ListenAddr)
48+
49+
advertiseURL, err := url.Parse(cfg.AdvertiseListenAddr)
50+
re.NoError(err)
51+
re.Equal("10.0.0.5", advertiseURL.Hostname())
52+
re.Equal(actualURL.Port(), advertiseURL.Port())
53+
re.Equal(cfg.AdvertiseListenAddr, cfg.Name)
54+
re.Equal(net.JoinHostPort("10.0.0.5", actualURL.Port()), svr.advertiseListenHost)
55+
}

0 commit comments

Comments
 (0)