redis_test.go 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. // Copyright 2017 Gary Burd
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License"): you may
  4. // not use this file except in compliance with the License. You may obtain
  5. // a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  11. // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  12. // License for the specific language governing permissions and limitations
  13. // under the License.
  14. package redis_test
  15. import (
  16. "testing"
  17. "time"
  18. "github.com/garyburd/redigo/redis"
  19. )
  20. type timeoutTestConn int
  21. func (tc timeoutTestConn) Do(string, ...interface{}) (interface{}, error) {
  22. return time.Duration(-1), nil
  23. }
  24. func (tc timeoutTestConn) DoWithTimeout(timeout time.Duration, cmd string, args ...interface{}) (interface{}, error) {
  25. return timeout, nil
  26. }
  27. func (tc timeoutTestConn) Receive() (interface{}, error) {
  28. return time.Duration(-1), nil
  29. }
  30. func (tc timeoutTestConn) ReceiveWithTimeout(timeout time.Duration) (interface{}, error) {
  31. return timeout, nil
  32. }
  33. func (tc timeoutTestConn) Send(string, ...interface{}) error { return nil }
  34. func (tc timeoutTestConn) Err() error { return nil }
  35. func (tc timeoutTestConn) Close() error { return nil }
  36. func (tc timeoutTestConn) Flush() error { return nil }
  37. func testTimeout(t *testing.T, c redis.Conn) {
  38. r, err := c.Do("PING")
  39. if r != time.Duration(-1) || err != nil {
  40. t.Errorf("Do() = %v, %v, want %v, %v", r, err, time.Duration(-1), nil)
  41. }
  42. r, err = redis.DoWithTimeout(c, time.Minute, "PING")
  43. if r != time.Minute || err != nil {
  44. t.Errorf("DoWithTimeout() = %v, %v, want %v, %v", r, err, time.Minute, nil)
  45. }
  46. r, err = c.Receive()
  47. if r != time.Duration(-1) || err != nil {
  48. t.Errorf("Receive() = %v, %v, want %v, %v", r, err, time.Duration(-1), nil)
  49. }
  50. r, err = redis.ReceiveWithTimeout(c, time.Minute)
  51. if r != time.Minute || err != nil {
  52. t.Errorf("ReceiveWithTimeout() = %v, %v, want %v, %v", r, err, time.Minute, nil)
  53. }
  54. }
  55. func TestConnTimeout(t *testing.T) {
  56. testTimeout(t, timeoutTestConn(0))
  57. }
  58. func TestPoolConnTimeout(t *testing.T) {
  59. p := &redis.Pool{Dial: func() (redis.Conn, error) { return timeoutTestConn(0), nil }}
  60. testTimeout(t, p.Get())
  61. }