Selaa lähdekoodia

Fix connection Err() bugs.

- Don't set permanent error for reply error values.
- Actually set the permanent error. It was never set before.
Gary Burd 13 vuotta sitten
vanhempi
commit
30ad260b4b
4 muutettua tiedostoa jossa 34 lisäystä ja 22 poistoa
  1. 4 6
      redis/conn.go
  2. 28 8
      redis/conn_test.go
  3. 1 4
      redis/reply_test.go
  4. 1 4
      redis/script_test.go

+ 4 - 6
redis/conn.go

@@ -85,7 +85,7 @@ func (c *conn) Err() error {
 
 func (c *conn) fatal(err error) error {
 	c.mu.Lock()
-	if c.err != nil {
+	if c.err == nil {
 		c.err = err
 	}
 	c.mu.Unlock()
@@ -250,13 +250,11 @@ func (c *conn) Receive() (interface{}, error) {
 		return nil, c.fatal(err)
 	}
 	v, err := c.parseReply()
-	if err == nil {
-		if e, ok := v.(Error); ok {
-			err = e
-		}
-	}
 	if err != nil {
 		return nil, c.fatal(err)
 	}
+	if err, ok := v.(Error); ok {
+		return nil, err
+	}
 	return v, nil
 }

+ 28 - 8
redis/conn_test.go

@@ -175,6 +175,14 @@ func dial() (redis.Conn, error) {
 	return testConn{c}, nil
 }
 
+func dialt(t *testing.T) redis.Conn {
+	c, err := dial()
+	if err != nil {
+		t.Fatalf("error connection to database, %v", err)
+	}
+	return c
+}
+
 var testCommands = []struct {
 	args     []interface{}
 	expected interface{}
@@ -237,10 +245,7 @@ var testCommands = []struct {
 }
 
 func TestDoCommands(t *testing.T) {
-	c, err := dial()
-	if err != nil {
-		t.Fatalf("Error connection to database, %v", err)
-	}
+	c := dialt(t)
 	defer c.Close()
 
 	for _, cmd := range testCommands {
@@ -256,10 +261,7 @@ func TestDoCommands(t *testing.T) {
 }
 
 func TestPipelineCommands(t *testing.T) {
-	c, err := dial()
-	if err != nil {
-		t.Fatalf("Error connection to database, %v", err)
-	}
+	c := dialt(t)
 	defer c.Close()
 
 	for _, cmd := range testCommands {
@@ -280,3 +282,21 @@ func TestPipelineCommands(t *testing.T) {
 		}
 	}
 }
+
+func TestError(t *testing.T) {
+	c := dialt(t)
+	defer c.Close()
+
+	c.Do("SET", "key", "val")
+	_, err := c.Do("HSET", "key", "fld", "val")
+	if err == nil {
+		t.Errorf("Expected err for HSET on string key.")
+	}
+	if c.Err() != nil {
+		t.Errorf("Conn has Err()=%v, expect nil", c.Err())
+	}
+	_, err = c.Do("SET", "key", "val")
+	if err != nil {
+		t.Errorf("Do(SET, key, val) returned error %v, expected nil.", err)
+	}
+}

+ 1 - 4
redis/reply_test.go

@@ -98,10 +98,7 @@ func expectNotification(t *testing.T, c redis.Conn, message string, expected int
 }
 
 func TestNotification(t *testing.T) {
-	pc, err := dial()
-	if err != nil {
-		t.Fatal(err)
-	}
+	pc := dialt(t)
 	defer pc.Close()
 
 	nc, err := net.Dial("tcp", ":6379")

+ 1 - 4
redis/script_test.go

@@ -33,10 +33,7 @@ func ExampleScript(c redis.Conn, reply interface{}, err error) {
 }
 
 func TestScript(t *testing.T) {
-	c, err := dial()
-	if err != nil {
-		t.Fatal(err)
-	}
+	c := dialt(t)
 	defer c.Close()
 
 	// To test fallback in Do, we make script unique by adding comment with current time.