diff options
author | =Corey Hulen <corey@hulen.com> | 2015-11-23 15:53:48 -0800 |
---|---|---|
committer | =Corey Hulen <corey@hulen.com> | 2015-11-23 15:53:48 -0800 |
commit | 4f4cd5e63573da4d6edcc7d4213afaca67c19f88 (patch) | |
tree | cefbc7af53629d97644ca2f6b2369e9d879f0101 /Godeps/_workspace/src/github.com/garyburd/redigo/redis/conn_test.go | |
parent | f8a3c9a14edca6df0647d89cf225f2470cbe025c (diff) | |
download | chat-4f4cd5e63573da4d6edcc7d4213afaca67c19f88.tar.gz chat-4f4cd5e63573da4d6edcc7d4213afaca67c19f88.tar.bz2 chat-4f4cd5e63573da4d6edcc7d4213afaca67c19f88.zip |
upgrading libs
Diffstat (limited to 'Godeps/_workspace/src/github.com/garyburd/redigo/redis/conn_test.go')
-rw-r--r-- | Godeps/_workspace/src/github.com/garyburd/redigo/redis/conn_test.go | 163 |
1 files changed, 140 insertions, 23 deletions
diff --git a/Godeps/_workspace/src/github.com/garyburd/redigo/redis/conn_test.go b/Godeps/_workspace/src/github.com/garyburd/redigo/redis/conn_test.go index 800370136..06a2c8fa7 100644 --- a/Godeps/_workspace/src/github.com/garyburd/redigo/redis/conn_test.go +++ b/Godeps/_workspace/src/github.com/garyburd/redigo/redis/conn_test.go @@ -15,19 +15,37 @@ package redis_test import ( - "bufio" "bytes" + "io" "math" "net" + "os" "reflect" "strings" "testing" "time" - "github.com/garyburd/redigo/internal/redistest" - "github.com/garyburd/redigo/redis" + "github.com/mattermost/platform/Godeps/_workspace/src/github.com/garyburd/redigo/redis" ) +type testConn struct { + io.Reader + io.Writer +} + +func (*testConn) Close() error { return nil } +func (*testConn) LocalAddr() net.Addr { return nil } +func (*testConn) RemoteAddr() net.Addr { return nil } +func (*testConn) SetDeadline(t time.Time) error { return nil } +func (*testConn) SetReadDeadline(t time.Time) error { return nil } +func (*testConn) SetWriteDeadline(t time.Time) error { return nil } + +func dialTestConn(r io.Reader, w io.Writer) redis.DialOption { + return redis.DialNetDial(func(net, addr string) (net.Conn, error) { + return &testConn{Reader: r, Writer: w}, nil + }) +} + var writeTests = []struct { args []interface{} expected string @@ -73,14 +91,13 @@ var writeTests = []struct { func TestWrite(t *testing.T) { for _, tt := range writeTests { var buf bytes.Buffer - rw := bufio.ReadWriter{Writer: bufio.NewWriter(&buf)} - c := redis.NewConnBufio(rw) + c, _ := redis.Dial("", "", dialTestConn(nil, &buf)) err := c.Send(tt.args[0].(string), tt.args[1:]...) if err != nil { t.Errorf("Send(%v) returned error %v", tt.args, err) continue } - rw.Flush() + c.Flush() actual := buf.String() if actual != tt.expected { t.Errorf("Send(%v) = %q, want %q", tt.args, actual, tt.expected) @@ -173,11 +190,7 @@ var readTests = []struct { func TestRead(t *testing.T) { for _, tt := range readTests { - rw := bufio.ReadWriter{ - Reader: bufio.NewReader(strings.NewReader(tt.reply)), - Writer: bufio.NewWriter(nil), // writer need to support Flush - } - c := redis.NewConnBufio(rw) + c, _ := redis.Dial("", "", dialTestConn(strings.NewReader(tt.reply), nil)) actual, err := c.Receive() if tt.expected == errorSentinel { if err == nil { @@ -257,7 +270,7 @@ var testCommands = []struct { } func TestDoCommands(t *testing.T) { - c, err := redistest.Dial() + c, err := redis.DialDefaultServer() if err != nil { t.Fatalf("error connection to database, %v", err) } @@ -276,7 +289,7 @@ func TestDoCommands(t *testing.T) { } func TestPipelineCommands(t *testing.T) { - c, err := redistest.Dial() + c, err := redis.DialDefaultServer() if err != nil { t.Fatalf("error connection to database, %v", err) } @@ -302,7 +315,7 @@ func TestPipelineCommands(t *testing.T) { } func TestBlankCommmand(t *testing.T) { - c, err := redistest.Dial() + c, err := redis.DialDefaultServer() if err != nil { t.Fatalf("error connection to database, %v", err) } @@ -329,7 +342,7 @@ func TestBlankCommmand(t *testing.T) { } func TestRecvBeforeSend(t *testing.T) { - c, err := redistest.Dial() + c, err := redis.DialDefaultServer() if err != nil { t.Fatalf("error connection to database, %v", err) } @@ -350,7 +363,7 @@ func TestRecvBeforeSend(t *testing.T) { } func TestError(t *testing.T) { - c, err := redistest.Dial() + c, err := redis.DialDefaultServer() if err != nil { t.Fatalf("error connection to database, %v", err) } @@ -370,7 +383,7 @@ func TestError(t *testing.T) { } } -func TestReadDeadline(t *testing.T) { +func TestReadTimeout(t *testing.T) { l, err := net.Listen("tcp", "127.0.0.1:0") if err != nil { t.Fatalf("net.Listen returned %v", err) @@ -391,7 +404,9 @@ func TestReadDeadline(t *testing.T) { } }() - c1, err := redis.DialTimeout(l.Addr().Network(), l.Addr().String(), 0, time.Millisecond, 0) + // Do + + c1, err := redis.Dial(l.Addr().Network(), l.Addr().String(), redis.DialReadTimeout(time.Millisecond)) if err != nil { t.Fatalf("redis.Dial returned %v", err) } @@ -405,7 +420,9 @@ func TestReadDeadline(t *testing.T) { t.Fatalf("c1.Err() = nil, expect error") } - c2, err := redis.DialTimeout(l.Addr().Network(), l.Addr().String(), 0, time.Millisecond, 0) + // Send/Flush/Receive + + c2, err := redis.Dial(l.Addr().Network(), l.Addr().String(), redis.DialReadTimeout(time.Millisecond)) if err != nil { t.Fatalf("redis.Dial returned %v", err) } @@ -422,6 +439,95 @@ func TestReadDeadline(t *testing.T) { } } +var dialErrors = []struct { + rawurl string + expectedError string +}{ + { + "localhost", + "invalid redis URL scheme", + }, + // The error message for invalid hosts is diffferent in different + // versions of Go, so just check that there is an error message. + { + "redis://weird url", + "", + }, + { + "redis://foo:bar:baz", + "", + }, + { + "http://www.google.com", + "invalid redis URL scheme: http", + }, + { + "redis://localhost:6379/abc123", + "invalid database: abc123", + }, +} + +func TestDialURLErrors(t *testing.T) { + for _, d := range dialErrors { + _, err := redis.DialURL(d.rawurl) + if err == nil || !strings.Contains(err.Error(), d.expectedError) { + t.Errorf("DialURL did not return expected error (expected %v to contain %s)", err, d.expectedError) + } + } +} + +func TestDialURLPort(t *testing.T) { + checkPort := func(network, address string) (net.Conn, error) { + if address != "localhost:6379" { + t.Errorf("DialURL did not set port to 6379 by default (got %v)", address) + } + return nil, nil + } + _, err := redis.DialURL("redis://localhost", redis.DialNetDial(checkPort)) + if err != nil { + t.Error("dial error:", err) + } +} + +func TestDialURLHost(t *testing.T) { + checkHost := func(network, address string) (net.Conn, error) { + if address != "localhost:6379" { + t.Errorf("DialURL did not set host to localhost by default (got %v)", address) + } + return nil, nil + } + _, err := redis.DialURL("redis://:6379", redis.DialNetDial(checkHost)) + if err != nil { + t.Error("dial error:", err) + } +} + +func TestDialURLPassword(t *testing.T) { + var buf bytes.Buffer + _, err := redis.DialURL("redis://x:abc123@localhost", dialTestConn(strings.NewReader("+OK\r\n"), &buf)) + if err != nil { + t.Error("dial error:", err) + } + expected := "*2\r\n$4\r\nAUTH\r\n$6\r\nabc123\r\n" + actual := buf.String() + if actual != expected { + t.Errorf("commands = %q, want %q", actual, expected) + } +} + +func TestDialURLDatabase(t *testing.T) { + var buf bytes.Buffer + _, err := redis.DialURL("redis://localhost/3", dialTestConn(strings.NewReader("+OK\r\n"), &buf)) + if err != nil { + t.Error("dial error:", err) + } + expected := "*2\r\n$6\r\nSELECT\r\n$1\r\n3\r\n" + actual := buf.String() + if actual != expected { + t.Errorf("commands = %q, want %q", actual, expected) + } +} + // Connect to local instance of Redis running on the default port. func ExampleDial(x int) { c, err := redis.Dial("tcp", ":6379") @@ -431,11 +537,20 @@ func ExampleDial(x int) { defer c.Close() } +// Connect to remote instance of Redis using a URL. +func ExampleDialURL() { + c, err := redis.DialURL(os.Getenv("REDIS_URL")) + if err != nil { + // handle connection error + } + defer c.Close() +} + // TextExecError tests handling of errors in a transaction. See // http://redis.io/topics/transactions for information on how Redis handles // errors in a transaction. func TestExecError(t *testing.T) { - c, err := redistest.Dial() + c, err := redis.DialDefaultServer() if err != nil { t.Fatalf("error connection to database, %v", err) } @@ -443,6 +558,7 @@ func TestExecError(t *testing.T) { // Execute commands that fail before EXEC is called. + c.Do("DEL", "k0") c.Do("ZADD", "k0", 0, 0) c.Send("MULTI") c.Send("NOTACOMMAND", "k0", 0, 0) @@ -455,6 +571,7 @@ func TestExecError(t *testing.T) { // Execute commands that fail after EXEC is called. The first command // returns an error. + c.Do("DEL", "k1") c.Do("ZADD", "k1", 0, 0) c.Send("MULTI") c.Send("HSET", "k1", 0, 0) @@ -478,7 +595,7 @@ func TestExecError(t *testing.T) { } if _, ok := vs[1].([]byte); !ok { - t.Fatalf("second result is type %T, expected []byte", vs[2]) + t.Fatalf("second result is type %T, expected []byte", vs[1]) } // Execute commands that fail after EXEC is called. The second command @@ -513,7 +630,7 @@ func TestExecError(t *testing.T) { func BenchmarkDoEmpty(b *testing.B) { b.StopTimer() - c, err := redistest.Dial() + c, err := redis.DialDefaultServer() if err != nil { b.Fatal(err) } @@ -528,7 +645,7 @@ func BenchmarkDoEmpty(b *testing.B) { func BenchmarkDoPing(b *testing.B) { b.StopTimer() - c, err := redistest.Dial() + c, err := redis.DialDefaultServer() if err != nil { b.Fatal(err) } |