github.com/polarismesh/polaris@v1.17.8/common/redispool/config_option_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  	"crypto/tls"
    22  	"testing"
    23  	"time"
    24  
    25  	"github.com/stretchr/testify/assert"
    26  )
    27  
    28  func TestNewRedisClient(t *testing.T) {
    29  	config := DefaultConfig()
    30  
    31  	t.Log("before config: ", config)
    32  
    33  	_ = NewRedisClient(config,
    34  		WithAddr("127.0.0.1:6379"),
    35  		WithMaxConnAge(1000*time.Second),
    36  		WithMinIdleConns(30),
    37  	)
    38  	assert.Equal(t, config.KvAddr, "127.0.0.1:6379")
    39  	assert.Equal(t, config.MaxConnAge, 1000*time.Second)
    40  	assert.Equal(t, config.MinIdleConns, 30)
    41  
    42  	t.Log("after config: ", config)
    43  
    44  	// client := NewRedisClient(WithConfig(config))
    45  	// err := client.Set(context.Background(), "polaris", 1, 60*time.Second).Err()
    46  	// if err != nil {
    47  	// 	t.Fatalf("test redis client error:%v", err)
    48  	// }
    49  
    50  	t.Log("test success")
    51  }
    52  
    53  // testOptions optional functions for test
    54  var testOptions = []Option{
    55  	WithAddr("127.0.0.1:6379"),
    56  	WithUsername(""),
    57  	WithPwd("polaris"),
    58  	WithMinIdleConns(1234),
    59  	WithIdleTimeout(time.Minute),
    60  	WithConnectTimeout(time.Millisecond * 10),
    61  	WithConcurrency(20),
    62  	WithCompatible(false),
    63  	WithMaxRetry(5),
    64  	WithMinBatchCount(1),
    65  	WithWaitTime(time.Millisecond * 50),
    66  	WithMaxRetries(5),
    67  	WithDB(16),
    68  	WithReadTimeout(time.Millisecond * 200),
    69  	WithWriteTimeout(time.Millisecond * 200),
    70  	WithPoolSize(2000),
    71  	WithPoolTimeout(time.Second * 3),
    72  	WithMaxConnAge(time.Minute * 30),
    73  	WithTLSConfig(&tls.Config{
    74  		InsecureSkipVerify: true,
    75  	}),
    76  }
    77  
    78  func Test_WithStandalone(t *testing.T) {
    79  	config := DefaultConfig()
    80  	for _, option := range testOptions {
    81  		option(config)
    82  	}
    83  	assert.Equal(t, &Config{
    84  		DeployMode: "",
    85  		StandaloneConfig: StandaloneConfig{
    86  			KvAddr:         "127.0.0.1:6379",
    87  			KvUser:         "",
    88  			KvPasswd:       "polaris",
    89  			MinIdleConns:   1234,
    90  			IdleTimeout:    time.Minute,
    91  			ConnectTimeout: time.Millisecond * 10,
    92  			Concurrency:    20,
    93  			MaxRetry:       5,
    94  			MinBatchCount:  1,
    95  			WaitTime:       time.Millisecond * 50,
    96  			MaxRetries:     5,
    97  			DB:             16,
    98  			ReadTimeout:    time.Millisecond * 200,
    99  			WriteTimeout:   time.Millisecond * 200,
   100  			MsgTimeout:     time.Millisecond * 300,
   101  			PoolSize:       2000,
   102  			PoolTimeout:    time.Second * 3,
   103  			MaxConnAge:     time.Minute * 30,
   104  			WithTLS:        true,
   105  			tlsConfig: &tls.Config{
   106  				InsecureSkipVerify: true,
   107  			},
   108  		},
   109  	}, config)
   110  }
   111  
   112  func Test_WithCluster(t *testing.T) {
   113  	config := DefaultConfig()
   114  	options := append(testOptions, WithCluster(ClusterConfig{
   115  		Addrs: []string{
   116  			"192.168.0.1:7000",
   117  			"192.168.0.1:17000",
   118  		},
   119  		ReadOnly: true, // 开启从库读
   120  	}))
   121  
   122  	for _, option := range options {
   123  		option(config)
   124  	}
   125  
   126  	assert.Equal(t, &Config{
   127  		DeployMode: redisCluster,
   128  		StandaloneConfig: StandaloneConfig{
   129  			KvPasswd:       "polaris",
   130  			MinIdleConns:   1234,
   131  			IdleTimeout:    time.Minute,
   132  			ConnectTimeout: time.Millisecond * 10,
   133  			Concurrency:    20,
   134  			MaxRetry:       5,
   135  			MinBatchCount:  1,
   136  			WaitTime:       time.Millisecond * 50,
   137  			MaxRetries:     5,
   138  			DB:             16,
   139  			ReadTimeout:    time.Millisecond * 200,
   140  			WriteTimeout:   time.Millisecond * 200,
   141  			MsgTimeout:     time.Millisecond * 300,
   142  			PoolSize:       2000,
   143  			PoolTimeout:    time.Second * 3,
   144  			MaxConnAge:     time.Minute * 30,
   145  			WithTLS:        true,
   146  			tlsConfig: &tls.Config{
   147  				InsecureSkipVerify: true,
   148  			},
   149  		},
   150  		ClusterConfig: ClusterConfig{
   151  			Addrs: []string{
   152  				"192.168.0.1:7000",
   153  				"192.168.0.1:17000",
   154  			},
   155  			ReadOnly: true,
   156  		},
   157  	}, config)
   158  }
   159  
   160  func TestWithSentinel(t *testing.T) {
   161  	config := DefaultConfig()
   162  	options := append(testOptions, WithSentinel(SentinelConfig{
   163  		Addrs: []string{
   164  			"192.168.0.1:26379",
   165  			"192.168.0.2:26379",
   166  		},
   167  		MasterName: "sentinel_master_name",
   168  	}))
   169  
   170  	for _, option := range options {
   171  		option(config)
   172  	}
   173  
   174  	assert.Equal(t, &Config{
   175  		DeployMode: redisSentinel,
   176  		StandaloneConfig: StandaloneConfig{
   177  			KvPasswd:       "polaris",
   178  			MinIdleConns:   1234,
   179  			IdleTimeout:    time.Minute,
   180  			ConnectTimeout: time.Millisecond * 10,
   181  			Concurrency:    20,
   182  			MaxRetry:       5,
   183  			MinBatchCount:  1,
   184  			WaitTime:       time.Millisecond * 50,
   185  			MaxRetries:     5,
   186  			DB:             16,
   187  			ReadTimeout:    time.Millisecond * 200,
   188  			WriteTimeout:   time.Millisecond * 200,
   189  			MsgTimeout:     time.Millisecond * 300,
   190  			PoolSize:       2000,
   191  			PoolTimeout:    time.Second * 3,
   192  			MaxConnAge:     time.Minute * 30,
   193  			WithTLS:        true,
   194  			tlsConfig: &tls.Config{
   195  				InsecureSkipVerify: true,
   196  			},
   197  		},
   198  		SentinelConfig: SentinelConfig{
   199  			Addrs: []string{
   200  				"192.168.0.1:26379",
   201  				"192.168.0.2:26379",
   202  			},
   203  			MasterName: "sentinel_master_name",
   204  		},
   205  	}, config)
   206  }