options_test.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. // +build go1.7
  2. package redis
  3. import (
  4. "errors"
  5. "testing"
  6. "time"
  7. )
  8. func TestParseURL(t *testing.T) {
  9. cases := []struct {
  10. u string
  11. addr string
  12. db int
  13. tls bool
  14. err error
  15. }{
  16. {
  17. "redis://localhost:123/1",
  18. "localhost:123",
  19. 1, false, nil,
  20. },
  21. {
  22. "redis://localhost:123",
  23. "localhost:123",
  24. 0, false, nil,
  25. },
  26. {
  27. "redis://localhost/1",
  28. "localhost:6379",
  29. 1, false, nil,
  30. },
  31. {
  32. "redis://12345",
  33. "12345:6379",
  34. 0, false, nil,
  35. },
  36. {
  37. "rediss://localhost:123",
  38. "localhost:123",
  39. 0, true, nil,
  40. },
  41. {
  42. "redis://localhost/?abc=123",
  43. "",
  44. 0, false, errors.New("no options supported"),
  45. },
  46. {
  47. "http://google.com",
  48. "",
  49. 0, false, errors.New("invalid redis URL scheme: http"),
  50. },
  51. {
  52. "redis://localhost/1/2/3/4",
  53. "",
  54. 0, false, errors.New("invalid redis URL path: /1/2/3/4"),
  55. },
  56. {
  57. "12345",
  58. "",
  59. 0, false, errors.New("invalid redis URL scheme: "),
  60. },
  61. {
  62. "redis://localhost/iamadatabase",
  63. "",
  64. 0, false, errors.New(`invalid redis database number: "iamadatabase"`),
  65. },
  66. }
  67. for _, c := range cases {
  68. t.Run(c.u, func(t *testing.T) {
  69. o, err := ParseURL(c.u)
  70. if c.err == nil && err != nil {
  71. t.Fatalf("unexpected error: %q", err)
  72. return
  73. }
  74. if c.err != nil && err != nil {
  75. if c.err.Error() != err.Error() {
  76. t.Fatalf("got %q, expected %q", err, c.err)
  77. }
  78. return
  79. }
  80. if o.Addr != c.addr {
  81. t.Errorf("got %q, want %q", o.Addr, c.addr)
  82. }
  83. if o.DB != c.db {
  84. t.Errorf("got %q, expected %q", o.DB, c.db)
  85. }
  86. if c.tls && o.TLSConfig == nil {
  87. t.Errorf("got nil TLSConfig, expected a TLSConfig")
  88. }
  89. })
  90. }
  91. }
  92. // Test ReadTimeout option initialization, including special values -1 and 0.
  93. // And also test behaviour of WriteTimeout option, when it is not explicitly set and use
  94. // ReadTimeout value.
  95. func TestReadTimeoutOptions(t *testing.T) {
  96. testDataInputOutputMap := map[time.Duration]time.Duration{
  97. -1: 0 * time.Second,
  98. 0: 3 * time.Second,
  99. 1: 1 * time.Nanosecond,
  100. 3: 3 * time.Nanosecond,
  101. }
  102. for in, out := range testDataInputOutputMap {
  103. o := &Options{ReadTimeout: in}
  104. o.init()
  105. if o.ReadTimeout != out {
  106. t.Errorf("got %d instead of %d as ReadTimeout option", o.ReadTimeout, out)
  107. }
  108. if o.WriteTimeout != o.ReadTimeout {
  109. t.Errorf("got %d instead of %d as WriteTimeout option", o.WriteTimeout, o.ReadTimeout)
  110. }
  111. }
  112. }