github.com/matrixorigin/matrixone@v1.2.0/pkg/tnservice/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 tnservice
    16  
    17  import (
    18  	"fmt"
    19  	"testing"
    20  
    21  	"github.com/matrixorigin/matrixone/pkg/lockservice"
    22  	"github.com/matrixorigin/matrixone/pkg/util/address"
    23  	"github.com/matrixorigin/matrixone/pkg/util/toml"
    24  	"github.com/stretchr/testify/assert"
    25  )
    26  
    27  func TestService_NewPortStrategy(t *testing.T) {
    28  	s := &store{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 := &store{
    41  		cfg: &Config{
    42  			ListenAddress:  fmt.Sprintf("%s:%d", listenHost, port1),
    43  			ServiceAddress: fmt.Sprintf("%s:%d", serviceHost, port1),
    44  			LogtailServer: struct {
    45  				ListenAddress              string        `toml:"listen-address"`
    46  				ServiceAddress             string        `toml:"service-address"`
    47  				RpcMaxMessageSize          toml.ByteSize `toml:"rpc-max-message-size"`
    48  				RpcEnableChecksum          bool          `toml:"rpc-enable-checksum" user_setting:"advanced"`
    49  				LogtailRPCStreamPoisonTime toml.Duration `toml:"logtail-rpc-stream-poison-time"`
    50  				LogtailCollectInterval     toml.Duration `toml:"logtail-collect-interval"`
    51  				LogtailResponseSendTimeout toml.Duration `toml:"logtail-response-send-timeout"`
    52  			}(struct {
    53  				ListenAddress              string
    54  				ServiceAddress             string
    55  				RpcMaxMessageSize          toml.ByteSize
    56  				RpcEnableChecksum          bool
    57  				LogtailRPCStreamPoisonTime toml.Duration
    58  				LogtailCollectInterval     toml.Duration
    59  				LogtailResponseSendTimeout toml.Duration
    60  			}{
    61  				ListenAddress:  fmt.Sprintf("%s:%d", listenHost, port1+1),
    62  				ServiceAddress: fmt.Sprintf("%s:%d", serviceHost, port1+1),
    63  			}),
    64  			LockService: lockservice.Config{
    65  				ListenAddress:  fmt.Sprintf("%s:%d", listenHost, port1+2),
    66  				ServiceAddress: fmt.Sprintf("%s:%d", serviceHost, port1+2),
    67  			},
    68  		},
    69  		addressMgr: address.NewAddressManager(serviceHost, port2),
    70  	}
    71  
    72  	assert.Equal(t, fmt.Sprintf("%s:%d", serviceHost, port1), s.txnServiceServiceAddr())
    73  	assert.Equal(t, fmt.Sprintf("%s:%d", listenHost, port1), s.txnServiceListenAddr())
    74  	assert.Equal(t, fmt.Sprintf("%s:%d", serviceHost, port1+1), s.logtailServiceServiceAddr())
    75  	assert.Equal(t, fmt.Sprintf("%s:%d", listenHost, port1+1), s.logtailServiceListenAddr())
    76  	assert.Equal(t, fmt.Sprintf("%s:%d", serviceHost, port1+2), s.lockServiceServiceAddr())
    77  	assert.Equal(t, fmt.Sprintf("%s:%d", listenHost, port1+2), s.lockServiceListenAddr())
    78  
    79  	s.cfg.PortBase = port2
    80  	s.registerServices()
    81  	assert.Equal(t, fmt.Sprintf("%s:%d", serviceHost, port2), s.txnServiceServiceAddr())
    82  	assert.Equal(t, fmt.Sprintf("%s:%d", listenHost, port2), s.txnServiceListenAddr())
    83  	assert.Equal(t, fmt.Sprintf("%s:%d", serviceHost, port2+1), s.logtailServiceServiceAddr())
    84  	assert.Equal(t, fmt.Sprintf("%s:%d", listenHost, port2+1), s.logtailServiceListenAddr())
    85  	assert.Equal(t, fmt.Sprintf("%s:%d", serviceHost, port2+2), s.lockServiceServiceAddr())
    86  	assert.Equal(t, fmt.Sprintf("%s:%d", listenHost, port2+2), s.lockServiceListenAddr())
    87  }