github.com/matrixorigin/matrixone@v1.2.0/pkg/cnservice/service_ports_test.go (about)

     1  // Copyright 2021 - 2023 Matrix Origin
     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 cnservice
    16  
    17  import (
    18  	"fmt"
    19  	"testing"
    20  
    21  	"github.com/matrixorigin/matrixone/pkg/lockservice"
    22  	"github.com/matrixorigin/matrixone/pkg/queryservice"
    23  	"github.com/matrixorigin/matrixone/pkg/util/address"
    24  	"github.com/stretchr/testify/assert"
    25  )
    26  
    27  func TestService_NewPortStrategy(t *testing.T) {
    28  	s := &service{cfg: &Config{}}
    29  	assert.False(t, s.newPortStrategy())
    30  	s.cfg.PortBase = 1000
    31  	assert.True(t, s.newPortStrategy())
    32  }
    33  
    34  func TestService_RegisterServices(t *testing.T) {
    35  	listenHost := "0.0.0.0"
    36  	serviceHost := "127.0.0.1"
    37  	port1 := 1000
    38  	port2 := 2000
    39  
    40  	s := &service{
    41  		cfg: &Config{
    42  			ListenAddress:  fmt.Sprintf("%s:%d", listenHost, port1),
    43  			ServiceAddress: fmt.Sprintf("%s:%d", serviceHost, port1),
    44  			LockService: lockservice.Config{
    45  				ListenAddress:  fmt.Sprintf("%s:%d", listenHost, port1+1),
    46  				ServiceAddress: fmt.Sprintf("%s:%d", serviceHost, port1+1),
    47  			},
    48  			QueryServiceConfig: queryservice.Config{
    49  				Address: address.Address{
    50  					ListenAddress:  fmt.Sprintf("%s:%d", listenHost, port1+3),
    51  					ServiceAddress: fmt.Sprintf("%s:%d", serviceHost, port1+3),
    52  				},
    53  			},
    54  		},
    55  		addressMgr: address.NewAddressManager(serviceHost, port2),
    56  	}
    57  
    58  	assert.Equal(t, fmt.Sprintf("%s:%d", serviceHost, port1), s.pipelineServiceServiceAddr())
    59  	assert.Equal(t, fmt.Sprintf("%s:%d", listenHost, port1), s.pipelineServiceListenAddr())
    60  	assert.Equal(t, fmt.Sprintf("%s:%d", serviceHost, port1+1), s.lockServiceServiceAddr())
    61  	assert.Equal(t, fmt.Sprintf("%s:%d", listenHost, port1+1), s.lockServiceListenAddr())
    62  	assert.Equal(t, fmt.Sprintf("%s:%d", serviceHost, port1+3), s.queryServiceServiceAddr())
    63  	assert.Equal(t, fmt.Sprintf("%s:%d", listenHost, port1+3), s.queryServiceListenAddr())
    64  
    65  	s.cfg.PortBase = port2
    66  	s.registerServices()
    67  	assert.Equal(t, fmt.Sprintf("%s:%d", serviceHost, port2), s.pipelineServiceServiceAddr())
    68  	assert.Equal(t, fmt.Sprintf("%s:%d", listenHost, port2), s.pipelineServiceListenAddr())
    69  	assert.Equal(t, fmt.Sprintf("%s:%d", serviceHost, port2+1), s.lockServiceServiceAddr())
    70  	assert.Equal(t, fmt.Sprintf("%s:%d", listenHost, port2+1), s.lockServiceListenAddr())
    71  	assert.Equal(t, fmt.Sprintf("%s:%d", serviceHost, port2+2), s.queryServiceServiceAddr())
    72  	assert.Equal(t, fmt.Sprintf("%s:%d", listenHost, port2+2), s.queryServiceListenAddr())
    73  }
    74  
    75  func TestDefaultCnConfig(t *testing.T) {
    76  	cfg := Config{}
    77  	cfg.SetDefaultValue()
    78  }