github.com/polarismesh/polaris@v1.17.8/common/redispool/config_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 redispool 19 20 import ( 21 "encoding/json" 22 "testing" 23 24 "github.com/stretchr/testify/assert" 25 "gopkg.in/yaml.v2" 26 27 "github.com/polarismesh/polaris/plugin" 28 ) 29 30 func Test_UnmarshalClusterConfig(t *testing.T) { 31 raw := ` 32 name: heartbeatRedis 33 option: 34 deployMode: cluster 35 addrs: 36 - "192.168.0.1:7001" 37 - "192.168.0.1:7002" 38 - "192.168.0.1:7003" 39 kvPasswd: "polaris" 40 poolSize: 233 41 minIdleConns: 30 42 idleTimeout: 120s 43 connectTimeout: 200ms 44 msgTimeout: 200ms 45 concurrency: 200 46 withTLS: false 47 ` 48 var entry plugin.ConfigEntry 49 if err := yaml.Unmarshal([]byte(raw), &entry); err != nil { 50 t.Fatalf("unmarshal yaml error: %v", err) 51 } 52 53 data, err := json.Marshal(entry.Option) 54 if err != nil { 55 t.Fatalf("marshal config entry got error: %v", err) 56 } 57 58 var config Config 59 if err = json.Unmarshal(data, &config); err != nil { 60 t.Fatalf("unmarshal to json got error:%v", err) 61 } 62 63 assert.Equal(t, config.DeployMode, "cluster") 64 assert.Equal(t, config.KvAddr, "") 65 assert.Equal(t, config.KvPasswd, "polaris") 66 assert.Equal(t, config.PoolSize, 233) 67 assert.Equal(t, config.ClusterConfig.Addrs, []string{ 68 "192.168.0.1:7001", 69 "192.168.0.1:7002", 70 "192.168.0.1:7003", 71 }) 72 } 73 74 func Test_SentinelConfig(t *testing.T) { 75 raw := ` 76 name: heartbeatRedis 77 option: 78 deployMode: sentinel 79 addrs: 80 - "192.168.0.1:26379" 81 - "192.168.0.2:26379" 82 - "192.168.0.3:26379" 83 masterName: "my-sentinel-master-name" 84 sentinelUsername: "sentinel-polaris" # sentinel 客户端的用户名 85 sentinelPassword: "sentinel-polaris-password" # sentinel 客户端的密码 86 kvPasswd: "polaris" # redis 客户端的密码 87 poolSize: 233 88 minIdleConns: 30 89 idleTimeout: 120s 90 connectTimeout: 200ms 91 msgTimeout: 200ms 92 concurrency: 200 93 withTLS: false 94 ` 95 96 var entry plugin.ConfigEntry 97 if err := yaml.Unmarshal([]byte(raw), &entry); err != nil { 98 t.Fatalf("unmarshal yaml error: %v", err) 99 } 100 101 data, err := json.Marshal(entry.Option) 102 if err != nil { 103 t.Fatalf("marshal config entry got error: %v", err) 104 } 105 106 var config Config 107 if err = json.Unmarshal(data, &config); err != nil { 108 t.Fatalf("unmarshal to json got error:%v", err) 109 } 110 111 // keep default 112 assert.Equal(t, config.StandaloneConfig.WaitTime, DefaultConfig().WaitTime) 113 assert.Equal(t, config.StandaloneConfig.MinBatchCount, DefaultConfig().MinBatchCount) 114 assert.Equal(t, config.StandaloneConfig.PoolTimeout, DefaultConfig().PoolTimeout) 115 assert.Equal(t, config.StandaloneConfig.MaxConnAge, DefaultConfig().MaxConnAge) 116 117 // use config file 118 assert.Equal(t, config.DeployMode, "sentinel") 119 assert.Equal(t, config.KvAddr, "") 120 assert.Equal(t, config.KvPasswd, "polaris") 121 assert.Equal(t, config.PoolSize, 233) 122 123 assert.Equal(t, config.SentinelConfig.MasterName, "my-sentinel-master-name") 124 assert.Equal(t, config.SentinelConfig.SentinelUsername, "sentinel-polaris") 125 assert.Equal(t, config.SentinelConfig.SentinelPassword, "sentinel-polaris-password") 126 assert.Equal(t, config.SentinelConfig.Addrs, []string{ 127 "192.168.0.1:26379", 128 "192.168.0.2:26379", 129 "192.168.0.3:26379", 130 }) 131 }