...

Source file src/github.com/redis/go-redis/v9/options_test.go

Documentation: github.com/redis/go-redis/v9

     1  //go:build go1.7
     2  
     3  package redis
     4  
     5  import (
     6  	"crypto/tls"
     7  	"errors"
     8  	"testing"
     9  	"time"
    10  )
    11  
    12  func TestParseURL(t *testing.T) {
    13  	cases := []struct {
    14  		url string
    15  		o   *Options // expected value
    16  		err error
    17  	}{
    18  		{
    19  			url: "redis://localhost:123/1",
    20  			o:   &Options{Addr: "localhost:123", DB: 1},
    21  		}, {
    22  			url: "redis://localhost:123",
    23  			o:   &Options{Addr: "localhost:123"},
    24  		}, {
    25  			url: "redis://localhost/1",
    26  			o:   &Options{Addr: "localhost:6379", DB: 1},
    27  		}, {
    28  			url: "redis://12345",
    29  			o:   &Options{Addr: "12345:6379"},
    30  		}, {
    31  			url: "rediss://localhost:123",
    32  			o:   &Options{Addr: "localhost:123", TLSConfig: &tls.Config{ /* no deep comparison */ }},
    33  		}, {
    34  			url: "rediss://localhost:123/?skip_verify=true",
    35  			o:   &Options{Addr: "localhost:123", TLSConfig: &tls.Config{InsecureSkipVerify: true}},
    36  		}, {
    37  			url: "redis://:bar@localhost:123",
    38  			o:   &Options{Addr: "localhost:123", Password: "bar"},
    39  		}, {
    40  			url: "redis://foo@localhost:123",
    41  			o:   &Options{Addr: "localhost:123", Username: "foo"},
    42  		}, {
    43  			url: "redis://foo:bar@localhost:123",
    44  			o:   &Options{Addr: "localhost:123", Username: "foo", Password: "bar"},
    45  		}, {
    46  			// multiple params
    47  			url: "redis://localhost:123/?db=2&read_timeout=2&pool_fifo=true",
    48  			o:   &Options{Addr: "localhost:123", DB: 2, ReadTimeout: 2 * time.Second, PoolFIFO: true},
    49  		}, {
    50  			// special case handling for disabled timeouts
    51  			url: "redis://localhost:123/?db=2&conn_max_idle_time=0",
    52  			o:   &Options{Addr: "localhost:123", DB: 2, ConnMaxIdleTime: -1},
    53  		}, {
    54  			// negative values disable timeouts as well
    55  			url: "redis://localhost:123/?db=2&conn_max_idle_time=-1",
    56  			o:   &Options{Addr: "localhost:123", DB: 2, ConnMaxIdleTime: -1},
    57  		}, {
    58  			// absent timeout values will use defaults
    59  			url: "redis://localhost:123/?db=2&conn_max_idle_time=",
    60  			o:   &Options{Addr: "localhost:123", DB: 2, ConnMaxIdleTime: 0},
    61  		}, {
    62  			url: "redis://localhost:123/?db=2&conn_max_idle_time", // missing "=" at the end
    63  			o:   &Options{Addr: "localhost:123", DB: 2, ConnMaxIdleTime: 0},
    64  		}, {
    65  			url: "redis://localhost:123/?db=2&client_name=hi", // client name
    66  			o:   &Options{Addr: "localhost:123", DB: 2, ClientName: "hi"},
    67  		}, {
    68  			url: "redis://localhost:123/?db=2&protocol=2", // RESP Protocol
    69  			o:   &Options{Addr: "localhost:123", DB: 2, Protocol: 2},
    70  		}, {
    71  			url: "unix:///tmp/redis.sock",
    72  			o:   &Options{Addr: "/tmp/redis.sock"},
    73  		}, {
    74  			url: "unix://foo:bar@/tmp/redis.sock",
    75  			o:   &Options{Addr: "/tmp/redis.sock", Username: "foo", Password: "bar"},
    76  		}, {
    77  			url: "unix://foo:bar@/tmp/redis.sock?db=3",
    78  			o:   &Options{Addr: "/tmp/redis.sock", Username: "foo", Password: "bar", DB: 3},
    79  		}, {
    80  			// invalid db format
    81  			url: "unix://foo:bar@/tmp/redis.sock?db=test",
    82  			err: errors.New(`redis: invalid database number: strconv.Atoi: parsing "test": invalid syntax`),
    83  		}, {
    84  			// invalid int value
    85  			url: "redis://localhost/?pool_size=five",
    86  			err: errors.New(`redis: invalid pool_size number: strconv.Atoi: parsing "five": invalid syntax`),
    87  		}, {
    88  			// invalid bool value
    89  			url: "redis://localhost/?pool_fifo=yes",
    90  			err: errors.New(`redis: invalid pool_fifo boolean: expected true/false/1/0 or an empty string, got "yes"`),
    91  		}, {
    92  			// it returns first error
    93  			url: "redis://localhost/?db=foo&pool_size=five",
    94  			err: errors.New(`redis: invalid database number: strconv.Atoi: parsing "foo": invalid syntax`),
    95  		}, {
    96  			url: "redis://localhost/?abc=123",
    97  			err: errors.New("redis: unexpected option: abc"),
    98  		}, {
    99  			url: "redis://foo@localhost/?username=bar",
   100  			err: errors.New("redis: unexpected option: username"),
   101  		}, {
   102  			url: "redis://localhost/?wrte_timout=10s&abc=123",
   103  			err: errors.New("redis: unexpected option: abc, wrte_timout"),
   104  		}, {
   105  			url: "http://google.com",
   106  			err: errors.New("redis: invalid URL scheme: http"),
   107  		}, {
   108  			url: "redis://localhost/1/2/3/4",
   109  			err: errors.New("redis: invalid URL path: /1/2/3/4"),
   110  		}, {
   111  			url: "12345",
   112  			err: errors.New("redis: invalid URL scheme: "),
   113  		}, {
   114  			url: "redis://localhost/iamadatabase",
   115  			err: errors.New(`redis: invalid database number: "iamadatabase"`),
   116  		},
   117  	}
   118  
   119  	for i := range cases {
   120  		tc := cases[i]
   121  		t.Run(tc.url, func(t *testing.T) {
   122  			t.Parallel()
   123  
   124  			actual, err := ParseURL(tc.url)
   125  			if tc.err == nil && err != nil {
   126  				t.Fatalf("unexpected error: %q", err)
   127  				return
   128  			}
   129  			if tc.err != nil && err != nil {
   130  				if tc.err.Error() != err.Error() {
   131  					t.Fatalf("got %q, expected %q", err, tc.err)
   132  				}
   133  				return
   134  			}
   135  			comprareOptions(t, actual, tc.o)
   136  		})
   137  	}
   138  }
   139  
   140  func comprareOptions(t *testing.T, actual, expected *Options) {
   141  	t.Helper()
   142  
   143  	if actual.Addr != expected.Addr {
   144  		t.Errorf("got %q, want %q", actual.Addr, expected.Addr)
   145  	}
   146  	if actual.DB != expected.DB {
   147  		t.Errorf("DB: got %q, expected %q", actual.DB, expected.DB)
   148  	}
   149  	if actual.TLSConfig == nil && expected.TLSConfig != nil {
   150  		t.Errorf("got nil TLSConfig, expected a TLSConfig")
   151  	}
   152  	if actual.TLSConfig != nil && expected.TLSConfig == nil {
   153  		t.Errorf("got TLSConfig, expected no TLSConfig")
   154  	}
   155  	if actual.Username != expected.Username {
   156  		t.Errorf("Username: got %q, expected %q", actual.Username, expected.Username)
   157  	}
   158  	if actual.Password != expected.Password {
   159  		t.Errorf("Password: got %q, expected %q", actual.Password, expected.Password)
   160  	}
   161  	if actual.MaxRetries != expected.MaxRetries {
   162  		t.Errorf("MaxRetries: got %v, expected %v", actual.MaxRetries, expected.MaxRetries)
   163  	}
   164  	if actual.MinRetryBackoff != expected.MinRetryBackoff {
   165  		t.Errorf("MinRetryBackoff: got %v, expected %v", actual.MinRetryBackoff, expected.MinRetryBackoff)
   166  	}
   167  	if actual.MaxRetryBackoff != expected.MaxRetryBackoff {
   168  		t.Errorf("MaxRetryBackoff: got %v, expected %v", actual.MaxRetryBackoff, expected.MaxRetryBackoff)
   169  	}
   170  	if actual.DialTimeout != expected.DialTimeout {
   171  		t.Errorf("DialTimeout: got %v, expected %v", actual.DialTimeout, expected.DialTimeout)
   172  	}
   173  	if actual.ReadTimeout != expected.ReadTimeout {
   174  		t.Errorf("ReadTimeout: got %v, expected %v", actual.ReadTimeout, expected.ReadTimeout)
   175  	}
   176  	if actual.WriteTimeout != expected.WriteTimeout {
   177  		t.Errorf("WriteTimeout: got %v, expected %v", actual.WriteTimeout, expected.WriteTimeout)
   178  	}
   179  	if actual.PoolFIFO != expected.PoolFIFO {
   180  		t.Errorf("PoolFIFO: got %v, expected %v", actual.PoolFIFO, expected.PoolFIFO)
   181  	}
   182  	if actual.PoolSize != expected.PoolSize {
   183  		t.Errorf("PoolSize: got %v, expected %v", actual.PoolSize, expected.PoolSize)
   184  	}
   185  	if actual.PoolTimeout != expected.PoolTimeout {
   186  		t.Errorf("PoolTimeout: got %v, expected %v", actual.PoolTimeout, expected.PoolTimeout)
   187  	}
   188  	if actual.MinIdleConns != expected.MinIdleConns {
   189  		t.Errorf("MinIdleConns: got %v, expected %v", actual.MinIdleConns, expected.MinIdleConns)
   190  	}
   191  	if actual.MaxIdleConns != expected.MaxIdleConns {
   192  		t.Errorf("MaxIdleConns: got %v, expected %v", actual.MaxIdleConns, expected.MaxIdleConns)
   193  	}
   194  	if actual.ConnMaxIdleTime != expected.ConnMaxIdleTime {
   195  		t.Errorf("ConnMaxIdleTime: got %v, expected %v", actual.ConnMaxIdleTime, expected.ConnMaxIdleTime)
   196  	}
   197  	if actual.ConnMaxLifetime != expected.ConnMaxLifetime {
   198  		t.Errorf("ConnMaxLifetime: got %v, expected %v", actual.ConnMaxLifetime, expected.ConnMaxLifetime)
   199  	}
   200  }
   201  
   202  // Test ReadTimeout option initialization, including special values -1 and 0.
   203  // And also test behaviour of WriteTimeout option, when it is not explicitly set and use
   204  // ReadTimeout value.
   205  func TestReadTimeoutOptions(t *testing.T) {
   206  	testDataInputOutputMap := map[time.Duration]time.Duration{
   207  		-1: 0 * time.Second,
   208  		0:  3 * time.Second,
   209  		1:  1 * time.Nanosecond,
   210  		3:  3 * time.Nanosecond,
   211  	}
   212  
   213  	for in, out := range testDataInputOutputMap {
   214  		o := &Options{ReadTimeout: in}
   215  		o.init()
   216  		if o.ReadTimeout != out {
   217  			t.Errorf("got %d instead of %d as ReadTimeout option", o.ReadTimeout, out)
   218  		}
   219  
   220  		if o.WriteTimeout != o.ReadTimeout {
   221  			t.Errorf("got %d instead of %d as WriteTimeout option", o.WriteTimeout, o.ReadTimeout)
   222  		}
   223  	}
   224  }
   225  
   226  func TestProtocolOptions(t *testing.T) {
   227  	testCasesMap := map[int]int{
   228  		0: 3,
   229  		1: 3,
   230  		2: 2,
   231  		3: 3,
   232  	}
   233  
   234  	o := &Options{}
   235  	o.init()
   236  	if o.Protocol != 3 {
   237  		t.Errorf("got %d instead of %d as protocol option", o.Protocol, 3)
   238  	}
   239  
   240  	for set, want := range testCasesMap {
   241  		o := &Options{Protocol: set}
   242  		o.init()
   243  		if o.Protocol != want {
   244  			t.Errorf("got %d instead of %d as protocol option", o.Protocol, want)
   245  		}
   246  	}
   247  }
   248  

View as plain text