github.com/msales/pkg/v3@v3.24.0/cache/redis_internal_test.go (about)

     1  package cache
     2  
     3  import (
     4  	"testing"
     5  	"time"
     6  
     7  	"github.com/go-redis/redis"
     8  	"github.com/stretchr/testify/assert"
     9  )
    10  
    11  func TestWithPoolSize(t *testing.T) {
    12  	o := &redis.UniversalOptions{}
    13  
    14  	WithPoolSize(12)(o)
    15  
    16  	assert.Equal(t, 12, o.PoolSize)
    17  }
    18  
    19  func TestWithPoolTimeout(t *testing.T) {
    20  	o := &redis.UniversalOptions{}
    21  
    22  	WithPoolTimeout(time.Second)(o)
    23  
    24  	assert.Equal(t, time.Second, o.PoolTimeout)
    25  }
    26  
    27  func TestWithReadTimeout(t *testing.T) {
    28  	o := &redis.UniversalOptions{}
    29  
    30  	WithReadTimeout(time.Second)(o)
    31  
    32  	assert.Equal(t, time.Second, o.ReadTimeout)
    33  }
    34  
    35  func TestWithWriteTimeout(t *testing.T) {
    36  	o := &redis.UniversalOptions{}
    37  
    38  	WithWriteTimeout(time.Second)(o)
    39  
    40  	assert.Equal(t, time.Second, o.WriteTimeout)
    41  }
    42  
    43  func TestNewRedis(t *testing.T) {
    44  	v, err := NewRedis("redis://test", WithPoolSize(12))
    45  	assert.NoError(t, err)
    46  
    47  	c := v.(*redisCache)
    48  	client := c.client.(*redis.Client)
    49  	assert.IsType(t, &redis.Client{}, client)
    50  	assert.Equal(t, 12, client.Options().PoolSize)
    51  }
    52  
    53  func TestNewRedisUniversal_Cluster(t *testing.T) {
    54  	v, err := NewRedisUniversal([]string{"test:8179", "test2:8179"}, WithPoolSize(12))
    55  	assert.NoError(t, err)
    56  
    57  	c := v.(*redisCache)
    58  	clusterClient := c.client.(*redis.ClusterClient)
    59  	assert.IsType(t, &redis.ClusterClient{}, clusterClient)
    60  	assert.Equal(t, 12, clusterClient.Options().PoolSize)
    61  }
    62  
    63  func TestNewRedisUniversal_NonCluster(t *testing.T) {
    64  	v, err := NewRedisUniversal([]string{"test:8179"}, WithPoolSize(12))
    65  	assert.NoError(t, err)
    66  
    67  	c := v.(*redisCache)
    68  	client := c.client.(*redis.Client)
    69  	assert.IsType(t, &redis.Client{}, client)
    70  	assert.Equal(t, 12, client.Options().PoolSize)
    71  }
    72  
    73  func TestNewRedis_InvalidUri(t *testing.T) {
    74  	_, err := NewRedis("test")
    75  	assert.Error(t, err)
    76  }