github.com/anycable/anycable-go@v1.5.1/redis/config_test.go (about)

     1  package redis
     2  
     3  import (
     4  	"testing"
     5  	"time"
     6  
     7  	"github.com/stretchr/testify/assert"
     8  	"github.com/stretchr/testify/require"
     9  )
    10  
    11  func TestBasic(t *testing.T) {
    12  	config := NewRedisConfig()
    13  	config.URL = "redis://localhost:6379"
    14  	options, err := config.ToRueidisOptions()
    15  	require.NoError(t, err)
    16  
    17  	assert.False(t, config.IsCluster())
    18  	assert.False(t, config.IsSentinel())
    19  
    20  	assert.Equal(t, "localhost:6379", config.Hostname())
    21  	assert.Equal(t, []string{"localhost:6379"}, config.Hostnames())
    22  
    23  	assert.Equal(t, []string{"localhost:6379"}, options.InitAddress)
    24  	assert.Equal(t, 0, options.SelectDB)
    25  	assert.Equal(t, 30*time.Second, options.Dialer.KeepAlive)
    26  	assert.False(t, options.ShuffleInit)
    27  	assert.Nil(t, options.TLSConfig)
    28  }
    29  
    30  func TestTrailingSlashHostname(t *testing.T) {
    31  	config := NewRedisConfig()
    32  	config.URL = "redis://localhost:6379/"
    33  	options, err := config.ToRueidisOptions()
    34  	require.NoError(t, err)
    35  
    36  	assert.Equal(t, []string{"localhost:6379"}, options.InitAddress)
    37  	assert.Equal(t, 0, options.SelectDB)
    38  }
    39  
    40  func TestCustomDatabase(t *testing.T) {
    41  	config := NewRedisConfig()
    42  	config.URL = "redis://localhost:6379/1"
    43  	options, err := config.ToRueidisOptions()
    44  	require.NoError(t, err)
    45  
    46  	assert.Equal(t, 1, options.SelectDB)
    47  }
    48  
    49  func TestCustomOptions(t *testing.T) {
    50  	config := NewRedisConfig()
    51  	config.URL = "redis://localhost:6379/1?dial_timeout=30s"
    52  	options, err := config.ToRueidisOptions()
    53  	require.NoError(t, err)
    54  
    55  	assert.Equal(t, 30*time.Second, options.Dialer.Timeout)
    56  }
    57  
    58  func TestTLS(t *testing.T) {
    59  	config := NewRedisConfig()
    60  	config.URL = "rediss://localhost:6379/1"
    61  	options, err := config.ToRueidisOptions()
    62  	require.NoError(t, err)
    63  
    64  	assert.True(t, options.TLSConfig.InsecureSkipVerify)
    65  
    66  	config.TLSVerify = true
    67  	options, err = config.ToRueidisOptions()
    68  	require.NoError(t, err)
    69  
    70  	assert.False(t, options.TLSConfig.InsecureSkipVerify)
    71  }
    72  
    73  func TestAuth(t *testing.T) {
    74  	config := NewRedisConfig()
    75  	config.URL = "redis://user:pass@localhost:6379/1"
    76  	options, err := config.ToRueidisOptions()
    77  	require.NoError(t, err)
    78  
    79  	assert.Equal(t, "user", options.Username)
    80  	assert.Equal(t, "pass", options.Password)
    81  }
    82  
    83  func TestCluster(t *testing.T) {
    84  	config := NewRedisConfig()
    85  	config.URL = "redis://localhost:6379/1,redis://localhost:6389/1"
    86  	options, err := config.ToRueidisOptions()
    87  	require.NoError(t, err)
    88  
    89  	assert.True(t, config.IsCluster())
    90  	assert.False(t, config.IsSentinel())
    91  
    92  	assert.Equal(t, "localhost:6379", config.Hostname())
    93  	assert.Equal(t, []string{"localhost:6379", "localhost:6389"}, config.Hostnames())
    94  
    95  	assert.Equal(t, []string{"localhost:6379", "localhost:6389"}, options.InitAddress)
    96  	assert.True(t, options.ShuffleInit)
    97  }
    98  
    99  func TestClusterShortSyntax(t *testing.T) {
   100  	config := NewRedisConfig()
   101  	config.URL = "redis://localhost:6379/1,localhost:6389/1"
   102  	options, err := config.ToRueidisOptions()
   103  	require.NoError(t, err)
   104  
   105  	assert.True(t, config.IsCluster())
   106  	assert.False(t, config.IsSentinel())
   107  
   108  	assert.Equal(t, "localhost:6379", config.Hostname())
   109  	assert.Equal(t, []string{"localhost:6379", "localhost:6389"}, config.Hostnames())
   110  
   111  	assert.Equal(t, []string{"localhost:6379", "localhost:6389"}, options.InitAddress)
   112  	assert.True(t, options.ShuffleInit)
   113  }
   114  
   115  func TestSentinel(t *testing.T) {
   116  	config := NewRedisConfig()
   117  	config.URL = "redis://master-name"
   118  	config.Sentinels = "user:pass@localhost:1234,localhost:1235"
   119  	options, err := config.ToRueidisOptions()
   120  	require.NoError(t, err)
   121  
   122  	assert.False(t, config.IsCluster())
   123  	assert.True(t, config.IsSentinel())
   124  
   125  	assert.Equal(t, "master-name", config.Hostname())
   126  	assert.Equal(t, []string{"localhost:1234", "localhost:1235"}, config.Hostnames())
   127  
   128  	assert.Equal(t, []string{"localhost:1234", "localhost:1235"}, options.InitAddress)
   129  	assert.Equal(t, "user", options.Username)
   130  	assert.Equal(t, "pass", options.Password)
   131  }
   132  
   133  func TestSentinelImplicitFormat(t *testing.T) {
   134  	config := NewRedisConfig()
   135  	config.URL = "redis://user:pass@localhost:1234?master_set=master-name"
   136  	options, err := config.ToRueidisOptions()
   137  	require.NoError(t, err)
   138  
   139  	assert.False(t, config.IsCluster())
   140  	assert.True(t, config.IsSentinel())
   141  
   142  	assert.Equal(t, "master-name", config.Hostname())
   143  	assert.Equal(t, []string{"localhost:1234"}, config.Hostnames())
   144  
   145  	assert.Equal(t, []string{"localhost:1234"}, options.InitAddress)
   146  	assert.Equal(t, "user", options.Username)
   147  	assert.Equal(t, "pass", options.Password)
   148  }
   149  
   150  func TestDefaultScheme(t *testing.T) {
   151  	config := NewRedisConfig()
   152  	config.URL = "localhost"
   153  	options, err := config.ToRueidisOptions()
   154  	require.NoError(t, err)
   155  
   156  	assert.Equal(t, "localhost:6379", config.Hostname())
   157  	assert.Equal(t, []string{"localhost:6379"}, config.Hostnames())
   158  
   159  	assert.Equal(t, []string{"localhost:6379"}, options.InitAddress)
   160  }
   161  
   162  func TestInvalidURL(t *testing.T) {
   163  	config := NewRedisConfig()
   164  	config.URL = "invalid://"
   165  	_, err := config.ToRueidisOptions()
   166  	require.Error(t, err)
   167  }