github.com/polarismesh/polaris@v1.17.8/apiserver/eurekaserver/write_test.go (about)

     1  /**
     2   * Tencent is pleased to support the open source community by making Polaris available.
     3   *
     4   * Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
     5   *
     6   * Licensed under the BSD 3-Clause License (the "License");
     7   * you may not use this file except in compliance with the License.
     8   * You may obtain a copy of the License at
     9   *
    10   * https://opensource.org/licenses/BSD-3-Clause
    11   *
    12   * Unless required by applicable law or agreed to in writing, software distributed
    13   * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
    14   * CONDITIONS OF ANY KIND, either express or implied. See the License for the
    15   * specific language governing permissions and limitations under the License.
    16   */
    17  
    18  package eurekaserver
    19  
    20  import (
    21  	"context"
    22  	"testing"
    23  	"time"
    24  
    25  	"github.com/golang/mock/gomock"
    26  	apiservice "github.com/polarismesh/specification/source/go/api/v1/service_manage"
    27  	"github.com/stretchr/testify/assert"
    28  	"google.golang.org/protobuf/types/known/wrapperspb"
    29  
    30  	"github.com/polarismesh/polaris/cache"
    31  	api "github.com/polarismesh/polaris/common/api/v1"
    32  	"github.com/polarismesh/polaris/common/eventhub"
    33  	"github.com/polarismesh/polaris/common/model"
    34  	"github.com/polarismesh/polaris/common/utils"
    35  	"github.com/polarismesh/polaris/store"
    36  	"github.com/polarismesh/polaris/store/mock"
    37  	testsuit "github.com/polarismesh/polaris/test/suit"
    38  )
    39  
    40  func TestEurekaServer_renew(t *testing.T) {
    41  	eventhub.InitEventHub()
    42  	ins := &model.Instance{
    43  		ServiceID: utils.NewUUID(),
    44  		Proto: &apiservice.Instance{
    45  			Service:   utils.NewStringValue("echo"),
    46  			Namespace: utils.NewStringValue("default"),
    47  			Host:      utils.NewStringValue("127.0.0.1"),
    48  			Port:      utils.NewUInt32Value(8080),
    49  			HealthCheck: &apiservice.HealthCheck{
    50  				Type: apiservice.HealthCheck_HEARTBEAT,
    51  				Heartbeat: &apiservice.HeartbeatHealthCheck{
    52  					Ttl: &wrapperspb.UInt32Value{
    53  						Value: 5,
    54  					},
    55  				},
    56  			},
    57  		},
    58  		Valid: true,
    59  	}
    60  
    61  	insId, resp := utils.CheckInstanceTetrad(ins.Proto)
    62  	if resp != nil {
    63  		t.Fatal(resp.GetInfo().GetValue())
    64  		return
    65  	}
    66  
    67  	ins.Proto.Id = utils.NewStringValue(insId)
    68  
    69  	disableBeatIns := &model.Instance{
    70  		ServiceID: utils.NewUUID(),
    71  		Proto: &apiservice.Instance{
    72  			Service:   utils.NewStringValue("echo"),
    73  			Namespace: utils.NewStringValue("default"),
    74  			Host:      utils.NewStringValue("127.0.0.2"),
    75  			Port:      utils.NewUInt32Value(8081),
    76  			HealthCheck: &apiservice.HealthCheck{
    77  				Type: apiservice.HealthCheck_HEARTBEAT,
    78  				Heartbeat: &apiservice.HeartbeatHealthCheck{
    79  					Ttl: &wrapperspb.UInt32Value{
    80  						Value: 5,
    81  					},
    82  				},
    83  			},
    84  		},
    85  		Valid: true,
    86  	}
    87  
    88  	disableBeatInsId, resp := utils.CheckInstanceTetrad(disableBeatIns.Proto)
    89  	if resp != nil {
    90  		t.Fatal(resp.GetInfo().GetValue())
    91  		return
    92  	}
    93  
    94  	disableBeatIns.Proto.Id = utils.NewStringValue(disableBeatInsId)
    95  
    96  	ctrl := gomock.NewController(t)
    97  
    98  	mockTx := mock.NewMockTx(ctrl)
    99  	mockTx.EXPECT().Commit().Return(nil).AnyTimes()
   100  	mockTx.EXPECT().Rollback().Return(nil).AnyTimes()
   101  	mockTx.EXPECT().CreateReadView().Return(nil).AnyTimes()
   102  
   103  	mockStore := mock.NewMockStore(ctrl)
   104  	mockStore.EXPECT().
   105  		GetMoreInstances(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).
   106  		AnyTimes().
   107  		Return(map[string]*model.Instance{
   108  			insId:            ins,
   109  			disableBeatInsId: disableBeatIns,
   110  		}, nil)
   111  	mockStore.EXPECT().StartReadTx().Return(mockTx, nil).AnyTimes()
   112  	mockStore.EXPECT().
   113  		GetMoreServices(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).
   114  		AnyTimes().
   115  		Return(map[string]*model.Service{
   116  			ins.ServiceID: {
   117  				ID:        ins.ServiceID,
   118  				Name:      ins.Proto.GetService().GetValue(),
   119  				Namespace: ins.Proto.GetNamespace().GetValue(),
   120  			},
   121  		}, nil)
   122  
   123  	mockStore.EXPECT().GetInstancesCountTx(gomock.Any()).AnyTimes().Return(uint32(1), nil)
   124  	mockStore.EXPECT().GetUnixSecond(gomock.Any()).AnyTimes().Return(time.Now().Unix(), nil)
   125  	mockStore.EXPECT().GetServicesCount().Return(uint32(1), nil).AnyTimes()
   126  	mockStore.EXPECT().StartLeaderElection(gomock.Any()).AnyTimes()
   127  	mockStore.EXPECT().Destroy().Return(nil)
   128  	mockStore.EXPECT().Initialize(gomock.Any()).Return(nil).AnyTimes()
   129  	mockStore.EXPECT().Name().Return("eureka_store_test").AnyTimes()
   130  
   131  	eurekaSuit := newEurekaTestSuit()
   132  	eurekaSuit.ReplaceStore(func() store.Store {
   133  		store.TestGetStore()
   134  		store.StoreSlots["eureka_store_test"] = mockStore
   135  		return mockStore
   136  	})
   137  	eurekaSuit.Initialize(func(conf *testsuit.TestConfig) {
   138  		conf.Cache = cache.Config{
   139  			Open: true,
   140  			Resources: []cache.ConfigEntry{
   141  				{
   142  					Name: "service",
   143  				},
   144  				{
   145  					Name: "instance",
   146  				},
   147  			},
   148  		}
   149  		store.TestInjectConfig(store.Config{
   150  			Name: "eureka_store_test",
   151  		})
   152  	})
   153  
   154  	defer eurekaSuit.Destroy()
   155  
   156  	t.Run("eureka客户端心跳上报-实例正常且开启心跳", func(t *testing.T) {
   157  		svr := &EurekaServer{
   158  			healthCheckServer: eurekaSuit.HealthCheckServer(),
   159  		}
   160  		code := svr.renew(context.Background(), ins.Namespace(), "", insId, false)
   161  		assert.Equalf(t, api.ExecuteSuccess, code, "code need success, actual : %d", code)
   162  	})
   163  
   164  	t.Run("eureka客户端心跳上报-实例未开启心跳", func(t *testing.T) {
   165  		svr := &EurekaServer{
   166  			healthCheckServer: eurekaSuit.HealthCheckServer(),
   167  		}
   168  		code := svr.renew(context.Background(), ins.Namespace(), "", disableBeatInsId, false)
   169  		assert.Equalf(t, api.ExecuteSuccess, code, "code need success, actual : %d", code)
   170  	})
   171  
   172  	t.Run("eureka客户端心跳上报-实例不存在", func(t *testing.T) {
   173  		svr := &EurekaServer{
   174  			healthCheckServer: eurekaSuit.HealthCheckServer(),
   175  		}
   176  		instId := utils.NewUUID()
   177  		var code uint32
   178  		for i := 0; i < 5; i++ {
   179  			code = svr.renew(context.Background(), ins.Namespace(), "", instId, false)
   180  			time.Sleep(time.Second)
   181  		}
   182  		assert.Equalf(t, api.NotFoundResource, code, "code need notfound, actual : %d", code)
   183  	})
   184  
   185  }