|
| 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 | +} |
0 commit comments