github.com/polarismesh/polaris@v1.17.8/cache/service/service_bucket_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 service
    19  
    20  import (
    21  	"testing"
    22  
    23  	"github.com/stretchr/testify/assert"
    24  )
    25  
    26  func Test_serviceNamespaceBucket(t *testing.T) {
    27  	mockData := genModelService(10)
    28  	bucket := newServiceNamespaceBucket()
    29  
    30  	for key := range mockData {
    31  		bucket.addService(mockData[key])
    32  	}
    33  	bucket.reloadRevision()
    34  
    35  	oldRevision := bucket.revision
    36  	assert.NotEmpty(t, bucket.revision)
    37  	bucket.reloadRevision()
    38  	assert.Equal(t, oldRevision, bucket.revision)
    39  
    40  	oldRevision = bucket.revision
    41  
    42  	newMockData := genModelServiceByNamespace(10, "MockNamespace")
    43  	for key := range newMockData {
    44  		bucket.addService(newMockData[key])
    45  	}
    46  
    47  	bucket.reloadRevision()
    48  	assert.NotEmpty(t, bucket.revision)
    49  	assert.NotEqual(t, oldRevision, bucket.revision)
    50  
    51  	allRevision, ret := bucket.ListAllServices()
    52  	assert.Equal(t, int64(len(mockData)+len(newMockData)), int64(len(ret)))
    53  
    54  	oneRevision, ret := bucket.ListServices("MockNamespace")
    55  	assert.Equal(t, int64(len(newMockData)), int64(len(ret)))
    56  
    57  	oldRevision = bucket.revision
    58  	bucket.removeService(newMockData["MockNamespace-ID-1"])
    59  	bucket.reloadRevision()
    60  	assert.NotEmpty(t, bucket.revision)
    61  	assert.NotEqual(t, oldRevision, bucket.revision)
    62  	allRevision2, ret := bucket.ListAllServices()
    63  	assert.NotEqual(t, allRevision, allRevision2)
    64  	assert.Equal(t, int64(len(mockData)+len(newMockData)-1), int64(len(ret)))
    65  
    66  	oneRevision2, ret := bucket.ListServices("MockNamespace")
    67  	assert.NotEqual(t, oneRevision, oneRevision2)
    68  	assert.Equal(t, int64(len(newMockData)-1), int64(len(ret)))
    69  }