THRIFT-4285 Move TX/RX methods from gen. code to library
This change removes a lot of duplication from generated code and allows
the caller to customize how they can read from / write to the
transport. Backwards compatible adapters make the change compatible
with existing code in use by consuming applications.
Client: Go
This closes #1382
diff --git a/test/go/src/common/client.go b/test/go/src/common/client.go
index 4251d91..236ce43 100644
--- a/test/go/src/common/client.go
+++ b/test/go/src/common/client.go
@@ -41,7 +41,7 @@
domain_socket string,
transport string,
protocol string,
- ssl bool) (client *thrifttest.ThriftTestClient, err error) {
+ ssl bool) (client *thrifttest.ThriftTestClient, trans thrift.TTransport, err error) {
hostPort := fmt.Sprintf("%s:%d", host, port)
@@ -56,12 +56,11 @@
case "binary":
protocolFactory = thrift.NewTBinaryProtocolFactoryDefault()
default:
- return nil, fmt.Errorf("Invalid protocol specified %s", protocol)
+ return nil, nil, fmt.Errorf("Invalid protocol specified %s", protocol)
}
if debugClientProtocol {
protocolFactory = thrift.NewTDebugProtocolFactory(protocolFactory, "client:")
}
- var trans thrift.TTransport
if ssl {
trans, err = thrift.NewTSSLSocket(hostPort, &tls.Config{InsecureSkipVerify: true})
} else {
@@ -72,7 +71,7 @@
}
}
if err != nil {
- return nil, err
+ return nil, nil, err
}
switch transport {
case "http":
@@ -86,29 +85,25 @@
} else {
trans, err = thrift.NewTHttpPostClient(fmt.Sprintf("http://%s/", hostPort))
}
-
- if err != nil {
- return nil, err
- }
-
case "framed":
trans = thrift.NewTFramedTransport(trans)
case "buffered":
trans = thrift.NewTBufferedTransport(trans, 8192)
case "zlib":
trans, err = thrift.NewTZlibTransport(trans, zlib.BestCompression)
- if err != nil {
- return nil, err
- }
case "":
trans = trans
default:
- return nil, fmt.Errorf("Invalid transport specified %s", transport)
+ return nil, nil, fmt.Errorf("Invalid transport specified %s", transport)
}
-
+ if err != nil {
+ return nil, nil, err
+ }
if err = trans.Open(); err != nil {
- return nil, err
+ return nil, nil, err
}
- client = thrifttest.NewThriftTestClientFactory(trans, protocolFactory)
+ iprot := protocolFactory.GetProtocol(trans)
+ oprot := protocolFactory.GetProtocol(trans)
+ client = thrifttest.NewThriftTestClient(thrift.NewTStandardClient(iprot, oprot))
return
}
diff --git a/test/go/src/common/clientserver_test.go b/test/go/src/common/clientserver_test.go
index ecd021f..c4cfd44 100644
--- a/test/go/src/common/clientserver_test.go
+++ b/test/go/src/common/clientserver_test.go
@@ -23,6 +23,7 @@
"errors"
"gen/thrifttest"
"reflect"
+ "sync"
"testing"
"thrift"
@@ -47,10 +48,15 @@
func TestAllConnection(t *testing.T) {
certPath = "../../../keys"
+ wg := &sync.WaitGroup{}
+ wg.Add(len(units))
for _, unit := range units {
- t.Logf("%#v", unit)
- doUnit(t, &unit)
+ go func(u test_unit) {
+ defer wg.Done()
+ doUnit(t, &u)
+ }(unit)
}
+ wg.Wait()
}
func doUnit(t *testing.T, unit *test_unit) {
@@ -62,17 +68,17 @@
server := thrift.NewTSimpleServer4(processor, serverTransport, transportFactory, protocolFactory)
if err = server.Listen(); err != nil {
- t.Errorf("Unable to start server", err)
- t.FailNow()
+ t.Errorf("Unable to start server: %v", err)
+ return
}
go server.AcceptLoop()
defer server.Stop()
- client, err := StartClient(unit.host, unit.port, unit.domain_socket, unit.transport, unit.protocol, unit.ssl)
+ client, trans, err := StartClient(unit.host, unit.port, unit.domain_socket, unit.transport, unit.protocol, unit.ssl)
if err != nil {
- t.Errorf("Unable to start client", err)
- t.FailNow()
+ t.Errorf("Unable to start client: %v", err)
+ return
}
- defer client.Transport.Close()
+ defer trans.Close()
callEverythingWithMock(t, client, handler)
}
@@ -273,7 +279,7 @@
xxsret, err := client.TestMulti(defaultCtx, 42, 4242, 424242, map[int16]string{1: "blah", 2: "thing"}, thrifttest.Numberz_EIGHT, thrifttest.UserId(24))
if err != nil {
- t.Errorf("Unexpected error in TestMulti() call: ", err)
+ t.Errorf("Unexpected error in TestMulti() call: %v", err)
}
if !reflect.DeepEqual(xxs, xxsret) {
t.Errorf("Unexpected TestMulti() result expected %#v, got %#v ", xxs, xxsret)
@@ -289,9 +295,12 @@
// TODO: connection is being closed on this
err = client.TestException(defaultCtx, "TException")
- tex, ok := err.(thrift.TApplicationException)
- if err == nil || !ok || tex.TypeId() != thrift.INTERNAL_ERROR {
- t.Errorf("Unexpected TestException() result expected ApplicationError, got %#v ", err)
+ if err == nil {
+ t.Error("expected exception got nil")
+ } else if tex, ok := err.(thrift.TApplicationException); !ok {
+ t.Errorf("Unexpected TestException() result expected ApplicationError, got %T ", err)
+ } else if tex.TypeId() != thrift.INTERNAL_ERROR {
+ t.Errorf("expected internal_error got %v", tex.TypeId())
}
ign, err := client.TestMultiException(defaultCtx, "Xception", "ignoreme")