You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@thrift.apache.org by ro...@apache.org on 2014/07/25 23:28:00 UTC

git commit: THRIFT-2619 Go lib http transport does not handle EOF correctly

Repository: thrift
Updated Branches:
  refs/heads/master da8b60715 -> ce9cf13bb


THRIFT-2619 Go lib http transport does not handle EOF correctly

Patch: Frank Schroeder


Project: http://git-wip-us.apache.org/repos/asf/thrift/repo
Commit: http://git-wip-us.apache.org/repos/asf/thrift/commit/ce9cf13b
Tree: http://git-wip-us.apache.org/repos/asf/thrift/tree/ce9cf13b
Diff: http://git-wip-us.apache.org/repos/asf/thrift/diff/ce9cf13b

Branch: refs/heads/master
Commit: ce9cf13bb30239d3b63ecbf2a8ff769998c0307f
Parents: da8b607
Author: Roger Meier <ro...@apache.org>
Authored: Fri Jul 25 23:20:54 2014 +0200
Committer: Roger Meier <ro...@apache.org>
Committed: Fri Jul 25 23:20:54 2014 +0200

----------------------------------------------------------------------
 lib/go/thrift/http_client.go         |  4 +++
 lib/go/thrift/protocol_test.go       |  2 --
 lib/go/thrift/rich_transport.go      | 13 +++++++---
 lib/go/thrift/rich_transport_test.go | 41 +++++++++++++++++++++++++++++++
 4 files changed, 54 insertions(+), 6 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/thrift/blob/ce9cf13b/lib/go/thrift/http_client.go
----------------------------------------------------------------------
diff --git a/lib/go/thrift/http_client.go b/lib/go/thrift/http_client.go
index cff5ea5..df66897 100644
--- a/lib/go/thrift/http_client.go
+++ b/lib/go/thrift/http_client.go
@@ -21,6 +21,7 @@ package thrift
 
 import (
 	"bytes"
+	"io"
 	"net/http"
 	"net/url"
 	"strconv"
@@ -150,6 +151,9 @@ func (p *THttpClient) Read(buf []byte) (int, error) {
 		return 0, NewTTransportException(NOT_OPEN, "Response buffer is empty, no request.")
 	}
 	n, err := p.response.Body.Read(buf)
+	if n > 0 && (err == nil || err == io.EOF) {
+		return n, nil
+	}
 	return n, NewTTransportExceptionFromError(err)
 }
 

http://git-wip-us.apache.org/repos/asf/thrift/blob/ce9cf13b/lib/go/thrift/protocol_test.go
----------------------------------------------------------------------
diff --git a/lib/go/thrift/protocol_test.go b/lib/go/thrift/protocol_test.go
index 67048fe..7e7950d 100644
--- a/lib/go/thrift/protocol_test.go
+++ b/lib/go/thrift/protocol_test.go
@@ -170,7 +170,6 @@ func ReadWriteProtocolTest(t *testing.T, protocolFactory TProtocolFactory) {
 		ReadWriteBinary(t, p, trans)
 		trans.Close()
 	}
-
 	for _, tf := range transports {
 		trans := tf.GetTransport(nil)
 		p := protocolFactory.GetProtocol(trans)
@@ -180,7 +179,6 @@ func ReadWriteProtocolTest(t *testing.T, protocolFactory TProtocolFactory) {
 		ReadWriteByte(t, p, trans)
 		trans.Close()
 	}
-
 }
 
 func ReadWriteBool(t testing.TB, p TProtocol, trans TTransport) {

http://git-wip-us.apache.org/repos/asf/thrift/blob/ce9cf13b/lib/go/thrift/rich_transport.go
----------------------------------------------------------------------
diff --git a/lib/go/thrift/rich_transport.go b/lib/go/thrift/rich_transport.go
index c409ae0..d7268d9 100644
--- a/lib/go/thrift/rich_transport.go
+++ b/lib/go/thrift/rich_transport.go
@@ -19,9 +19,7 @@
 
 package thrift
 
-import (
-	"io"
-)
+import "io"
 
 type RichTransport struct {
 	TTransport
@@ -46,7 +44,14 @@ func (r *RichTransport) WriteString(s string) (n int, err error) {
 
 func readByte(r io.Reader) (c byte, err error) {
 	v := [1]byte{0}
-	if _, err := r.Read(v[0:1]); err != nil {
+	n, err := r.Read(v[0:1])
+	if n > 0 && (err == nil || err == io.EOF) {
+		return v[0], nil
+	}
+	if n > 0 && err != nil {
+		return v[0], err
+	}
+	if err != nil {
 		return 0, err
 	}
 	return v[0], nil

http://git-wip-us.apache.org/repos/asf/thrift/blob/ce9cf13b/lib/go/thrift/rich_transport_test.go
----------------------------------------------------------------------
diff --git a/lib/go/thrift/rich_transport_test.go b/lib/go/thrift/rich_transport_test.go
index 3241167..41513f8 100644
--- a/lib/go/thrift/rich_transport_test.go
+++ b/lib/go/thrift/rich_transport_test.go
@@ -21,6 +21,8 @@ package thrift
 
 import (
 	"bytes"
+	"errors"
+	"io"
 	"reflect"
 	"testing"
 )
@@ -42,3 +44,42 @@ func TestEnsureTransportsAreRich(t *testing.T) {
 		}
 	}
 }
+
+// TestReadByte tests whether readByte handles error cases correctly.
+func TestReadByte(t *testing.T) {
+	for i, test := range readByteTests {
+		v, err := readByte(test.r)
+		if v != test.v {
+			t.Fatalf("TestReadByte %d: value differs. Expected %d, got %d", i, test.v, test.r.v)
+		}
+		if err != test.err {
+			t.Fatalf("TestReadByte %d: error differs. Expected %s, got %s", i, test.err, test.r.err)
+		}
+	}
+}
+
+var someError = errors.New("Some error")
+var readByteTests = []struct {
+	r   *mockReader
+	v   byte
+	err error
+}{
+	{&mockReader{0, 55, io.EOF}, 0, io.EOF},        // reader sends EOF w/o data
+	{&mockReader{0, 55, someError}, 0, someError},  // reader sends some other error
+	{&mockReader{1, 55, nil}, 55, nil},             // reader sends data w/o error
+	{&mockReader{1, 55, io.EOF}, 55, nil},          // reader sends data with EOF
+	{&mockReader{1, 55, someError}, 55, someError}, // reader sends data withsome error
+}
+
+type mockReader struct {
+	n   int
+	v   byte
+	err error
+}
+
+func (r *mockReader) Read(p []byte) (n int, err error) {
+	if r.n > 0 {
+		p[0] = r.v
+	}
+	return r.n, r.err
+}