From f459868f939e702eb71cdb6202d1d725e3132c89 Mon Sep 17 00:00:00 2001 From: Jens Geyer Date: Thu, 8 May 2014 23:18:44 +0200 Subject: [PATCH] THRIFT-2497 server and client for test/go, also several fixes and improvements Client: Go Patch: Aleksey Pesternikov This closes #109 commit f2e7186ca8d63f407dba0c56ee51afd6405926ba Author: Aleksey Pesternikov Date: 2014-04-22T12:48:14Z add _ to generated filename if it ends with _test.go commit a6ed88196fbf4622a3b0261bbac0fe6b258bdd36 Author: Aleksey Pesternikov Date: 2014-04-22T14:19:13Z use tcxx instead of tr1 in test/cpp commit d1848fa05f4baefc66eb405f4b1a8657bb2869bd Author: Aleksey Pesternikov Date: 2014-04-23T01:01:03Z Merge branch 'master' of https://github.com/apache/thrift into go_integration commit 04d22fef228d3f868b32a296a38f99ff52ee2142 Author: Aleksey Pesternikov Date: 2014-04-25T00:34:24Z additions: test for client/server with several protocol/transport/ssl combinations bin/testclient and bin/testserver debug_transport fixes: separate Listen() and AcceptLoop() instead of Serve() in SimpleServer if handler function returns any unknown exception, handler closes connection commit ed88d57d977cffea9fac8f61143801f3097ef46c Author: Aleksey Pesternikov Date: 2014-04-25T13:29:54Z Generate lowercase package name lowercased pkg name in thrift files underline() -> lowercase() commit 498a910c6c6753a4b1e3633eb5c9c82be33e8c7b Author: Aleksey Pesternikov Date: 2014-04-25T13:32:26Z Merge branch 'master' into go_test_service_name commit 25792d7218a2c32ee3c2077f65ca7d0cea7f31f5 Author: Aleksey Pesternikov Date: 2014-04-22T12:48:14Z add _ to generated filename if it ends with _test.go commit af994b415308a2e174d57a03675fc83d7cbd43d1 Author: Aleksey Pesternikov Date: 2014-04-25T00:34:24Z additions: test for client/server with several protocol/transport/ssl combinations bin/testclient and bin/testserver debug_transport fixes: separate Listen() and AcceptLoop() instead of Serve() in SimpleServer if handler function returns any unknown exception, handler closes connection commit f22a777a5d5b4d93a8d7981e7aadc2c63919518a Author: Aleksey Pesternikov Date: 2014-04-25T13:44:08Z Merge branch 'go_integration' of github.com:apesternikov/thrift into go_integration commit 49a33c8c80ea35f923ee9aa3577780fcf41fe840 Author: Aleksey Pesternikov Date: 2014-04-25T13:49:05Z Merge branch 'master' into test_cpp_on_mac commit 81b402ec6301f6b132c9b346ad5481f55f0aff89 Author: Aleksey Pesternikov Date: 2014-04-25T13:56:56Z operator < for ThriftTest commit cde312d940d12077274dd0ba677ca850b5b439a7 Author: Aleksey Pesternikov Date: 2014-04-25T14:12:58Z minor formatting commit 0a693115c2c20a2a8375f3859ff7bed261e6c8da Author: Aleksey Pesternikov Date: 2014-04-25T14:13:20Z Merge branch 'test_cpp_on_mac' into go_integration commit e06b5d24e8b6d429723a3c77a58c6ce903e1366a Author: Aleksey Pesternikov Date: 2014-04-25T15:19:20Z Merge branch 'go_test_service_name' into go_integration commit 42d577c9812a070060c773fcd0598e58e6d6ba61 Author: Aleksey Pesternikov Date: 2014-04-25T15:39:57Z imported THRIFT-2491 lowercase package names commit 491ccf8b018c046c5ced72b1e19d9ac4ec48a6f5 Author: Aleksey Pesternikov Date: 2014-04-25T15:51:53Z --noinsane flag for c++ TestClient. works with go server now for other default params commit 78db3c9a3a5742818a5de6e57f2fe9aed84919e5 Author: Aleksey Pesternikov Date: 2014-04-25T16:03:23Z license --- configure.ac | 1 + lib/go/thrift/debug_protocol.go | 269 ++++++++++++++++++ lib/go/thrift/simple_server.go | 22 +- test/Makefile.am | 4 + test/StressTest.thrift | 1 + test/cpp/src/TestClient.cpp | 91 +++--- test/go/Makefile.am | 58 ++++ test/go/src/bin/stress/main.go | 254 +++++++++++++++++ test/go/src/bin/testclient/main.go | 243 ++++++++++++++++ test/go/src/bin/testserver/main.go | 42 +++ test/go/src/common/client.go | 96 +++++++ test/go/src/common/clientserver_test.go | 292 ++++++++++++++++++++ test/go/src/common/mock_handler.go | 266 ++++++++++++++++++ test/go/src/common/printing_handler.go | 351 ++++++++++++++++++++++++ test/go/src/common/server.go | 113 ++++++++ test/go/src/common/simple_handler.go | 147 ++++++++++ 16 files changed, 2200 insertions(+), 50 deletions(-) create mode 100644 lib/go/thrift/debug_protocol.go create mode 100644 test/go/Makefile.am create mode 100644 test/go/src/bin/stress/main.go create mode 100644 test/go/src/bin/testclient/main.go create mode 100644 test/go/src/bin/testserver/main.go create mode 100644 test/go/src/common/client.go create mode 100644 test/go/src/common/clientserver_test.go create mode 100644 test/go/src/common/mock_handler.go create mode 100644 test/go/src/common/printing_handler.go create mode 100644 test/go/src/common/server.go create mode 100644 test/go/src/common/simple_handler.go diff --git a/configure.ac b/configure.ac index 5712584f..995dba53 100755 --- a/configure.ac +++ b/configure.ac @@ -649,6 +649,7 @@ AC_CONFIG_FILES([ lib/lua/Makefile test/Makefile test/cpp/Makefile + test/go/Makefile test/hs/Makefile test/php/Makefile test/perl/Makefile diff --git a/lib/go/thrift/debug_protocol.go b/lib/go/thrift/debug_protocol.go new file mode 100644 index 00000000..ee341b2f --- /dev/null +++ b/lib/go/thrift/debug_protocol.go @@ -0,0 +1,269 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package thrift + +import ( + "log" +) + +type TDebugProtocol struct { + Delegate TProtocol + LogPrefix string +} + +type TDebugProtocolFactory struct { + Underlying TProtocolFactory + LogPrefix string +} + +func NewTDebugProtocolFactory(underlying TProtocolFactory, logPrefix string) *TDebugProtocolFactory { + return &TDebugProtocolFactory{ + Underlying: underlying, + LogPrefix: logPrefix, + } +} + +func (t *TDebugProtocolFactory) GetProtocol(trans TTransport) TProtocol { + return &TDebugProtocol{ + Delegate: t.Underlying.GetProtocol(trans), + LogPrefix: t.LogPrefix, + } +} + +func (tdp *TDebugProtocol) WriteMessageBegin(name string, typeId TMessageType, seqid int32) error { + err := tdp.Delegate.WriteMessageBegin(name, typeId, seqid) + log.Printf("%sWriteMessageBegin(name=%#v, typeId=%#v, seqid=%#v) => %#v", tdp.LogPrefix, name, typeId, seqid, err) + return err +} +func (tdp *TDebugProtocol) WriteMessageEnd() error { + err := tdp.Delegate.WriteMessageEnd() + log.Printf("%sWriteMessageEnd() => %#v", tdp.LogPrefix, err) + return err +} +func (tdp *TDebugProtocol) WriteStructBegin(name string) error { + err := tdp.Delegate.WriteStructBegin(name) + log.Printf("%sWriteStructBegin(name=%#v) => %#v", tdp.LogPrefix, name, err) + return err +} +func (tdp *TDebugProtocol) WriteStructEnd() error { + err := tdp.Delegate.WriteStructEnd() + log.Printf("%sWriteStructEnd() => %#v", tdp.LogPrefix, err) + return err +} +func (tdp *TDebugProtocol) WriteFieldBegin(name string, typeId TType, id int16) error { + err := tdp.Delegate.WriteFieldBegin(name, typeId, id) + log.Printf("%sWriteFieldBegin(name=%#v, typeId=%#v, id%#v) => %#v", tdp.LogPrefix, name, typeId, id, err) + return err +} +func (tdp *TDebugProtocol) WriteFieldEnd() error { + err := tdp.Delegate.WriteFieldEnd() + log.Printf("%sWriteFieldEnd() => %#v", tdp.LogPrefix, err) + return err +} +func (tdp *TDebugProtocol) WriteFieldStop() error { + err := tdp.Delegate.WriteFieldStop() + log.Printf("%sWriteFieldStop() => %#v", tdp.LogPrefix, err) + return err +} +func (tdp *TDebugProtocol) WriteMapBegin(keyType TType, valueType TType, size int) error { + err := tdp.Delegate.WriteMapBegin(keyType, valueType, size) + log.Printf("%sWriteMapBegin(keyType=%#v, valueType=%#v, size=%#v) => %#v", tdp.LogPrefix, keyType, valueType, size, err) + return err +} +func (tdp *TDebugProtocol) WriteMapEnd() error { + err := tdp.Delegate.WriteMapEnd() + log.Printf("%sWriteMapEnd() => %#v", tdp.LogPrefix, err) + return err +} +func (tdp *TDebugProtocol) WriteListBegin(elemType TType, size int) error { + err := tdp.Delegate.WriteListBegin(elemType, size) + log.Printf("%sWriteListBegin(elemType=%#v, size=%#v) => %#v", tdp.LogPrefix, elemType, size, err) + return err +} +func (tdp *TDebugProtocol) WriteListEnd() error { + err := tdp.Delegate.WriteListEnd() + log.Printf("%sWriteListEnd() => %#v", tdp.LogPrefix, err) + return err +} +func (tdp *TDebugProtocol) WriteSetBegin(elemType TType, size int) error { + err := tdp.Delegate.WriteSetBegin(elemType, size) + log.Printf("%sWriteSetBegin(elemType=%#v, size=%#v) => %#v", tdp.LogPrefix, elemType, size, err) + return err +} +func (tdp *TDebugProtocol) WriteSetEnd() error { + err := tdp.Delegate.WriteSetEnd() + log.Printf("%sWriteSetEnd() => %#v", tdp.LogPrefix, err) + return err +} +func (tdp *TDebugProtocol) WriteBool(value bool) error { + err := tdp.Delegate.WriteBool(value) + log.Printf("%sWriteBool(value=%#v) => %#v", tdp.LogPrefix, value, err) + return err +} +func (tdp *TDebugProtocol) WriteByte(value byte) error { + err := tdp.Delegate.WriteByte(value) + log.Printf("%sWriteByte(value=%#v) => %#v", tdp.LogPrefix, value, err) + return err +} +func (tdp *TDebugProtocol) WriteI16(value int16) error { + err := tdp.Delegate.WriteI16(value) + log.Printf("%sWriteI16(value=%#v) => %#v", tdp.LogPrefix, value, err) + return err +} +func (tdp *TDebugProtocol) WriteI32(value int32) error { + err := tdp.Delegate.WriteI32(value) + log.Printf("%sWriteI32(value=%#v) => %#v", tdp.LogPrefix, value, err) + return err +} +func (tdp *TDebugProtocol) WriteI64(value int64) error { + err := tdp.Delegate.WriteI64(value) + log.Printf("%sWriteI64(value=%#v) => %#v", tdp.LogPrefix, value, err) + return err +} +func (tdp *TDebugProtocol) WriteDouble(value float64) error { + err := tdp.Delegate.WriteDouble(value) + log.Printf("%sWriteDouble(value=%#v) => %#v", tdp.LogPrefix, value, err) + return err +} +func (tdp *TDebugProtocol) WriteString(value string) error { + err := tdp.Delegate.WriteString(value) + log.Printf("%sWriteString(value=%#v) => %#v", tdp.LogPrefix, value, err) + return err +} +func (tdp *TDebugProtocol) WriteBinary(value []byte) error { + err := tdp.Delegate.WriteBinary(value) + log.Printf("%sWriteBinary(value=%#v) => %#v", tdp.LogPrefix, value, err) + return err +} + +func (tdp *TDebugProtocol) ReadMessageBegin() (name string, typeId TMessageType, seqid int32, err error) { + name, typeId, seqid, err = tdp.Delegate.ReadMessageBegin() + log.Printf("%sReadMessageBegin() (name=%#v, typeId=%#v, seqid=%#v, err=%#v)", tdp.LogPrefix, name, typeId, seqid, err) + return +} +func (tdp *TDebugProtocol) ReadMessageEnd() (err error) { + err = tdp.Delegate.ReadMessageEnd() + log.Printf("%sReadMessageEnd() err=%#v", tdp.LogPrefix, err) + return +} +func (tdp *TDebugProtocol) ReadStructBegin() (name string, err error) { + name, err = tdp.Delegate.ReadStructBegin() + log.Printf("%sReadStructBegin() (name%#v, err=%#v)", tdp.LogPrefix, name, err) + return +} +func (tdp *TDebugProtocol) ReadStructEnd() (err error) { + err = tdp.Delegate.ReadStructEnd() + log.Printf("%sReadStructEnd() err=%#v", tdp.LogPrefix, err) + return +} +func (tdp *TDebugProtocol) ReadFieldBegin() (name string, typeId TType, id int16, err error) { + name, typeId, id, err = tdp.Delegate.ReadFieldBegin() + log.Printf("%sReadFieldBegin() (name=%#v, typeId=%#v, id=%#v, err=%#v)", tdp.LogPrefix, name, typeId, id, err) + return +} +func (tdp *TDebugProtocol) ReadFieldEnd() (err error) { + err = tdp.Delegate.ReadFieldEnd() + log.Printf("%sReadFieldEnd() err=%#v", tdp.LogPrefix, err) + return +} +func (tdp *TDebugProtocol) ReadMapBegin() (keyType TType, valueType TType, size int, err error) { + keyType, valueType, size, err = tdp.Delegate.ReadMapBegin() + log.Printf("%sReadMapBegin() (keyType=%#v, valueType=%#v, size=%#v, err=%#v)", tdp.LogPrefix, keyType, valueType, size, err) + return +} +func (tdp *TDebugProtocol) ReadMapEnd() (err error) { + err = tdp.Delegate.ReadMapEnd() + log.Printf("%sReadMapEnd() err=%#v", tdp.LogPrefix, err) + return +} +func (tdp *TDebugProtocol) ReadListBegin() (elemType TType, size int, err error) { + elemType, size, err = tdp.Delegate.ReadListBegin() + log.Printf("%sReadListBegin() (elemType=%#v, size=%#v, err=%#v)", tdp.LogPrefix, elemType, size, err) + return +} +func (tdp *TDebugProtocol) ReadListEnd() (err error) { + err = tdp.Delegate.ReadListEnd() + log.Printf("%sReadListEnd() err=%#v", tdp.LogPrefix, err) + return +} +func (tdp *TDebugProtocol) ReadSetBegin() (elemType TType, size int, err error) { + elemType, size, err = tdp.Delegate.ReadSetBegin() + log.Printf("%sReadSetBegin() (elemType=%#v, size=%#v, err=%#v)", tdp.LogPrefix, elemType, size, err) + return +} +func (tdp *TDebugProtocol) ReadSetEnd() (err error) { + err = tdp.Delegate.ReadSetEnd() + log.Printf("%sReadSetEnd() err=%#v", tdp.LogPrefix, err) + return +} +func (tdp *TDebugProtocol) ReadBool() (value bool, err error) { + value, err = tdp.Delegate.ReadBool() + log.Printf("%sReadBool() (value=%#v, err=%#v)", tdp.LogPrefix, value, err) + return +} +func (tdp *TDebugProtocol) ReadByte() (value byte, err error) { + value, err = tdp.Delegate.ReadByte() + log.Printf("%sReadByte() (value=%#v, err=%#v)", tdp.LogPrefix, value, err) + return +} +func (tdp *TDebugProtocol) ReadI16() (value int16, err error) { + value, err = tdp.Delegate.ReadI16() + log.Printf("%sReadI16() (value=%#v, err=%#v)", tdp.LogPrefix, value, err) + return +} +func (tdp *TDebugProtocol) ReadI32() (value int32, err error) { + value, err = tdp.Delegate.ReadI32() + log.Printf("%sReadI32() (value=%#v, err=%#v)", tdp.LogPrefix, value, err) + return +} +func (tdp *TDebugProtocol) ReadI64() (value int64, err error) { + value, err = tdp.Delegate.ReadI64() + log.Printf("%sReadI64() (value=%#v, err=%#v)", tdp.LogPrefix, value, err) + return +} +func (tdp *TDebugProtocol) ReadDouble() (value float64, err error) { + value, err = tdp.Delegate.ReadDouble() + log.Printf("%sReadDouble() (value=%#v, err=%#v)", tdp.LogPrefix, value, err) + return +} +func (tdp *TDebugProtocol) ReadString() (value string, err error) { + value, err = tdp.Delegate.ReadString() + log.Printf("%sReadString() (value=%#v, err=%#v)", tdp.LogPrefix, value, err) + return +} +func (tdp *TDebugProtocol) ReadBinary() (value []byte, err error) { + value, err = tdp.Delegate.ReadBinary() + log.Printf("%sReadBinary() (value=%#v, err=%#v)", tdp.LogPrefix, value, err) + return +} +func (tdp *TDebugProtocol) Skip(fieldType TType) (err error) { + err = tdp.Delegate.Skip(fieldType) + log.Printf("%sSkip(fieldType=%#v) (err=%#v)", tdp.LogPrefix, fieldType, err) + return +} +func (tdp *TDebugProtocol) Flush() (err error) { + err = tdp.Delegate.Flush() + log.Printf("%sFlush() (err=%#v)", tdp.LogPrefix, err) + return +} + +func (tdp *TDebugProtocol) Transport() TTransport { + return tdp.Delegate.Transport() +} diff --git a/lib/go/thrift/simple_server.go b/lib/go/thrift/simple_server.go index ffbfb766..eb8eb953 100644 --- a/lib/go/thrift/simple_server.go +++ b/lib/go/thrift/simple_server.go @@ -113,17 +113,15 @@ func (p *TSimpleServer) OutputProtocolFactory() TProtocolFactory { return p.outputProtocolFactory } -func (p *TSimpleServer) Serve() error { - err := p.serverTransport.Listen() - if err != nil { - return err - } +func (p *TSimpleServer) Listen() error { + return p.serverTransport.Listen() +} -loop: +func (p *TSimpleServer) AcceptLoop() error { for { select { case <-p.quit: - break loop + return nil default: } @@ -139,6 +137,14 @@ loop: }() } } +} + +func (p *TSimpleServer) Serve() error { + err := p.Listen() + if err != nil { + return err + } + p.AcceptLoop() return nil } @@ -162,7 +168,7 @@ func (p *TSimpleServer) processRequest(client TTransport) error { } for { ok, err := processor.Process(inputProtocol, outputProtocol) - if err, ok := err.(TTransportException); ok && err.TypeId() == END_OF_FILE{ + if err, ok := err.(TTransportException); ok && err.TypeId() == END_OF_FILE { return nil } else if err != nil { return err diff --git a/test/Makefile.am b/test/Makefile.am index 2d44229d..75630cc1 100755 --- a/test/Makefile.am +++ b/test/Makefile.am @@ -45,6 +45,10 @@ if WITH_HASKELL SUBDIRS += hs endif +if WITH_GO +SUBDIRS += go +endif + # # generate html for ThriftTest.thrift # diff --git a/test/StressTest.thrift b/test/StressTest.thrift index e09a1fc8..ccf22aff 100644 --- a/test/StressTest.thrift +++ b/test/StressTest.thrift @@ -19,6 +19,7 @@ namespace cpp test.stress namespace d thrift.test.stress +namespace go stress service Service { diff --git a/test/cpp/src/TestClient.cpp b/test/cpp/src/TestClient.cpp index 4ebbb812..7a783d22 100755 --- a/test/cpp/src/TestClient.cpp +++ b/test/cpp/src/TestClient.cpp @@ -102,6 +102,7 @@ int main(int argc, char** argv) { string transport_type = "buffered"; string protocol_type = "binary"; string domain_socket = ""; + bool noinsane = false; boost::program_options::options_description desc("Allowed options"); desc.add_options() @@ -113,6 +114,7 @@ int main(int argc, char** argv) { ("protocol", boost::program_options::value(&protocol_type)->default_value(protocol_type), "Protocol: binary, compact, json") ("ssl", "Encrypted Transport using SSL") ("testloops,n", boost::program_options::value(&numTests)->default_value(numTests), "Number of Tests") + ("noinsane", "Do not run insanity test") ; boost::program_options::variables_map vm; @@ -153,6 +155,10 @@ int main(int argc, char** argv) { if (vm.count("ssl")) { ssl = true; } + + if (vm.count("noinsane")) { + noinsane = true; + } boost::shared_ptr transport; boost::shared_ptr protocol; @@ -518,52 +524,53 @@ int main(int argc, char** argv) { /** * INSANITY TEST */ - Insanity insane; - insane.userMap.insert(make_pair(Numberz::FIVE, 5000)); - Xtruct truck; - truck.string_thing = "Truck"; - truck.byte_thing = 8; - truck.i32_thing = 8; - truck.i64_thing = 8; - insane.xtructs.push_back(truck); - printf("testInsanity()"); - map > whoa; - testClient.testInsanity(whoa, insane); - printf(" = {"); - map >::const_iterator i_iter; - for (i_iter = whoa.begin(); i_iter != whoa.end(); ++i_iter) { - printf("%" PRId64 " => {", i_iter->first); - map::const_iterator i2_iter; - for (i2_iter = i_iter->second.begin(); - i2_iter != i_iter->second.end(); - ++i2_iter) { - printf("%d => {", i2_iter->first); - map userMap = i2_iter->second.userMap; - map::const_iterator um; - printf("{"); - for (um = userMap.begin(); um != userMap.end(); ++um) { - printf("%d => %" PRId64 ", ", um->first, um->second); + if (!noinsane) { + Insanity insane; + insane.userMap.insert(make_pair(Numberz::FIVE, 5000)); + Xtruct truck; + truck.string_thing = "Truck"; + truck.byte_thing = 8; + truck.i32_thing = 8; + truck.i64_thing = 8; + insane.xtructs.push_back(truck); + printf("testInsanity()"); + map > whoa; + testClient.testInsanity(whoa, insane); + printf(" = {"); + map >::const_iterator i_iter; + for (i_iter = whoa.begin(); i_iter != whoa.end(); ++i_iter) { + printf("%" PRId64 " => {", i_iter->first); + map::const_iterator i2_iter; + for (i2_iter = i_iter->second.begin(); + i2_iter != i_iter->second.end(); + ++i2_iter) { + printf("%d => {", i2_iter->first); + map userMap = i2_iter->second.userMap; + map::const_iterator um; + printf("{"); + for (um = userMap.begin(); um != userMap.end(); ++um) { + printf("%d => %" PRId64 ", ", um->first, um->second); + } + printf("}, "); + + vector xtructs = i2_iter->second.xtructs; + vector::const_iterator x; + printf("{"); + for (x = xtructs.begin(); x != xtructs.end(); ++x) { + printf("{\"%s\", %d, %d, %" PRId64 "}, ", + x->string_thing.c_str(), + (int)x->byte_thing, + x->i32_thing, + x->i64_thing); + } + printf("}"); + + printf("}, "); } printf("}, "); - - vector xtructs = i2_iter->second.xtructs; - vector::const_iterator x; - printf("{"); - for (x = xtructs.begin(); x != xtructs.end(); ++x) { - printf("{\"%s\", %d, %d, %" PRId64 "}, ", - x->string_thing.c_str(), - (int)x->byte_thing, - x->i32_thing, - x->i64_thing); - } - printf("}"); - - printf("}, "); } - printf("}, "); + printf("}\n"); } - printf("}\n"); - /* test exception */ try { diff --git a/test/go/Makefile.am b/test/go/Makefile.am new file mode 100644 index 00000000..48757574 --- /dev/null +++ b/test/go/Makefile.am @@ -0,0 +1,58 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +# + +THRIFT = $(top_srcdir)/compiler/cpp/thrift +THRIFTCMD = $(THRIFT) -out src/gen --gen go:thrift_import=thrift +THRIFTTEST = $(top_srcdir)/test/ThriftTest.thrift + +all: bin/testclient bin/testserver bin/stress + +ThriftTest.thrift: $(THRIFTTEST) + grep -v list.*map.*list.*map $(THRIFTTEST) > ThriftTest.thrift + +# Thrift for GO has problems with complex map keys: THRIFT-2063 +gopath: $(THRIFT) ThriftTest.thrift + $(THRIFTCMD) ThriftTest.thrift + $(THRIFTCMD) ../StressTest.thrift + ln -nfs ../../../lib/go/thrift src/thrift + GOPATH=`pwd` $(GO) get code.google.com/p/gomock/gomock + touch gopath + +bin/testclient: gopath + GOPATH=`pwd` $(GO) install bin/testclient + +bin/testserver: gopath + GOPATH=`pwd` $(GO) install bin/testserver + +bin/stress: gopath + GOPATH=`pwd` $(GO) install bin/stress + +clean-local: + $(RM) -r src/gen/* src/code.google.com src/thrift bin pkg gopath ThriftTest.thrift + +check: gopath + GOPATH=`pwd` $(GO) test -v common/... + +genmock: gopath + GOPATH=`pwd` $(GO) install code.google.com/p/gomock/mockgen + GOPATH=`pwd` bin/mockgen -destination=src/common/mock_handler.go -package=common gen/thrifttest ThriftTest + +EXTRA_DIST = \ + IncludesTest.thrift \ + NamespacedTest.thrift diff --git a/test/go/src/bin/stress/main.go b/test/go/src/bin/stress/main.go new file mode 100644 index 00000000..4fc88e07 --- /dev/null +++ b/test/go/src/bin/stress/main.go @@ -0,0 +1,254 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package main + +import ( + "flag" + "fmt" + "gen/stress" + "log" + _ "net/http/pprof" + "os" + "runtime" + "runtime/pprof" + "sync" + "sync/atomic" + "thrift" + "time" +) + +var cpuprofile = flag.String("cpuprofile", "", "write cpu profile to this file") +var memprofile = flag.String("memprofile", "", "write memory profile to this file") + +var ( + host = flag.String("host", "localhost", "Host to connect") + port = flag.Int64("port", 9091, "Port number to connect") + loop = flag.Int("loops", 50000, "The number of remote thrift calls each client makes") + runserver = flag.Int("server", 1, "Run the Thrift server in this process") + clients = flag.Int("clients", 20, "Number of client threads to create - 0 implies no clients, i.e. server only") + callName = flag.String("call", "echoVoid", "Service method to call, one of echoVoid, echoByte, echoI32, echoI64, echoString, echiList, echoSet, echoMap") + compact = flag.Bool("compact", false, "Use compact protocol instead of binary.") + framed = flag.Bool("framed", false, "Use framed transport instead of buffered.") +) +var hostPort string + +type callT int64 + +const ( + echoVoid callT = iota + echoByte + echoI32 + echoI64 + echoString + echiList + echoSet + echoMap +) + +var callTMap = map[string]callT{ + "echoVoid": echoVoid, + "echoByte": echoByte, + "echoI32": echoI32, + "echoI64": echoI64, + "echoString": echoString, + "echiList": echiList, + "echoSet": echoSet, + "echoMap": echoMap, +} +var callType callT + +var ready, done sync.WaitGroup + +var clicounter int64 = 0 +var counter int64 = 0 + +func main() { + flag.Parse() + if *memprofile != "" { + runtime.MemProfileRate = 100 + } + var ok bool + if callType, ok = callTMap[*callName]; !ok { + log.Fatal("Unknown service call", *callName) + } + if *cpuprofile != "" { + f, err := os.Create(*cpuprofile) + if err != nil { + log.Fatal(err) + } + pprof.StartCPUProfile(f) + defer pprof.StopCPUProfile() + } + hostPort = fmt.Sprintf("%s:%d", *host, *port) + var protocolFactory thrift.TProtocolFactory + var transportFactory thrift.TTransportFactory + + if *compact { + protocolFactory = thrift.NewTCompactProtocolFactory() + } else { + protocolFactory = thrift.NewTBinaryProtocolFactoryDefault() + } + + if *framed { + transportFactory = thrift.NewTTransportFactory() + transportFactory = thrift.NewTFramedTransportFactory(transportFactory) + } else { + transportFactory = thrift.NewTBufferedTransportFactory(8192) + } + + if *runserver > 0 { + serverTransport, err := thrift.NewTServerSocket(hostPort) + if err != nil { + log.Fatalf("Unable to create server socket: %s", err) + } + + processor := stress.NewServiceProcessor(&handler{}) + server := thrift.NewTSimpleServer4(processor, serverTransport, transportFactory, protocolFactory) + if *clients == 0 { + server.Serve() + } else { + go server.Serve() + } + } + //start clients + if *clients != 0 { + ready.Add(*clients + 1) + done.Add(*clients) + for i := 0; i < *clients; i++ { + go client(protocolFactory) + } + ready.Done() + ready.Wait() + //run! + startTime := time.Now() + //wait for completion + done.Wait() + endTime := time.Now() + duration := endTime.Sub(startTime) + log.Printf("%d calls in %v (%f calls per second)\n", clicounter, duration, float64(clicounter)/duration.Seconds()) + } + if *memprofile != "" { + f, err := os.Create(*memprofile) + if err != nil { + log.Fatal(err) + } + pprof.WriteHeapProfile(f) + f.Close() + return + } +} + +func client(protocolFactory thrift.TProtocolFactory) { + trans, err := thrift.NewTSocket(hostPort) + if err != nil { + log.Fatalf("Unable to create server socket: %s", err) + } + btrans := thrift.NewTBufferedTransport(trans, 2048) + client := stress.NewServiceClientFactory(btrans, protocolFactory) + err = trans.Open() + if err != nil { + log.Fatalf("Unable to open connection: %s", err) + } + ready.Done() + ready.Wait() + switch callType { + case echoVoid: + for i := 0; i < *loop; i++ { + client.EchoVoid() + atomic.AddInt64(&clicounter, 1) + } + case echoByte: + for i := 0; i < *loop; i++ { + client.EchoByte(42) + atomic.AddInt64(&clicounter, 1) + } + case echoI32: + for i := 0; i < *loop; i++ { + client.EchoI32(4242) + atomic.AddInt64(&clicounter, 1) + } + case echoI64: + for i := 0; i < *loop; i++ { + client.EchoI64(424242) + atomic.AddInt64(&clicounter, 1) + } + case echoString: + for i := 0; i < *loop; i++ { + client.EchoString("TestString") + atomic.AddInt64(&clicounter, 1) + } + case echiList: + l := []int8{-10, -9, -8, -7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8} + for i := 0; i < *loop; i++ { + client.EchoList(l) + atomic.AddInt64(&clicounter, 1) + } + case echoSet: + s := map[int8]bool{-10: true, -9: true, -8: true, -7: true, -6: true, -5: true, -4: true, -3: true, -2: true, -1: true, 0: true, 1: true, 2: true, 3: true, 4: true, 5: true, 6: true, 7: true, 8: true} + for i := 0; i < *loop; i++ { + client.EchoSet(s) + atomic.AddInt64(&clicounter, 1) + } + case echoMap: + m := map[int8]int8{ + -10: 10, -9: 9, -8: 8, -7: 7, -6: 6, -5: 5, -4: 4, -3: 3, -2: 2, -1: 1, 0: 0, 1: 1, 2: 2, 3: 3, 4: 4, 5: 5, 6: 6, 7: 7, 8: 8} + for i := 0; i < *loop; i++ { + client.EchoMap(m) + atomic.AddInt64(&clicounter, 1) + } + } + + done.Done() +} + +type handler struct{} + +func (h *handler) EchoVoid() (err error) { + atomic.AddInt64(&counter, 1) + return nil +} +func (h *handler) EchoByte(arg int8) (r int8, err error) { + atomic.AddInt64(&counter, 1) + return arg, nil +} +func (h *handler) EchoI32(arg int32) (r int32, err error) { + atomic.AddInt64(&counter, 1) + return arg, nil +} +func (h *handler) EchoI64(arg int64) (r int64, err error) { + atomic.AddInt64(&counter, 1) + return arg, nil +} +func (h *handler) EchoString(arg string) (r string, err error) { + atomic.AddInt64(&counter, 1) + return arg, nil +} +func (h *handler) EchoList(arg []int8) (r []int8, err error) { + atomic.AddInt64(&counter, 1) + return arg, nil +} +func (h *handler) EchoSet(arg map[int8]bool) (r map[int8]bool, err error) { + atomic.AddInt64(&counter, 1) + return arg, nil +} +func (h *handler) EchoMap(arg map[int8]int8) (r map[int8]int8, err error) { + atomic.AddInt64(&counter, 1) + return arg, nil +} diff --git a/test/go/src/bin/testclient/main.go b/test/go/src/bin/testclient/main.go new file mode 100644 index 00000000..2acf6cfc --- /dev/null +++ b/test/go/src/bin/testclient/main.go @@ -0,0 +1,243 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package main + +import ( + "common" + "flag" + "gen/thrifttest" + t "log" + "reflect" + "thrift" +) + +var host = flag.String("host", "localhost", "Host to connect") +var port = flag.Int64("port", 9090, "Port number to connect") +var domain_socket = flag.String("domain-socket", "", "Domain Socket (e.g. /tmp/thrifttest.thrift), instead of host and port") +var transport = flag.String("transport", "buffered", "Transport: buffered, framed, http") +var protocol = flag.String("protocol", "binary", "Protocol: binary, compact, json") +var ssl = flag.Bool("ssl", false, "Encrypted Transport using SSL") +var testloops = flag.Int("testloops", 1, "Number of Tests") + +func main() { + flag.Parse() + client, err := common.StartClient(*host, *port, *domain_socket, *transport, *protocol, *ssl) + if err != nil { + t.Fatalf("Unable to start client: ", err) + } + for i := 0; i < *testloops; i++ { + callEverything(client) + } +} + +var rmapmap = map[int32]map[int32]int32{ + -4: map[int32]int32{-4: -4, -3: -3, -2: -2, -1: -1}, + 4: map[int32]int32{4: 4, 3: 3, 2: 2, 1: 1}, +} + +var xxs = &thrifttest.Xtruct{ + StringThing: "Hello2", + ByteThing: 42, + I32Thing: 4242, + I64Thing: 424242, +} + +var xcept = &thrifttest.Xception{ErrorCode: 1001, Message: "Xception"} + +func callEverything(client *thrifttest.ThriftTestClient) { + var err error + if err = client.TestVoid(); err != nil { + t.Fatalf("Unexpected error in TestVoid() call: ", err) + } + + thing, err := client.TestString("thing") + if err != nil { + t.Fatalf("Unexpected error in TestString() call: ", err) + } + if thing != "thing" { + t.Fatalf("Unexpected TestString() result, expected 'thing' got '%s' ", thing) + } + + b, err := client.TestByte(42) + if err != nil { + t.Fatalf("Unexpected error in TestByte() call: ", err) + } + if b != 42 { + t.Fatalf("Unexpected TestByte() result expected 42, got %d ", b) + } + + i32, err := client.TestI32(4242) + if err != nil { + t.Fatalf("Unexpected error in TestI32() call: ", err) + } + if i32 != 4242 { + t.Fatalf("Unexpected TestI32() result expected 4242, got %d ", i32) + } + + i64, err := client.TestI64(424242) + if err != nil { + t.Fatalf("Unexpected error in TestI64() call: ", err) + } + if i64 != 424242 { + t.Fatalf("Unexpected TestI64() result expected 424242, got %d ", i64) + } + + d, err := client.TestDouble(42.42) + if err != nil { + t.Fatalf("Unexpected error in TestDouble() call: ", err) + } + if d != 42.42 { + t.Fatalf("Unexpected TestDouble() result expected 42.42, got %f ", d) + } + + xs := thrifttest.NewXtruct() + xs.StringThing = "thing" + xs.ByteThing = 42 + xs.I32Thing = 4242 + xs.I64Thing = 424242 + xsret, err := client.TestStruct(xs) + if err != nil { + t.Fatalf("Unexpected error in TestStruct() call: ", err) + } + if *xs != *xsret { + t.Fatalf("Unexpected TestStruct() result expected %#v, got %#v ", xs, xsret) + } + + x2 := thrifttest.NewXtruct2() + x2.StructThing = xs + x2ret, err := client.TestNest(x2) + if err != nil { + t.Fatalf("Unexpected error in TestNest() call: ", err) + } + if !reflect.DeepEqual(x2, x2ret) { + t.Fatalf("Unexpected TestNest() result expected %#v, got %#v ", x2, x2ret) + } + + m := map[int32]int32{1: 2, 3: 4, 5: 42} + mret, err := client.TestMap(m) + if err != nil { + t.Fatalf("Unexpected error in TestMap() call: ", err) + } + if !reflect.DeepEqual(m, mret) { + t.Fatalf("Unexpected TestMap() result expected %#v, got %#v ", m, mret) + } + + sm := map[string]string{"a": "2", "b": "blah", "some": "thing"} + smret, err := client.TestStringMap(sm) + if err != nil { + t.Fatalf("Unexpected error in TestStringMap() call: ", err) + } + if !reflect.DeepEqual(sm, smret) { + t.Fatalf("Unexpected TestStringMap() result expected %#v, got %#v ", sm, smret) + } + + s := map[int32]bool{1: true, 2: true, 42: true} + sret, err := client.TestSet(s) + if err != nil { + t.Fatalf("Unexpected error in TestSet() call: ", err) + } + if !reflect.DeepEqual(s, sret) { + t.Fatalf("Unexpected TestSet() result expected %#v, got %#v ", s, sret) + } + + l := []int32{1, 2, 42} + lret, err := client.TestList(l) + if err != nil { + t.Fatalf("Unexpected error in TestList() call: ", err) + } + if !reflect.DeepEqual(l, lret) { + t.Fatalf("Unexpected TestSet() result expected %#v, got %#v ", l, lret) + } + + eret, err := client.TestEnum(thrifttest.Numberz_TWO) + if err != nil { + t.Fatalf("Unexpected error in TestEnum() call: ", err) + } + if eret != thrifttest.Numberz_TWO { + t.Fatalf("Unexpected TestEnum() result expected %#v, got %#v ", thrifttest.Numberz_TWO, eret) + } + + tret, err := client.TestTypedef(thrifttest.UserId(42)) + if err != nil { + t.Fatalf("Unexpected error in TestTypedef() call: ", err) + } + if tret != thrifttest.UserId(42) { + t.Fatalf("Unexpected TestTypedef() result expected %#v, got %#v ", thrifttest.UserId(42), tret) + } + + mapmap, err := client.TestMapMap(42) + if err != nil { + t.Fatalf("Unexpected error in TestMapmap() call: ", err) + } + if !reflect.DeepEqual(mapmap, rmapmap) { + t.Fatalf("Unexpected TestMapmap() result expected %#v, got %#v ", rmapmap, mapmap) + } + + xxsret, err := client.TestMulti(42, 4242, 424242, map[int16]string{1: "blah", 2: "thing"}, thrifttest.Numberz_EIGHT, thrifttest.UserId(24)) + if err != nil { + t.Fatalf("Unexpected error in TestMulti() call: ", err) + } + if !reflect.DeepEqual(xxs, xxsret) { + t.Fatalf("Unexpected TestMulti() result expected %#v, got %#v ", xxs, xxsret) + } + + err = client.TestException("Xception") + if err == nil { + t.Fatalf("Expecting exception in TestException() call") + } + if !reflect.DeepEqual(err, xcept) { + t.Fatalf("Unexpected TestException() result expected %#v, got %#v ", xcept, err) + } + + // TODO: connection is being closed on this + err = client.TestException("TException") + tex, ok := err.(thrift.TApplicationException) + if err == nil || !ok || tex.TypeId() != thrift.INTERNAL_ERROR { + t.Fatalf("Unexpected TestException() result expected ApplicationError, got %#v ", err) + } + + ign, err := client.TestMultiException("Xception", "ignoreme") + if ign != nil || err == nil { + t.Fatalf("Expecting exception in TestMultiException() call") + } + if !reflect.DeepEqual(err, &thrifttest.Xception{ErrorCode: 1001, Message: "This is an Xception"}) { + t.Fatalf("Unexpected TestMultiException() %#v ", err) + } + + ign, err = client.TestMultiException("Xception2", "ignoreme") + if ign != nil || err == nil { + t.Fatalf("Expecting exception in TestMultiException() call") + } + expecting := &thrifttest.Xception2{ErrorCode: 2002, StructThing: &thrifttest.Xtruct{StringThing: "This is an Xception2"}} + + if !reflect.DeepEqual(err, expecting) { + t.Fatalf("Unexpected TestMultiException() %#v ", err) + } + + err = client.TestOneway(2) + if err != nil { + t.Fatalf("Unexpected error in TestOneway() call: ", err) + } + + //Make sure the connection still alive + if err = client.TestVoid(); err != nil { + t.Fatalf("Unexpected error in TestVoid() call: ", err) + } +} diff --git a/test/go/src/bin/testserver/main.go b/test/go/src/bin/testserver/main.go new file mode 100644 index 00000000..cd32f921 --- /dev/null +++ b/test/go/src/bin/testserver/main.go @@ -0,0 +1,42 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package main + +import ( + "common" + "flag" + "log" +) + +var host = flag.String("host", "localhost", "Host to connect") +var port = flag.Int64("port", 9090, "Port number to connect") +var domain_socket = flag.String("domain-socket", "", "Domain Socket (e.g. /tmp/ThriftTest.thrift), instead of host and port") +var transport = flag.String("transport", "buffered", "Transport: buffered, framed, http") +var protocol = flag.String("protocol", "binary", "Protocol: binary, compact, json") +var ssl = flag.Bool("ssl", false, "Encrypted Transport using SSL") + +func main() { + flag.Parse() + server, err := common.StartServer(*host, *port, *domain_socket, *transport, *protocol, *ssl, common.PrintingHandler) + if err != nil { + log.Fatalf("Unable to start server: ", err) + } + server.Serve() +} diff --git a/test/go/src/common/client.go b/test/go/src/common/client.go new file mode 100644 index 00000000..267273ef --- /dev/null +++ b/test/go/src/common/client.go @@ -0,0 +1,96 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package common + +import ( + "crypto/tls" + "flag" + "fmt" + "gen/thrifttest" + "thrift" +) + +var debugClientProtocol bool + +func init() { + flag.BoolVar(&debugClientProtocol, "debug_client_protocol", false, "turn client protocol trace on") +} + +func StartClient( + host string, + port int64, + domain_socket string, + transport string, + protocol string, + ssl bool) (client *thrifttest.ThriftTestClient, err error) { + + hostPort := fmt.Sprintf("%s:%d", host, port) + + var protocolFactory thrift.TProtocolFactory + switch protocol { + case "compact": + protocolFactory = thrift.NewTCompactProtocolFactory() + case "simplejson": + protocolFactory = thrift.NewTSimpleJSONProtocolFactory() + case "json": + protocolFactory = thrift.NewTJSONProtocolFactory() + case "binary": + protocolFactory = thrift.NewTBinaryProtocolFactoryDefault() + default: + return 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 { + if domain_socket != "" { + trans, err = thrift.NewTSocket(domain_socket) + } else { + trans, err = thrift.NewTSocket(hostPort) + } + } + if err != nil { + return nil, err + } + switch transport { + case "http": + trans, err = thrift.NewTHttpClient(fmt.Sprintf("http://%s/service", hostPort)) + if err != nil { + return nil, err + } + case "framed": + trans = thrift.NewTFramedTransport(trans) + case "buffered": + trans = thrift.NewTBufferedTransport(trans, 8192) + case "": + trans = trans + default: + return nil, fmt.Errorf("Invalid transport specified %s", transport) + } + + if err = trans.Open(); err != nil { + return nil, err + } + client = thrifttest.NewThriftTestClientFactory(trans, protocolFactory) + return +} diff --git a/test/go/src/common/clientserver_test.go b/test/go/src/common/clientserver_test.go new file mode 100644 index 00000000..8add0416 --- /dev/null +++ b/test/go/src/common/clientserver_test.go @@ -0,0 +1,292 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package common + +import ( + "code.google.com/p/gomock/gomock" + "errors" + "gen/thrifttest" + "reflect" + "testing" + "thrift" +) + +type test_unit struct { + host string + port int64 + domain_socket string + transport string + protocol string + ssl bool +} + +var units = []test_unit{ + {"127.0.0.1", 9090, "", "", "binary", false}, + {"127.0.0.1", 9091, "", "", "compact", false}, + {"127.0.0.1", 9092, "", "", "binary", true}, + {"127.0.0.1", 9093, "", "", "compact", true}, +} + +func TestAllConnection(t *testing.T) { + certPath = "../../../keys" + for _, unit := range units { + t.Logf("%#v", unit) + doUnit(t, &unit) + } +} + +func doUnit(t *testing.T, unit *test_unit) { + ctrl := gomock.NewController(t) + defer ctrl.Finish() + handler := NewMockThriftTest(ctrl) + server, err := StartServer(unit.host, unit.port, unit.domain_socket, unit.transport, unit.protocol, unit.ssl, handler) + if err != nil { + t.Errorf("Unable to start server", err) + t.FailNow() + } + defer server.Stop() + client, 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() + } + defer client.Transport.Close() + callEverythingWithMock(t, client, handler) +} + +var rmapmap = map[int32]map[int32]int32{ + -4: map[int32]int32{-4: -4, -3: -3, -2: -2, -1: -1}, + 4: map[int32]int32{4: 4, 3: 3, 2: 2, 1: 1}, +} + +var xxs = &thrifttest.Xtruct{ + StringThing: "Hello2", + ByteThing: 42, + I32Thing: 4242, + I64Thing: 424242, +} + +var xcept = &thrifttest.Xception{ErrorCode: 1001, Message: "some"} + +func callEverythingWithMock(t *testing.T, client *thrifttest.ThriftTestClient, handler *MockThriftTest) { + gomock.InOrder( + handler.EXPECT().TestVoid(), + handler.EXPECT().TestString("thing").Return("thing", nil), + handler.EXPECT().TestByte(int8(42)).Return(int8(42), nil), + handler.EXPECT().TestI32(int32(4242)).Return(int32(4242), nil), + handler.EXPECT().TestI64(int64(424242)).Return(int64(424242), nil), + handler.EXPECT().TestDouble(float64(42.42)).Return(float64(42.42), nil), + handler.EXPECT().TestStruct(&thrifttest.Xtruct{StringThing: "thing", ByteThing: 42, I32Thing: 4242, I64Thing: 424242}).Return(&thrifttest.Xtruct{StringThing: "thing", ByteThing: 42, I32Thing: 4242, I64Thing: 424242}, nil), + handler.EXPECT().TestNest(&thrifttest.Xtruct2{StructThing: &thrifttest.Xtruct{StringThing: "thing", ByteThing: 42, I32Thing: 4242, I64Thing: 424242}}).Return(&thrifttest.Xtruct2{StructThing: &thrifttest.Xtruct{StringThing: "thing", ByteThing: 42, I32Thing: 4242, I64Thing: 424242}}, nil), + handler.EXPECT().TestMap(map[int32]int32{1: 2, 3: 4, 5: 42}).Return(map[int32]int32{1: 2, 3: 4, 5: 42}, nil), + handler.EXPECT().TestStringMap(map[string]string{"a": "2", "b": "blah", "some": "thing"}).Return(map[string]string{"a": "2", "b": "blah", "some": "thing"}, nil), + handler.EXPECT().TestSet(map[int32]bool{1: true, 2: true, 42: true}).Return(map[int32]bool{1: true, 2: true, 42: true}, nil), + handler.EXPECT().TestList([]int32{1, 2, 42}).Return([]int32{1, 2, 42}, nil), + handler.EXPECT().TestEnum(thrifttest.Numberz_TWO).Return(thrifttest.Numberz_TWO, nil), + handler.EXPECT().TestTypedef(thrifttest.UserId(42)).Return(thrifttest.UserId(42), nil), + handler.EXPECT().TestMapMap(int32(42)).Return(rmapmap, nil), + //not testing insanity + handler.EXPECT().TestMulti(int8(42), int32(4242), int64(424242), map[int16]string{1: "blah", 2: "thing"}, thrifttest.Numberz_EIGHT, thrifttest.UserId(24)).Return(xxs, nil), + handler.EXPECT().TestException("some").Return(xcept), + handler.EXPECT().TestException("TException").Return(errors.New("Just random exception")), + handler.EXPECT().TestMultiException("Xception", "ignoreme").Return(nil, &thrifttest.Xception{ErrorCode: 1001, Message: "This is an Xception"}), + handler.EXPECT().TestMultiException("Xception2", "ignoreme").Return(nil, &thrifttest.Xception2{ErrorCode: 2002, StructThing: &thrifttest.Xtruct{StringThing: "This is an Xception2"}}), + handler.EXPECT().TestOneway(int32(2)).Return(nil), + handler.EXPECT().TestVoid(), + ) + var err error + if err = client.TestVoid(); err != nil { + t.Errorf("Unexpected error in TestVoid() call: ", err) + } + + thing, err := client.TestString("thing") + if err != nil { + t.Errorf("Unexpected error in TestString() call: ", err) + } + if thing != "thing" { + t.Errorf("Unexpected TestString() result, expected 'thing' got '%s' ", thing) + } + + b, err := client.TestByte(42) + if err != nil { + t.Errorf("Unexpected error in TestByte() call: ", err) + } + if b != 42 { + t.Errorf("Unexpected TestByte() result expected 42, got %d ", b) + } + + i32, err := client.TestI32(4242) + if err != nil { + t.Errorf("Unexpected error in TestI32() call: ", err) + } + if i32 != 4242 { + t.Errorf("Unexpected TestI32() result expected 4242, got %d ", i32) + } + + i64, err := client.TestI64(424242) + if err != nil { + t.Errorf("Unexpected error in TestI64() call: ", err) + } + if i64 != 424242 { + t.Errorf("Unexpected TestI64() result expected 424242, got %d ", i64) + } + + d, err := client.TestDouble(42.42) + if err != nil { + t.Errorf("Unexpected error in TestDouble() call: ", err) + } + if d != 42.42 { + t.Errorf("Unexpected TestDouble() result expected 42.42, got %f ", d) + } + + xs := thrifttest.NewXtruct() + xs.StringThing = "thing" + xs.ByteThing = 42 + xs.I32Thing = 4242 + xs.I64Thing = 424242 + xsret, err := client.TestStruct(xs) + if err != nil { + t.Errorf("Unexpected error in TestStruct() call: ", err) + } + if *xs != *xsret { + t.Errorf("Unexpected TestStruct() result expected %#v, got %#v ", xs, xsret) + } + + x2 := thrifttest.NewXtruct2() + x2.StructThing = xs + x2ret, err := client.TestNest(x2) + if err != nil { + t.Errorf("Unexpected error in TestNest() call: ", err) + } + if !reflect.DeepEqual(x2, x2ret) { + t.Errorf("Unexpected TestNest() result expected %#v, got %#v ", x2, x2ret) + } + + m := map[int32]int32{1: 2, 3: 4, 5: 42} + mret, err := client.TestMap(m) + if err != nil { + t.Errorf("Unexpected error in TestMap() call: ", err) + } + if !reflect.DeepEqual(m, mret) { + t.Errorf("Unexpected TestMap() result expected %#v, got %#v ", m, mret) + } + + sm := map[string]string{"a": "2", "b": "blah", "some": "thing"} + smret, err := client.TestStringMap(sm) + if err != nil { + t.Errorf("Unexpected error in TestStringMap() call: ", err) + } + if !reflect.DeepEqual(sm, smret) { + t.Errorf("Unexpected TestStringMap() result expected %#v, got %#v ", sm, smret) + } + + s := map[int32]bool{1: true, 2: true, 42: true} + sret, err := client.TestSet(s) + if err != nil { + t.Errorf("Unexpected error in TestSet() call: ", err) + } + if !reflect.DeepEqual(s, sret) { + t.Errorf("Unexpected TestSet() result expected %#v, got %#v ", s, sret) + } + + l := []int32{1, 2, 42} + lret, err := client.TestList(l) + if err != nil { + t.Errorf("Unexpected error in TestList() call: ", err) + } + if !reflect.DeepEqual(l, lret) { + t.Errorf("Unexpected TestSet() result expected %#v, got %#v ", l, lret) + } + + eret, err := client.TestEnum(thrifttest.Numberz_TWO) + if err != nil { + t.Errorf("Unexpected error in TestEnum() call: ", err) + } + if eret != thrifttest.Numberz_TWO { + t.Errorf("Unexpected TestEnum() result expected %#v, got %#v ", thrifttest.Numberz_TWO, eret) + } + + tret, err := client.TestTypedef(thrifttest.UserId(42)) + if err != nil { + t.Errorf("Unexpected error in TestTypedef() call: ", err) + } + if tret != thrifttest.UserId(42) { + t.Errorf("Unexpected TestTypedef() result expected %#v, got %#v ", thrifttest.UserId(42), tret) + } + + mapmap, err := client.TestMapMap(42) + if err != nil { + t.Errorf("Unexpected error in TestMapmap() call: ", err) + } + if !reflect.DeepEqual(mapmap, rmapmap) { + t.Errorf("Unexpected TestMapmap() result expected %#v, got %#v ", rmapmap, mapmap) + } + + xxsret, err := client.TestMulti(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) + } + if !reflect.DeepEqual(xxs, xxsret) { + t.Errorf("Unexpected TestMulti() result expected %#v, got %#v ", xxs, xxsret) + } + + err = client.TestException("some") + if err == nil { + t.Errorf("Expecting exception in TestException() call") + } + if !reflect.DeepEqual(err, xcept) { + t.Errorf("Unexpected TestException() result expected %#v, got %#v ", xcept, err) + } + + // TODO: connection is being closed on this + err = client.TestException("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) + } + + ign, err := client.TestMultiException("Xception", "ignoreme") + if ign != nil || err == nil { + t.Errorf("Expecting exception in TestMultiException() call") + } + if !reflect.DeepEqual(err, &thrifttest.Xception{ErrorCode: 1001, Message: "This is an Xception"}) { + t.Errorf("Unexpected TestMultiException() %#v ", err) + } + + ign, err = client.TestMultiException("Xception2", "ignoreme") + if ign != nil || err == nil { + t.Errorf("Expecting exception in TestMultiException() call") + } + expecting := &thrifttest.Xception2{ErrorCode: 2002, StructThing: &thrifttest.Xtruct{StringThing: "This is an Xception2"}} + + if !reflect.DeepEqual(err, expecting) { + t.Errorf("Unexpected TestMultiException() %#v ", err) + } + + err = client.TestOneway(2) + if err != nil { + t.Errorf("Unexpected error in TestOneway() call: ", err) + } + + //Make sure the connection still alive + if err = client.TestVoid(); err != nil { + t.Errorf("Unexpected error in TestVoid() call: ", err) + } +} diff --git a/test/go/src/common/mock_handler.go b/test/go/src/common/mock_handler.go new file mode 100644 index 00000000..0aed38b2 --- /dev/null +++ b/test/go/src/common/mock_handler.go @@ -0,0 +1,266 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +// Automatically generated by MockGen. DO NOT EDIT! +// Source: gen/thrifttest (interfaces: ThriftTest) + +package common + +import ( + thrifttest "gen/thrifttest" + gomock "code.google.com/p/gomock/gomock" +) + +// Mock of ThriftTest interface +type MockThriftTest struct { + ctrl *gomock.Controller + recorder *_MockThriftTestRecorder +} + +// Recorder for MockThriftTest (not exported) +type _MockThriftTestRecorder struct { + mock *MockThriftTest +} + +func NewMockThriftTest(ctrl *gomock.Controller) *MockThriftTest { + mock := &MockThriftTest{ctrl: ctrl} + mock.recorder = &_MockThriftTestRecorder{mock} + return mock +} + +func (_m *MockThriftTest) EXPECT() *_MockThriftTestRecorder { + return _m.recorder +} + +func (_m *MockThriftTest) TestByte(_param0 int8) (int8, error) { + ret := _m.ctrl.Call(_m, "TestByte", _param0) + ret0, _ := ret[0].(int8) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +func (_mr *_MockThriftTestRecorder) TestByte(arg0 interface{}) *gomock.Call { + return _mr.mock.ctrl.RecordCall(_mr.mock, "TestByte", arg0) +} + +func (_m *MockThriftTest) TestDouble(_param0 float64) (float64, error) { + ret := _m.ctrl.Call(_m, "TestDouble", _param0) + ret0, _ := ret[0].(float64) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +func (_mr *_MockThriftTestRecorder) TestDouble(arg0 interface{}) *gomock.Call { + return _mr.mock.ctrl.RecordCall(_mr.mock, "TestDouble", arg0) +} + +func (_m *MockThriftTest) TestEnum(_param0 thrifttest.Numberz) (thrifttest.Numberz, error) { + ret := _m.ctrl.Call(_m, "TestEnum", _param0) + ret0, _ := ret[0].(thrifttest.Numberz) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +func (_mr *_MockThriftTestRecorder) TestEnum(arg0 interface{}) *gomock.Call { + return _mr.mock.ctrl.RecordCall(_mr.mock, "TestEnum", arg0) +} + +func (_m *MockThriftTest) TestException(_param0 string) error { + ret := _m.ctrl.Call(_m, "TestException", _param0) + ret0, _ := ret[0].(error) + return ret0 +} + +func (_mr *_MockThriftTestRecorder) TestException(arg0 interface{}) *gomock.Call { + return _mr.mock.ctrl.RecordCall(_mr.mock, "TestException", arg0) +} + +func (_m *MockThriftTest) TestI32(_param0 int32) (int32, error) { + ret := _m.ctrl.Call(_m, "TestI32", _param0) + ret0, _ := ret[0].(int32) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +func (_mr *_MockThriftTestRecorder) TestI32(arg0 interface{}) *gomock.Call { + return _mr.mock.ctrl.RecordCall(_mr.mock, "TestI32", arg0) +} + +func (_m *MockThriftTest) TestI64(_param0 int64) (int64, error) { + ret := _m.ctrl.Call(_m, "TestI64", _param0) + ret0, _ := ret[0].(int64) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +func (_mr *_MockThriftTestRecorder) TestI64(arg0 interface{}) *gomock.Call { + return _mr.mock.ctrl.RecordCall(_mr.mock, "TestI64", arg0) +} + +func (_m *MockThriftTest) TestInsanity(_param0 *thrifttest.Insanity) (map[thrifttest.UserId]map[thrifttest.Numberz]*thrifttest.Insanity, error) { + ret := _m.ctrl.Call(_m, "TestInsanity", _param0) + ret0, _ := ret[0].(map[thrifttest.UserId]map[thrifttest.Numberz]*thrifttest.Insanity) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +func (_mr *_MockThriftTestRecorder) TestInsanity(arg0 interface{}) *gomock.Call { + return _mr.mock.ctrl.RecordCall(_mr.mock, "TestInsanity", arg0) +} + +func (_m *MockThriftTest) TestList(_param0 []int32) ([]int32, error) { + ret := _m.ctrl.Call(_m, "TestList", _param0) + ret0, _ := ret[0].([]int32) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +func (_mr *_MockThriftTestRecorder) TestList(arg0 interface{}) *gomock.Call { + return _mr.mock.ctrl.RecordCall(_mr.mock, "TestList", arg0) +} + +func (_m *MockThriftTest) TestMap(_param0 map[int32]int32) (map[int32]int32, error) { + ret := _m.ctrl.Call(_m, "TestMap", _param0) + ret0, _ := ret[0].(map[int32]int32) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +func (_mr *_MockThriftTestRecorder) TestMap(arg0 interface{}) *gomock.Call { + return _mr.mock.ctrl.RecordCall(_mr.mock, "TestMap", arg0) +} + +func (_m *MockThriftTest) TestMapMap(_param0 int32) (map[int32]map[int32]int32, error) { + ret := _m.ctrl.Call(_m, "TestMapMap", _param0) + ret0, _ := ret[0].(map[int32]map[int32]int32) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +func (_mr *_MockThriftTestRecorder) TestMapMap(arg0 interface{}) *gomock.Call { + return _mr.mock.ctrl.RecordCall(_mr.mock, "TestMapMap", arg0) +} + +func (_m *MockThriftTest) TestMulti(_param0 int8, _param1 int32, _param2 int64, _param3 map[int16]string, _param4 thrifttest.Numberz, _param5 thrifttest.UserId) (*thrifttest.Xtruct, error) { + ret := _m.ctrl.Call(_m, "TestMulti", _param0, _param1, _param2, _param3, _param4, _param5) + ret0, _ := ret[0].(*thrifttest.Xtruct) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +func (_mr *_MockThriftTestRecorder) TestMulti(arg0, arg1, arg2, arg3, arg4, arg5 interface{}) *gomock.Call { + return _mr.mock.ctrl.RecordCall(_mr.mock, "TestMulti", arg0, arg1, arg2, arg3, arg4, arg5) +} + +func (_m *MockThriftTest) TestMultiException(_param0 string, _param1 string) (*thrifttest.Xtruct, error) { + ret := _m.ctrl.Call(_m, "TestMultiException", _param0, _param1) + ret0, _ := ret[0].(*thrifttest.Xtruct) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +func (_mr *_MockThriftTestRecorder) TestMultiException(arg0, arg1 interface{}) *gomock.Call { + return _mr.mock.ctrl.RecordCall(_mr.mock, "TestMultiException", arg0, arg1) +} + +func (_m *MockThriftTest) TestNest(_param0 *thrifttest.Xtruct2) (*thrifttest.Xtruct2, error) { + ret := _m.ctrl.Call(_m, "TestNest", _param0) + ret0, _ := ret[0].(*thrifttest.Xtruct2) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +func (_mr *_MockThriftTestRecorder) TestNest(arg0 interface{}) *gomock.Call { + return _mr.mock.ctrl.RecordCall(_mr.mock, "TestNest", arg0) +} + +func (_m *MockThriftTest) TestOneway(_param0 int32) error { + ret := _m.ctrl.Call(_m, "TestOneway", _param0) + ret0, _ := ret[0].(error) + return ret0 +} + +func (_mr *_MockThriftTestRecorder) TestOneway(arg0 interface{}) *gomock.Call { + return _mr.mock.ctrl.RecordCall(_mr.mock, "TestOneway", arg0) +} + +func (_m *MockThriftTest) TestSet(_param0 map[int32]bool) (map[int32]bool, error) { + ret := _m.ctrl.Call(_m, "TestSet", _param0) + ret0, _ := ret[0].(map[int32]bool) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +func (_mr *_MockThriftTestRecorder) TestSet(arg0 interface{}) *gomock.Call { + return _mr.mock.ctrl.RecordCall(_mr.mock, "TestSet", arg0) +} + +func (_m *MockThriftTest) TestString(_param0 string) (string, error) { + ret := _m.ctrl.Call(_m, "TestString", _param0) + ret0, _ := ret[0].(string) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +func (_mr *_MockThriftTestRecorder) TestString(arg0 interface{}) *gomock.Call { + return _mr.mock.ctrl.RecordCall(_mr.mock, "TestString", arg0) +} + +func (_m *MockThriftTest) TestStringMap(_param0 map[string]string) (map[string]string, error) { + ret := _m.ctrl.Call(_m, "TestStringMap", _param0) + ret0, _ := ret[0].(map[string]string) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +func (_mr *_MockThriftTestRecorder) TestStringMap(arg0 interface{}) *gomock.Call { + return _mr.mock.ctrl.RecordCall(_mr.mock, "TestStringMap", arg0) +} + +func (_m *MockThriftTest) TestStruct(_param0 *thrifttest.Xtruct) (*thrifttest.Xtruct, error) { + ret := _m.ctrl.Call(_m, "TestStruct", _param0) + ret0, _ := ret[0].(*thrifttest.Xtruct) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +func (_mr *_MockThriftTestRecorder) TestStruct(arg0 interface{}) *gomock.Call { + return _mr.mock.ctrl.RecordCall(_mr.mock, "TestStruct", arg0) +} + +func (_m *MockThriftTest) TestTypedef(_param0 thrifttest.UserId) (thrifttest.UserId, error) { + ret := _m.ctrl.Call(_m, "TestTypedef", _param0) + ret0, _ := ret[0].(thrifttest.UserId) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +func (_mr *_MockThriftTestRecorder) TestTypedef(arg0 interface{}) *gomock.Call { + return _mr.mock.ctrl.RecordCall(_mr.mock, "TestTypedef", arg0) +} + +func (_m *MockThriftTest) TestVoid() error { + ret := _m.ctrl.Call(_m, "TestVoid") + ret0, _ := ret[0].(error) + return ret0 +} + +func (_mr *_MockThriftTestRecorder) TestVoid() *gomock.Call { + return _mr.mock.ctrl.RecordCall(_mr.mock, "TestVoid") +} diff --git a/test/go/src/common/printing_handler.go b/test/go/src/common/printing_handler.go new file mode 100644 index 00000000..e93621f8 --- /dev/null +++ b/test/go/src/common/printing_handler.go @@ -0,0 +1,351 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package common + +import ( + "errors" + "fmt" + . "gen/thrifttest" + "time" +) + +var PrintingHandler = &printingHandler{} + +type printingHandler struct{} + +// Prints "testVoid()" and returns nothing. +func (p *printingHandler) TestVoid() (err error) { + fmt.Println("testVoid()") + return nil +} + +// Prints 'testString("%s")' with thing as '%s' +// @param string thing - the string to print +// @return string - returns the string 'thing' +// +// Parameters: +// - Thing +func (p *printingHandler) TestString(thing string) (r string, err error) { + fmt.Printf("testString(\"%s\")\n", thing) + return thing, nil +} + +// Prints 'testByte("%d")' with thing as '%d' +// @param byte thing - the byte to print +// @return byte - returns the byte 'thing' +// +// Parameters: +// - Thing +func (p *printingHandler) TestByte(thing int8) (r int8, err error) { + fmt.Printf("testByte(%d)\n", thing) + return thing, nil +} + +// Prints 'testI32("%d")' with thing as '%d' +// @param i32 thing - the i32 to print +// @return i32 - returns the i32 'thing' +// +// Parameters: +// - Thing +func (p *printingHandler) TestI32(thing int32) (r int32, err error) { + fmt.Printf("testI32(%d)\n", thing) + return thing, nil +} + +// Prints 'testI64("%d")' with thing as '%d' +// @param i64 thing - the i64 to print +// @return i64 - returns the i64 'thing' +// +// Parameters: +// - Thing +func (p *printingHandler) TestI64(thing int64) (r int64, err error) { + fmt.Printf("testI64(%d)\n", thing) + return thing, nil +} + +// Prints 'testDouble("%f")' with thing as '%f' +// @param double thing - the double to print +// @return double - returns the double 'thing' +// +// Parameters: +// - Thing +func (p *printingHandler) TestDouble(thing float64) (r float64, err error) { + fmt.Printf("testDouble(%f)\n", thing) + return thing, nil +} + +// Prints 'testStruct("{%s}")' where thing has been formatted into a string of comma seperated values +// @param Xtruct thing - the Xtruct to print +// @return Xtruct - returns the Xtruct 'thing' +// +// Parameters: +// - Thing +func (p *printingHandler) TestStruct(thing *Xtruct) (r *Xtruct, err error) { + fmt.Printf("testStruct({\"%s\", %d, %d, %d})\n", thing.StringThing, thing.ByteThing, thing.I32Thing, thing.I64Thing) + return thing, err +} + +// Prints 'testNest("{%s}")' where thing has been formatted into a string of the nested struct +// @param Xtruct2 thing - the Xtruct2 to print +// @return Xtruct2 - returns the Xtruct2 'thing' +// +// Parameters: +// - Thing +func (p *printingHandler) TestNest(nest *Xtruct2) (r *Xtruct2, err error) { + thing := nest.StructThing + fmt.Printf("testNest({%d, {\"%s\", %d, %d, %d}, %d})\n", nest.ByteThing, thing.StringThing, thing.ByteThing, thing.I32Thing, thing.I64Thing, nest.I32Thing) + return nest, nil +} + +// Prints 'testMap("{%s")' where thing has been formatted into a string of 'key => value' pairs +// seperated by commas and new lines +// @param map thing - the map to print +// @return map - returns the map 'thing' +// +// Parameters: +// - Thing +func (p *printingHandler) TestMap(thing map[int32]int32) (r map[int32]int32, err error) { + fmt.Printf("testMap({") + first := true + for k, v := range thing { + if first { + first = false + } else { + fmt.Printf(", ") + } + fmt.Printf("%d => %d", k, v) + } + fmt.Printf("})\n") + return thing, nil +} + +// Prints 'testStringMap("{%s}")' where thing has been formatted into a string of 'key => value' pairs +// seperated by commas and new lines +// @param map thing - the map to print +// @return map - returns the map 'thing' +// +// Parameters: +// - Thing +func (p *printingHandler) TestStringMap(thing map[string]string) (r map[string]string, err error) { + fmt.Printf("testStringMap({") + first := true + for k, v := range thing { + if first { + first = false + } else { + fmt.Printf(", ") + } + fmt.Printf("%s => %s", k, v) + } + fmt.Printf("})\n") + return thing, nil +} + +// Prints 'testSet("{%s}")' where thing has been formatted into a string of values +// seperated by commas and new lines +// @param set thing - the set to print +// @return set - returns the set 'thing' +// +// Parameters: +// - Thing +func (p *printingHandler) TestSet(thing map[int32]bool) (r map[int32]bool, err error) { + fmt.Printf("testSet({") + first := true + for k, _ := range thing { + if first { + first = false + } else { + fmt.Printf(", ") + } + fmt.Printf("%d", k) + } + fmt.Printf("})\n") + return thing, nil +} + +// Prints 'testList("{%s}")' where thing has been formatted into a string of values +// seperated by commas and new lines +// @param list thing - the list to print +// @return list - returns the list 'thing' +// +// Parameters: +// - Thing +func (p *printingHandler) TestList(thing []int32) (r []int32, err error) { + fmt.Printf("testList({") + for i, v := range thing { + if i != 0 { + fmt.Printf(", ") + } + fmt.Printf("%d", v) + } + fmt.Printf("})\n") + return thing, nil +} + +// Prints 'testEnum("%d")' where thing has been formatted into it's numeric value +// @param Numberz thing - the Numberz to print +// @return Numberz - returns the Numberz 'thing' +// +// Parameters: +// - Thing +func (p *printingHandler) TestEnum(thing Numberz) (r Numberz, err error) { + fmt.Printf("testEnum(%d)\n", thing) + return thing, nil +} + +// Prints 'testTypedef("%d")' with thing as '%d' +// @param UserId thing - the UserId to print +// @return UserId - returns the UserId 'thing' +// +// Parameters: +// - Thing +func (p *printingHandler) TestTypedef(thing UserId) (r UserId, err error) { + fmt.Printf("testTypedef(%d)\n", thing) + return thing, nil +} + +// Prints 'testMapMap("%d")' with hello as '%d' +// @param i32 hello - the i32 to print +// @return map> - returns a dictionary with these values: +// {-4 => {-4 => -4, -3 => -3, -2 => -2, -1 => -1, }, 4 => {1 => 1, 2 => 2, 3 => 3, 4 => 4, }, } +// +// Parameters: +// - Hello +func (p *printingHandler) TestMapMap(hello int32) (r map[int32]map[int32]int32, err error) { + fmt.Printf("testMapMap(%d)\n", hello) + + r = map[int32]map[int32]int32{ + -4: map[int32]int32{-4: -4, -3: -3, -2: -2, -1: -1}, + 4: map[int32]int32{4: 4, 3: 3, 2: 2, 1: 1}, + } + return +} + +// So you think you've got this all worked, out eh? +// +// Creates a the returned map with these values and prints it out: +// { 1 => { 2 => argument, +// 3 => argument, +// }, +// 2 => { 6 => , }, +// } +// @return map> - a map with the above values +// +// Parameters: +// - Argument +func (p *printingHandler) TestInsanity(argument *Insanity) (r map[UserId]map[Numberz]*Insanity, err error) { + return nil, errors.New("No Insanity") +} + +// Prints 'testMulti()' +// @param byte arg0 - +// @param i32 arg1 - +// @param i64 arg2 - +// @param map arg3 - +// @param Numberz arg4 - +// @param UserId arg5 - +// @return Xtruct - returns an Xtruct with StringThing = "Hello2, ByteThing = arg0, I32Thing = arg1 +// and I64Thing = arg2 +// +// Parameters: +// - Arg0 +// - Arg1 +// - Arg2 +// - Arg3 +// - Arg4 +// - Arg5 +func (p *printingHandler) TestMulti(arg0 int8, arg1 int32, arg2 int64, arg3 map[int16]string, arg4 Numberz, arg5 UserId) (r *Xtruct, err error) { + fmt.Printf("testMulti()\n") + r = NewXtruct() + + r.StringThing = "Hello2" + r.ByteThing = arg0 + r.I32Thing = arg1 + r.I64Thing = arg2 + return +} + +// Print 'testException(%s)' with arg as '%s' +// @param string arg - a string indication what type of exception to throw +// if arg == "Xception" throw Xception with errorCode = 1001 and message = arg +// elsen if arg == "TException" throw TException +// else do not throw anything +// +// Parameters: +// - Arg +func (p *printingHandler) TestException(arg string) (err error) { + fmt.Printf("testException(%s)\n", arg) + switch arg { + case "Xception": + e := NewXception() + e.ErrorCode = 1001 + e.Message = arg + return e + case "TException": + return errors.New("Just TException") + } + return +} + +// Print 'testMultiException(%s, %s)' with arg0 as '%s' and arg1 as '%s' +// @param string arg - a string indication what type of exception to throw +// if arg0 == "Xception" throw Xception with errorCode = 1001 and message = "This is an Xception" +// elsen if arg0 == "Xception2" throw Xception2 with errorCode = 2002 and message = "This is an Xception2" +// else do not throw anything +// @return Xtruct - an Xtruct with StringThing = arg1 +// +// Parameters: +// - Arg0 +// - Arg1 +func (p *printingHandler) TestMultiException(arg0 string, arg1 string) (r *Xtruct, err error) { + fmt.Printf("testMultiException(%s, %s)\n", arg0, arg1) + switch arg0 { + + case "Xception": + e := NewXception() + e.ErrorCode = 1001 + e.Message = "This is an Xception" + return nil, e + case "Xception2": + e := NewXception2() + e.ErrorCode = 2002 + e.StructThing = NewXtruct() + e.StructThing.StringThing = "This is an Xception2" + return nil, e + default: + r = NewXtruct() + r.StringThing = arg1 + return + } +} + +// Print 'testOneway(%d): Sleeping...' with secondsToSleep as '%d' +// sleep 'secondsToSleep' +// Print 'testOneway(%d): done sleeping!' with secondsToSleep as '%d' +// @param i32 secondsToSleep - the number of seconds to sleep +// +// Parameters: +// - SecondsToSleep +func (p *printingHandler) TestOneway(secondsToSleep int32) (err error) { + fmt.Printf("testOneway(%d): Sleeping...\n", secondsToSleep) + time.Sleep(time.Second * time.Duration(secondsToSleep)) + fmt.Printf("testOneway(%d): done sleeping!\n", secondsToSleep) + return +} diff --git a/test/go/src/common/server.go b/test/go/src/common/server.go new file mode 100644 index 00000000..e77cd374 --- /dev/null +++ b/test/go/src/common/server.go @@ -0,0 +1,113 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package common + +import ( + "crypto/tls" + "flag" + "fmt" + "gen/thrifttest" + "thrift" +) + +var ( + debugServerProtocol bool + certPath string +) + +func init() { + flag.BoolVar(&debugServerProtocol, "debug_server_protocol", false, "turn server protocol trace on") +} + +func StartServer( + host string, + port int64, + domain_socket string, + transport string, + protocol string, + ssl bool, + handler thrifttest.ThriftTest) (srv *thrift.TSimpleServer, err error) { + + hostPort := fmt.Sprintf("%s:%d", host, port) + + var protocolFactory thrift.TProtocolFactory + switch protocol { + case "compact": + protocolFactory = thrift.NewTCompactProtocolFactory() + case "simplejson": + protocolFactory = thrift.NewTSimpleJSONProtocolFactory() + case "json": + protocolFactory = thrift.NewTJSONProtocolFactory() + case "binary": + protocolFactory = thrift.NewTBinaryProtocolFactoryDefault() + default: + return nil, fmt.Errorf("Invalid protocol specified %s", protocol) + } + if debugServerProtocol { + protocolFactory = thrift.NewTDebugProtocolFactory(protocolFactory, "server:") + } + + var serverTransport thrift.TServerTransport + if ssl { + cfg := new(tls.Config) + if cert, err := tls.LoadX509KeyPair(certPath+"/server.crt", certPath+"/server.key"); err != nil { + return nil, err + } else { + cfg.Certificates = append(cfg.Certificates, cert) + } + serverTransport, err = thrift.NewTSSLServerSocket(hostPort, cfg) + } else { + if domain_socket != "" { + serverTransport, err = thrift.NewTServerSocket(domain_socket) + } else { + serverTransport, err = thrift.NewTServerSocket(hostPort) + } + } + if err != nil { + return nil, err + } + + var transportFactory thrift.TTransportFactory + + switch transport { + case "http": + return nil, fmt.Errorf("Http server transport is not supported") + // trans, err = thrift.NewTHttpClient(fmt.Sprintf("http://%s/service", hostPort)) + // if err != nil { + // return nil, err + // } + case "framed": + transportFactory = thrift.NewTTransportFactory() + transportFactory = thrift.NewTFramedTransportFactory(transportFactory) + case "buffered": + transportFactory = thrift.NewTBufferedTransportFactory(8192) + case "": + transportFactory = thrift.NewTTransportFactory() + default: + return nil, fmt.Errorf("Invalid transport specified %s", transport) + } + processor := thrifttest.NewThriftTestProcessor(handler) + server := thrift.NewTSimpleServer4(processor, serverTransport, transportFactory, protocolFactory) + if err = server.Listen(); err != nil { + return + } + go server.AcceptLoop() + return server, nil +} diff --git a/test/go/src/common/simple_handler.go b/test/go/src/common/simple_handler.go new file mode 100644 index 00000000..433616da --- /dev/null +++ b/test/go/src/common/simple_handler.go @@ -0,0 +1,147 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package common + +import ( + "errors" + . "gen/thrifttest" + "time" +) + +var SimpleHandler = &simpleHandler{} + +type simpleHandler struct{} + +func (p *simpleHandler) TestVoid() (err error) { + return nil +} + +func (p *simpleHandler) TestString(thing string) (r string, err error) { + return thing, nil +} + +func (p *simpleHandler) TestByte(thing int8) (r int8, err error) { + return thing, nil +} + +func (p *simpleHandler) TestI32(thing int32) (r int32, err error) { + return thing, nil +} + +func (p *simpleHandler) TestI64(thing int64) (r int64, err error) { + return thing, nil +} + +func (p *simpleHandler) TestDouble(thing float64) (r float64, err error) { + return thing, nil +} + +func (p *simpleHandler) TestStruct(thing *Xtruct) (r *Xtruct, err error) { + return r, err +} + +func (p *simpleHandler) TestNest(nest *Xtruct2) (r *Xtruct2, err error) { + return nest, nil +} + +func (p *simpleHandler) TestMap(thing map[int32]int32) (r map[int32]int32, err error) { + return thing, nil +} + +func (p *simpleHandler) TestStringMap(thing map[string]string) (r map[string]string, err error) { + return thing, nil +} + +func (p *simpleHandler) TestSet(thing map[int32]bool) (r map[int32]bool, err error) { + return thing, nil +} + +func (p *simpleHandler) TestList(thing []int32) (r []int32, err error) { + return thing, nil +} + +func (p *simpleHandler) TestEnum(thing Numberz) (r Numberz, err error) { + return thing, nil +} + +func (p *simpleHandler) TestTypedef(thing UserId) (r UserId, err error) { + return thing, nil +} + +func (p *simpleHandler) TestMapMap(hello int32) (r map[int32]map[int32]int32, err error) { + + r = map[int32]map[int32]int32{ + -4: map[int32]int32{-4: -4, -3: -3, -2: -2, -1: -1}, + 4: map[int32]int32{4: 4, 3: 3, 2: 2, 1: 1}, + } + return +} + +func (p *simpleHandler) TestInsanity(argument *Insanity) (r map[UserId]map[Numberz]*Insanity, err error) { + return nil, errors.New("No Insanity") +} + +func (p *simpleHandler) TestMulti(arg0 int8, arg1 int32, arg2 int64, arg3 map[int16]string, arg4 Numberz, arg5 UserId) (r *Xtruct, err error) { + r = NewXtruct() + + r.StringThing = "Hello2" + r.ByteThing = arg0 + r.I32Thing = arg1 + r.I64Thing = arg2 + return +} + +func (p *simpleHandler) TestException(arg string) (err error) { + switch arg { + case "Xception": + e := NewXception() + e.ErrorCode = 1001 + e.Message = arg + return e + case "TException": + return errors.New("Just TException") + } + return +} + +func (p *simpleHandler) TestMultiException(arg0 string, arg1 string) (r *Xtruct, err error) { + switch arg0 { + + case "Xception": + e := NewXception() + e.ErrorCode = 1001 + e.Message = "This is an Xception" + return nil, e + case "Xception2": + e := NewXception2() + e.ErrorCode = 2002 + e.StructThing.StringThing = "This is an Xception2" + return nil, e + default: + r = NewXtruct() + r.StringThing = arg1 + return + } +} + +func (p *simpleHandler) TestOneway(secondsToSleep int32) (err error) { + time.Sleep(time.Second * time.Duration(secondsToSleep)) + return +} -- 2.17.1