github.com/matrixorigin/matrixone@v1.2.0/pkg/gossip/node_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 gossip
    16  
    17  import (
    18  	"context"
    19  	"testing"
    20  	"time"
    21  
    22  	"github.com/lni/goutils/leaktest"
    23  	"github.com/stretchr/testify/assert"
    24  )
    25  
    26  func TestNodeGossip(t *testing.T) {
    27  	defer leaktest.AfterTest(t)()
    28  	ctx := context.Background()
    29  	n1, err := NewNode(ctx, "n1",
    30  		WithListenAddrFn(func() string {
    31  			return "127.0.0.1:38001"
    32  		}),
    33  		WithServiceAddrFn(func() string {
    34  			return "127.0.0.1:38001"
    35  		}),
    36  		WithCacheServerAddrFn(func() string {
    37  			return "127.0.0.1:38101"
    38  		}))
    39  	assert.NoError(t, err)
    40  	assert.NotNil(t, n1)
    41  	err = n1.Create()
    42  	assert.NoError(t, err)
    43  	defer func() {
    44  		assert.NoError(t, n1.Leave(time.Millisecond*300))
    45  	}()
    46  	err = n1.Join(nil)
    47  	assert.Equal(t, 1, n1.NumMembers())
    48  	assert.NotNil(t, n1.DistKeyCacheGetter()())
    49  
    50  	assert.NoError(t, err)
    51  	n2, err := NewNode(ctx, "n2",
    52  		WithListenAddrFn(func() string {
    53  			return "127.0.0.1:38002"
    54  		}),
    55  		WithServiceAddrFn(func() string {
    56  			return "127.0.0.1:38002"
    57  		}),
    58  		WithCacheServerAddrFn(func() string {
    59  			return "127.0.0.1:38201"
    60  		}))
    61  	assert.NoError(t, err)
    62  	assert.NotNil(t, n2)
    63  	err = n2.Create()
    64  	assert.NoError(t, err)
    65  	defer func() {
    66  		assert.NoError(t, n2.Leave(time.Millisecond*300))
    67  	}()
    68  	err = n2.Join([]string{"127.0.0.1:38001"})
    69  	assert.NoError(t, err)
    70  	assert.Equal(t, 2, n2.NumMembers())
    71  	assert.NotNil(t, n2.DistKeyCacheGetter()())
    72  }