...

Source file src/github.com/gomodule/redigo/redis/redis_test.go

Documentation: github.com/gomodule/redigo/redis

     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  
    15  package redis_test
    16  
    17  import (
    18  	"testing"
    19  	"time"
    20  
    21  	"github.com/gomodule/redigo/redis"
    22  )
    23  
    24  type timeoutTestConn int
    25  
    26  func (tc timeoutTestConn) Do(string, ...interface{}) (interface{}, error) {
    27  	return time.Duration(-1), nil
    28  }
    29  func (tc timeoutTestConn) DoWithTimeout(timeout time.Duration, cmd string, args ...interface{}) (interface{}, error) {
    30  	return timeout, nil
    31  }
    32  
    33  func (tc timeoutTestConn) Receive() (interface{}, error) {
    34  	return time.Duration(-1), nil
    35  }
    36  func (tc timeoutTestConn) ReceiveWithTimeout(timeout time.Duration) (interface{}, error) {
    37  	return timeout, nil
    38  }
    39  
    40  func (tc timeoutTestConn) Send(string, ...interface{}) error { return nil }
    41  func (tc timeoutTestConn) Err() error                        { return nil }
    42  func (tc timeoutTestConn) Close() error                      { return nil }
    43  func (tc timeoutTestConn) Flush() error                      { return nil }
    44  
    45  func testTimeout(t *testing.T, c redis.Conn) {
    46  	r, err := c.Do("PING")
    47  	if r != time.Duration(-1) || err != nil {
    48  		t.Errorf("Do() = %v, %v, want %v, %v", r, err, time.Duration(-1), nil)
    49  	}
    50  	r, err = redis.DoWithTimeout(c, time.Minute, "PING")
    51  	if r != time.Minute || err != nil {
    52  		t.Errorf("DoWithTimeout() = %v, %v, want %v, %v", r, err, time.Minute, nil)
    53  	}
    54  	r, err = c.Receive()
    55  	if r != time.Duration(-1) || err != nil {
    56  		t.Errorf("Receive() = %v, %v, want %v, %v", r, err, time.Duration(-1), nil)
    57  	}
    58  	r, err = redis.ReceiveWithTimeout(c, time.Minute)
    59  	if r != time.Minute || err != nil {
    60  		t.Errorf("ReceiveWithTimeout() = %v, %v, want %v, %v", r, err, time.Minute, nil)
    61  	}
    62  }
    63  
    64  func TestConnTimeout(t *testing.T) {
    65  	testTimeout(t, timeoutTestConn(0))
    66  }
    67  
    68  func TestPoolConnTimeout(t *testing.T) {
    69  	p := &redis.Pool{Dial: func() (redis.Conn, error) { return timeoutTestConn(0), nil }}
    70  	testTimeout(t, p.Get())
    71  }
    72  

View as plain text