dubbo.apache.org/dubbo-go/v3@v3.1.1/registry/servicediscovery/store/cache_manager_test.go (about)

     1  /*
     2   * Licensed to the Apache Software Foundation (ASF) under one or more
     3   * contributor license agreements.  See the NOTICE file distributed with
     4   * this work for additional information regarding copyright ownership.
     5   * The ASF licenses this file to You under the Apache License, Version 2.0
     6   * (the "License"); you may not use this file except in compliance with
     7   * the License.  You may obtain a copy of the License at
     8   *
     9   *     http://www.apache.org/licenses/LICENSE-2.0
    10   *
    11   * Unless required by applicable law or agreed to in writing, software
    12   * distributed under the License is distributed on an "AS IS" BASIS,
    13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14   * See the License for the specific language governing permissions and
    15   * limitations under the License.
    16   */
    17  
    18  package store
    19  
    20  import (
    21  	"testing"
    22  	"time"
    23  )
    24  
    25  import (
    26  	"dubbo.apache.org/dubbo-go/v3/common"
    27  )
    28  
    29  var defaultTime = time.Second * 3
    30  
    31  func TestCacheManager(t *testing.T) {
    32  	cm, err := NewCacheManager("test", "test_cache", defaultTime, 10, true)
    33  	if err != nil {
    34  		t.Fatalf("failed to create cache manager: %v", err)
    35  	}
    36  
    37  	// Test Set and Get
    38  	cm.Set("key1", "value1")
    39  	cm.Set("key2", "value2")
    40  	value, ok := cm.Get("key1")
    41  	if !ok {
    42  		t.Errorf("failed to get key1: %v", err)
    43  	}
    44  	if value != "value1" {
    45  		t.Errorf("unexpected Value for key1: got %v, want %v", value, "value1")
    46  	}
    47  
    48  	// Test Delete
    49  	cm.Delete("key2")
    50  	_, ok = cm.Get("key2")
    51  	if ok {
    52  		t.Errorf("key2 was not removed from cache")
    53  	}
    54  
    55  	// Test GetAll
    56  	cm.Set("key3", "value3")
    57  	all := cm.GetAll()
    58  	if len(all) != 2 {
    59  		t.Errorf("unexpected number of items in cache: got %d, want %d", len(all), 2)
    60  	}
    61  
    62  	// Test cache file creation and loading
    63  	cm2, err := NewCacheManager("test2", "nonexistent_cache", defaultTime, 10, true)
    64  	if err != nil {
    65  		t.Fatalf("failed to create cache manager: %v", err)
    66  	}
    67  	cm2.Set("key4", "value4")
    68  	time.Sleep(time.Second * 4)
    69  	cm2.StopDump()
    70  	cm3, err := NewCacheManager("test3", "nonexistent_cache", defaultTime, 10, true)
    71  	if err != nil {
    72  		t.Fatalf("failed to create cache manager: %v", err)
    73  	}
    74  	all3 := cm3.GetAll()
    75  	if len(all3) != 1 {
    76  		t.Errorf("unexpected number of items in cache: got %d, want %d", len(all3), 1)
    77  	}
    78  	_, ok = cm3.Get("key4")
    79  	if !ok {
    80  		t.Errorf("failed to get key4: %v", err)
    81  	}
    82  	cm3.destroy()
    83  	cm2.destroy()
    84  	cm.destroy() // clear cache file
    85  }
    86  
    87  func TestMetaInfoCacheManager(t *testing.T) {
    88  
    89  	serverInfo := make(map[string]*common.ServiceInfo)
    90  	serverInfo["1"] = common.NewServiceInfo("1", "1", "1", "1", "1", make(map[string]string))
    91  	serverInfo["2"] = common.NewServiceInfo("2", "2", "2", "2", "2", make(map[string]string))
    92  	serverInfo["3"] = common.NewServiceInfo("3", "3", "3", "3", "3", make(map[string]string))
    93  
    94  	metadataInfo1 := common.NewMetadataInfo("1", "1", serverInfo)
    95  	metadataInfo2 := common.NewMetadataInfo("2", "2", serverInfo)
    96  	metadataInfo3 := common.NewMetadataInfo("3", "3", serverInfo)
    97  	metadataInfo4 := common.NewMetadataInfo("4", "4", serverInfo)
    98  
    99  	cm, err := NewCacheManager("metaTest1", "test_meta_cache", defaultTime, 10, true)
   100  	if err != nil {
   101  		t.Fatalf("failed to create cache manager: %v", err)
   102  	}
   103  
   104  	// Test Set and Get
   105  	cm.Set("key1", metadataInfo1)
   106  	cm.Set("key2", metadataInfo2)
   107  	value, ok := cm.Get("key1")
   108  	if !ok {
   109  		t.Errorf("failed to get key1: %v", err)
   110  	}
   111  	if value != metadataInfo1 {
   112  		t.Errorf("unexpected Value for key1: got %v, want %v", value, "value1")
   113  	}
   114  
   115  	// Test Delete
   116  	cm.Delete("key2")
   117  	_, ok = cm.Get("key2")
   118  	if ok {
   119  		t.Errorf("key2 was not removed from cache")
   120  	}
   121  
   122  	// Test GetAll
   123  	cm.Set("key3", metadataInfo3)
   124  	all := cm.GetAll()
   125  	if len(all) != 2 {
   126  		t.Errorf("unexpected number of items in cache: got %d, want %d", len(all), 2)
   127  	}
   128  
   129  	// Test cache file creation and loading
   130  	cm2, err := NewCacheManager("metaTest2", "nonexistent_meta_cache", defaultTime, 10, true)
   131  	if err != nil {
   132  		t.Fatalf("failed to create cache manager: %v", err)
   133  	}
   134  	cm2.Set("key4", metadataInfo4)
   135  	time.Sleep(time.Second * 4)
   136  	cm2.StopDump()
   137  	cm3, err := NewCacheManager("test3", "nonexistent_meta_cache", defaultTime, 10, true)
   138  	if err != nil {
   139  		t.Fatalf("failed to create cache manager: %v", err)
   140  	}
   141  	all3 := cm3.GetAll()
   142  	if len(all3) != 1 {
   143  		t.Errorf("unexpected number of items in cache: got %d, want %d", len(all3), 1)
   144  	}
   145  	_, ok = cm3.Get("key4")
   146  	if !ok {
   147  		t.Errorf("failed to get key4: %v", err)
   148  	}
   149  	cm3.destroy()
   150  	cm2.destroy()
   151  	cm.destroy() // clear cache file
   152  }