github.com/matrixorigin/matrixone@v1.2.0/pkg/util/address/address_test.go (about)

     1  // Copyright 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 address
    16  
    17  import (
    18  	"fmt"
    19  	"net"
    20  	"testing"
    21  	"time"
    22  
    23  	"github.com/stretchr/testify/assert"
    24  )
    25  
    26  func TestAdjust(t *testing.T) {
    27  	defaultListenAddress := "127.0.0.1:8080"
    28  	cases := []struct {
    29  		address       Address
    30  		exceptAddress Address
    31  		machineHost   string
    32  	}{
    33  		{
    34  			address: Address{},
    35  			exceptAddress: Address{
    36  				ListenAddress:  defaultListenAddress,
    37  				ServiceAddress: defaultListenAddress,
    38  			},
    39  		},
    40  		{
    41  			address: Address{
    42  				ListenAddress:  "127.0.0.1:8081",
    43  				ServiceAddress: "",
    44  			},
    45  			exceptAddress: Address{
    46  				ListenAddress:  "127.0.0.1:8081",
    47  				ServiceAddress: "127.0.0.1:8081",
    48  			},
    49  		},
    50  		{
    51  			address: Address{
    52  				ListenAddress:  "127.0.0.1:8081",
    53  				ServiceAddress: "abc:8081",
    54  			},
    55  			exceptAddress: Address{
    56  				ListenAddress:  "127.0.0.1:8081",
    57  				ServiceAddress: "abc:8081",
    58  			},
    59  		},
    60  		{
    61  			address: Address{
    62  				ListenAddress:  "127.0.0.1:8081",
    63  				ServiceAddress: "abc:8081",
    64  			},
    65  			exceptAddress: Address{
    66  				ListenAddress:  "127.0.0.1:8081",
    67  				ServiceAddress: "bcd:8081",
    68  			},
    69  			machineHost: "bcd",
    70  		},
    71  		{
    72  			address: Address{},
    73  			exceptAddress: Address{
    74  				ListenAddress:  defaultListenAddress,
    75  				ServiceAddress: "abc:8080",
    76  			},
    77  			machineHost: "abc",
    78  		},
    79  	}
    80  
    81  	for _, c := range cases {
    82  		c.address.Adjust(c.machineHost, defaultListenAddress)
    83  		assert.Equal(t, c.exceptAddress, c.address)
    84  	}
    85  }
    86  
    87  func TestAddressManager(t *testing.T) {
    88  	serviceAddr := "127.0.0.1"
    89  	portBase := 59320
    90  	m := NewAddressManager(serviceAddr, portBase)
    91  	assert.NotNil(t, m)
    92  
    93  	p1 := m.Register(2)
    94  	assert.Equal(t, fmt.Sprintf("0.0.0.0:%d", p1), m.ListenAddress(2))
    95  	assert.Equal(t, fmt.Sprintf("127.0.0.1:%d", p1), m.ServiceAddress(2))
    96  
    97  	p2 := m.Register(1)
    98  	assert.Equal(t, fmt.Sprintf("0.0.0.0:%d", p2), m.ListenAddress(1))
    99  	assert.Equal(t, fmt.Sprintf("127.0.0.1:%d", p2), m.ServiceAddress(1))
   100  
   101  	p3 := p2 + 1
   102  
   103  	var err error
   104  	var l net.Listener
   105  	for {
   106  		l, err = net.Listen("tcp4", fmt.Sprintf("0.0.0.0:%d", p3))
   107  		if err == nil {
   108  			break
   109  		}
   110  		p3++
   111  	}
   112  	defer func() {
   113  		if l != nil {
   114  			err = l.Close()
   115  			assert.NoError(t, err)
   116  		}
   117  	}()
   118  	p4 := m.Register(3)
   119  	assert.Equal(t, fmt.Sprintf("0.0.0.0:%d", p4), m.ListenAddress(3))
   120  	assert.Equal(t, fmt.Sprintf("127.0.0.1:%d", p4), m.ServiceAddress(3))
   121  }
   122  
   123  func TestRemoteAddressAvail(t *testing.T) {
   124  	remoteAddr := "127.0.0.1:17001"
   125  	timeout := time.Millisecond * 500
   126  	assert.False(t, RemoteAddressAvail(remoteAddr, timeout))
   127  
   128  	l, err := net.Listen("tcp", remoteAddr)
   129  	assert.NoError(t, err)
   130  	assert.NotNil(t, l)
   131  	defer l.Close()
   132  	assert.True(t, RemoteAddressAvail(remoteAddr, timeout))
   133  }