diff options
Diffstat (limited to '')
11 files changed, 1917 insertions, 0 deletions
diff --git a/src/jaegertracing/thrift/test/go/Makefile.am b/src/jaegertracing/thrift/test/go/Makefile.am new file mode 100644 index 000000000..3cf6a70ee --- /dev/null +++ b/src/jaegertracing/thrift/test/go/Makefile.am @@ -0,0 +1,65 @@ +# +# 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. +# + +BUILT_SOURCES = gopath + +THRIFTCMD = $(THRIFT) -out src/gen --gen go:thrift_import=thrift$(COMPILER_EXTRAFLAG) +THRIFTTEST = $(top_srcdir)/test/ThriftTest.thrift + +precross: bin/testclient bin/testserver + +ThriftTest.thrift: $(THRIFTTEST) + grep -v list.*map.*list.*map $(THRIFTTEST) > ThriftTest.thrift + +.PHONY: gopath + +# Thrift for GO has problems with complex map keys: THRIFT-2063 +gopath: $(THRIFT) ThriftTest.thrift + mkdir -p src/gen + $(THRIFTCMD) ThriftTest.thrift + $(THRIFTCMD) ../StressTest.thrift + GOPATH=`pwd` $(GO) get github.com/golang/mock/gomock || true + sed -i 's/\"context\"/\"golang.org\/x\/net\/context\"/g' src/github.com/golang/mock/gomock/controller.go || true + GOPATH=`pwd` $(GO) get github.com/golang/mock/gomock + ln -nfs ../../../lib/go/thrift src/thrift + 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/github.com/golang src/thrift bin pkg gopath ThriftTest.thrift + +check_PROGRAMS: bin/testclient bin/testserver bin/stress + +check: gopath genmock + GOPATH=`pwd` $(GO) test -v common/... + +genmock: gopath + sh genmock.sh + +EXTRA_DIST = \ + src/bin \ + src/common diff --git a/src/jaegertracing/thrift/test/go/genmock.sh b/src/jaegertracing/thrift/test/go/genmock.sh new file mode 100644 index 000000000..3ba41b9ed --- /dev/null +++ b/src/jaegertracing/thrift/test/go/genmock.sh @@ -0,0 +1,15 @@ +#!/bin/sh +set -e + +export GOPATH=`pwd` +export GOBIN=`pwd`/bin +export GO111MODULE=off + +mkdir -p src/github.com/golang/mock +cd src/github.com/golang +curl -fsSL https://github.com/golang/mock/archive/v1.2.0.tar.gz -o mock.tar.gz +tar -xzvf mock.tar.gz -C mock --strip-components=1 +cd mock/mockgen +go install . +cd ../../../../../ +bin/mockgen -destination=src/common/mock_handler.go -package=common gen/thrifttest ThriftTest diff --git a/src/jaegertracing/thrift/test/go/src/bin/stress/main.go b/src/jaegertracing/thrift/test/go/src/bin/stress/main.go new file mode 100644 index 000000000..f2e0f2073 --- /dev/null +++ b/src/jaegertracing/thrift/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 ( + "context" + "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]struct{}{-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.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(ctx context.Context) (err error) { + atomic.AddInt64(&counter, 1) + return nil +} +func (h *handler) EchoByte(ctx context.Context, arg int8) (r int8, err error) { + atomic.AddInt64(&counter, 1) + return arg, nil +} +func (h *handler) EchoI32(ctx context.Context, arg int32) (r int32, err error) { + atomic.AddInt64(&counter, 1) + return arg, nil +} +func (h *handler) EchoI64(ctx context.Context, arg int64) (r int64, err error) { + atomic.AddInt64(&counter, 1) + return arg, nil +} +func (h *handler) EchoString(ctx context.Context, arg string) (r string, err error) { + atomic.AddInt64(&counter, 1) + return arg, nil +} +func (h *handler) EchoList(ctx context.Context, arg []int8) (r []int8, err error) { + atomic.AddInt64(&counter, 1) + return arg, nil +} +func (h *handler) EchoSet(ctx context.Context, arg map[int8]struct{}) (r map[int8]struct{}, err error) { + atomic.AddInt64(&counter, 1) + return arg, nil +} +func (h *handler) EchoMap(ctx context.Context, arg map[int8]int8) (r map[int8]int8, err error) { + atomic.AddInt64(&counter, 1) + return arg, nil +} diff --git a/src/jaegertracing/thrift/test/go/src/bin/testclient/main.go b/src/jaegertracing/thrift/test/go/src/bin/testclient/main.go new file mode 100644 index 000000000..4357ee83f --- /dev/null +++ b/src/jaegertracing/thrift/test/go/src/bin/testclient/main.go @@ -0,0 +1,316 @@ +/* + * 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" + "context" + "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, zlib") +var protocol = flag.String("protocol", "binary", "Protocol: binary, compact, json") +var ssl = flag.Bool("ssl", false, "Encrypted Transport using SSL") +var zlib = flag.Bool("zlib", false, "Wrapped Transport using Zlib") +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"} +var defaultCtx = context.Background() + +func callEverything(client *thrifttest.ThriftTestClient) { + var err error + if err = client.TestVoid(defaultCtx); err != nil { + t.Fatalf("Unexpected error in TestVoid() call: ", err) + } + + thing, err := client.TestString(defaultCtx, "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) + } + + bl, err := client.TestBool(defaultCtx, true) + if err != nil { + t.Fatalf("Unexpected error in TestBool() call: ", err) + } + if !bl { + t.Fatalf("Unexpected TestBool() result expected true, got %f ", bl) + } + bl, err = client.TestBool(defaultCtx, false) + if err != nil { + t.Fatalf("Unexpected error in TestBool() call: ", err) + } + if bl { + t.Fatalf("Unexpected TestBool() result expected false, got %f ", bl) + } + + b, err := client.TestByte(defaultCtx, 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(defaultCtx, 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(defaultCtx, 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(defaultCtx, 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) + } + + binout := make([]byte, 256) + for i := 0; i < 256; i++ { + binout[i] = byte(i) + } + bin, err := client.TestBinary(defaultCtx, binout) + for i := 0; i < 256; i++ { + if binout[i] != bin[i] { + t.Fatalf("Unexpected TestBinary() result expected %d, got %d ", binout[i], bin[i]) + } + } + + xs := thrifttest.NewXtruct() + xs.StringThing = "thing" + xs.ByteThing = 42 + xs.I32Thing = 4242 + xs.I64Thing = 424242 + xsret, err := client.TestStruct(defaultCtx, 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(defaultCtx, 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(defaultCtx, 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(defaultCtx, 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 := []int32{1, 2, 42} + sret, err := client.TestSet(defaultCtx, s) + if err != nil { + t.Fatalf("Unexpected error in TestSet() call: ", err) + } + // Sets can be in any order, but Go slices are ordered, so reflect.DeepEqual won't work. + stemp := map[int32]struct{}{} + for _, val := range s { + stemp[val] = struct{}{} + } + for _, val := range sret { + if _, ok := stemp[val]; !ok { + t.Fatalf("Unexpected TestSet() result expected %#v, got %#v ", s, sret) + } + } + + l := []int32{1, 2, 42} + lret, err := client.TestList(defaultCtx, l) + if err != nil { + t.Fatalf("Unexpected error in TestList() call: ", err) + } + if !reflect.DeepEqual(l, lret) { + t.Fatalf("Unexpected TestList() result expected %#v, got %#v ", l, lret) + } + + eret, err := client.TestEnum(defaultCtx, 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(defaultCtx, 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(defaultCtx, 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) + } + + crazy := thrifttest.NewInsanity() + crazy.UserMap = map[thrifttest.Numberz]thrifttest.UserId{ + thrifttest.Numberz_FIVE: 5, + thrifttest.Numberz_EIGHT: 8, + } + truck1 := thrifttest.NewXtruct() + truck1.StringThing = "Goodbye4" + truck1.ByteThing = 4 + truck1.I32Thing = 4 + truck1.I64Thing = 4 + truck2 := thrifttest.NewXtruct() + truck2.StringThing = "Hello2" + truck2.ByteThing = 2 + truck2.I32Thing = 2 + truck2.I64Thing = 2 + crazy.Xtructs = []*thrifttest.Xtruct{ + truck1, + truck2, + } + insanity, err := client.TestInsanity(defaultCtx, crazy) + if err != nil { + t.Fatalf("Unexpected error in TestInsanity() call: ", err) + } + if !reflect.DeepEqual(crazy, insanity[1][2]) { + t.Fatalf("Unexpected TestInsanity() first result expected %#v, got %#v ", + crazy, + insanity[1][2]) + } + if !reflect.DeepEqual(crazy, insanity[1][3]) { + t.Fatalf("Unexpected TestInsanity() second result expected %#v, got %#v ", + crazy, + insanity[1][3]) + } + if len(insanity[2][6].UserMap) > 0 || len(insanity[2][6].Xtructs) > 0 { + t.Fatalf("Unexpected TestInsanity() non-empty result got %#v ", + insanity[2][6]) + } + + 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.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(defaultCtx, "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) + } + + err = client.TestException(defaultCtx, "TException") + _, ok := err.(thrift.TApplicationException) + if err == nil || !ok { + t.Fatalf("Unexpected TestException() result expected ApplicationError, got %#v ", err) + } + + ign, err := client.TestMultiException(defaultCtx, "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(defaultCtx, "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(defaultCtx, 2) + if err != nil { + t.Fatalf("Unexpected error in TestOneway() call: ", err) + } + + //Make sure the connection still alive + if err = client.TestVoid(defaultCtx); err != nil { + t.Fatalf("Unexpected error in TestVoid() call: ", err) + } +} diff --git a/src/jaegertracing/thrift/test/go/src/bin/testserver/main.go b/src/jaegertracing/thrift/test/go/src/bin/testserver/main.go new file mode 100644 index 000000000..6fc1185a2 --- /dev/null +++ b/src/jaegertracing/thrift/test/go/src/bin/testserver/main.go @@ -0,0 +1,71 @@ +/* + * 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" + "fmt" + "log" + "net/http" + "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, zlib") +var protocol = flag.String("protocol", "binary", "Protocol: binary, compact, json, header") +var ssl = flag.Bool("ssl", false, "Encrypted Transport using SSL") +var zlib = flag.Bool("zlib", false, "Wrapped Transport using Zlib") +var certPath = flag.String("certPath", "keys", "Directory that contains SSL certificates") + +func main() { + flag.Parse() + + processor, serverTransport, transportFactory, protocolFactory, err := common.GetServerParams(*host, *port, *domain_socket, *transport, *protocol, *ssl, *certPath, common.PrintingHandler) + + if err != nil { + log.Fatalf("Unable to process server params: %v", err) + } + + if *transport == "http" { + http.HandleFunc("/", thrift.NewThriftHandlerFunc(processor, protocolFactory, protocolFactory)) + + if *ssl { + err := http.ListenAndServeTLS(fmt.Sprintf(":%d", *port), + *certPath+"/server.pem", *certPath+"/server.key", nil) + + if err != nil { + fmt.Println(err) + return + } + } else { + http.ListenAndServe(fmt.Sprintf(":%d", *port), nil) + } + } else { + server := thrift.NewTSimpleServer4(processor, serverTransport, transportFactory, protocolFactory) + if err = server.Listen(); err != nil { + return + } + go server.AcceptLoop() + server.Serve() + } +} diff --git a/src/jaegertracing/thrift/test/go/src/common/client.go b/src/jaegertracing/thrift/test/go/src/common/client.go new file mode 100644 index 000000000..ed820aeaf --- /dev/null +++ b/src/jaegertracing/thrift/test/go/src/common/client.go @@ -0,0 +1,111 @@ +/* + * 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 ( + "compress/zlib" + "crypto/tls" + "flag" + "fmt" + "gen/thrifttest" + "net/http" + "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, trans thrift.TTransport, 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() + case "header": + protocolFactory = thrift.NewTHeaderProtocolFactory() + default: + return nil, nil, fmt.Errorf("Invalid protocol specified %s", protocol) + } + if debugClientProtocol { + protocolFactory = thrift.NewTDebugProtocolFactory(protocolFactory, "client:") + } + 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, nil, err + } + switch transport { + case "http": + if ssl { + tr := &http.Transport{ + TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, + } + client := &http.Client{Transport: tr} + trans, err = thrift.NewTHttpPostClientWithOptions(fmt.Sprintf("https://%s/", hostPort), thrift.THttpClientOptions{Client: client}) + fmt.Println(hostPort) + } else { + trans, err = thrift.NewTHttpPostClient(fmt.Sprintf("http://%s/", hostPort)) + } + case "framed": + trans = thrift.NewTFramedTransport(trans) + case "buffered": + trans = thrift.NewTBufferedTransport(trans, 8192) + case "zlib": + trans, err = thrift.NewTZlibTransport(trans, zlib.BestCompression) + case "": + trans = trans + default: + 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, nil, err + } + iprot := protocolFactory.GetProtocol(trans) + oprot := protocolFactory.GetProtocol(trans) + client = thrifttest.NewThriftTestClient(thrift.NewTStandardClient(iprot, oprot)) + return +} diff --git a/src/jaegertracing/thrift/test/go/src/common/clientserver_test.go b/src/jaegertracing/thrift/test/go/src/common/clientserver_test.go new file mode 100644 index 000000000..9f93c4c6c --- /dev/null +++ b/src/jaegertracing/thrift/test/go/src/common/clientserver_test.go @@ -0,0 +1,335 @@ +/* + * 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 ( + "context" + "errors" + "gen/thrifttest" + "reflect" + "sync" + "testing" + "thrift" + + "github.com/golang/mock/gomock" +) + +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", 9095, "", "", "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" + wg := &sync.WaitGroup{} + wg.Add(len(units)) + for _, unit := range units { + go func(u test_unit) { + defer wg.Done() + doUnit(t, &u) + }(unit) + } + wg.Wait() +} + +func doUnit(t *testing.T, unit *test_unit) { + ctrl := gomock.NewController(t) + defer ctrl.Finish() + handler := NewMockThriftTest(ctrl) + + processor, serverTransport, transportFactory, protocolFactory, err := GetServerParams(unit.host, unit.port, unit.domain_socket, unit.transport, unit.protocol, unit.ssl, "../../../keys", handler) + + server := thrift.NewTSimpleServer4(processor, serverTransport, transportFactory, protocolFactory) + if err = server.Listen(); err != nil { + t.Errorf("Unable to start server: %v", err) + return + } + go server.AcceptLoop() + defer server.Stop() + 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: %v", err) + return + } + defer trans.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"} +var defaultCtx = context.Background() + +func callEverythingWithMock(t *testing.T, client *thrifttest.ThriftTestClient, handler *MockThriftTest) { + gomock.InOrder( + handler.EXPECT().TestVoid(gomock.Any()), + handler.EXPECT().TestString(gomock.Any(), "thing").Return("thing", nil), + handler.EXPECT().TestBool(gomock.Any(), true).Return(true, nil), + handler.EXPECT().TestBool(gomock.Any(), false).Return(false, nil), + handler.EXPECT().TestByte(gomock.Any(), int8(42)).Return(int8(42), nil), + handler.EXPECT().TestI32(gomock.Any(), int32(4242)).Return(int32(4242), nil), + handler.EXPECT().TestI64(gomock.Any(), int64(424242)).Return(int64(424242), nil), + // TODO: add TestBinary() + handler.EXPECT().TestDouble(gomock.Any(), float64(42.42)).Return(float64(42.42), nil), + handler.EXPECT().TestStruct(gomock.Any(), &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(gomock.Any(), &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(gomock.Any(), map[int32]int32{1: 2, 3: 4, 5: 42}).Return(map[int32]int32{1: 2, 3: 4, 5: 42}, nil), + handler.EXPECT().TestStringMap(gomock.Any(), map[string]string{"a": "2", "b": "blah", "some": "thing"}).Return(map[string]string{"a": "2", "b": "blah", "some": "thing"}, nil), + handler.EXPECT().TestSet(gomock.Any(), []int32{1, 2, 42}).Return([]int32{1, 2, 42}, nil), + handler.EXPECT().TestList(gomock.Any(), []int32{1, 2, 42}).Return([]int32{1, 2, 42}, nil), + handler.EXPECT().TestEnum(gomock.Any(), thrifttest.Numberz_TWO).Return(thrifttest.Numberz_TWO, nil), + handler.EXPECT().TestTypedef(gomock.Any(), thrifttest.UserId(42)).Return(thrifttest.UserId(42), nil), + handler.EXPECT().TestMapMap(gomock.Any(), int32(42)).Return(rmapmap, nil), + // TODO: not testing insanity + handler.EXPECT().TestMulti(gomock.Any(), 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(gomock.Any(), "some").Return(xcept), + handler.EXPECT().TestException(gomock.Any(), "TException").Return(errors.New("Just random exception")), + handler.EXPECT().TestMultiException(gomock.Any(), "Xception", "ignoreme").Return(nil, &thrifttest.Xception{ErrorCode: 1001, Message: "This is an Xception"}), + handler.EXPECT().TestMultiException(gomock.Any(), "Xception2", "ignoreme").Return(nil, &thrifttest.Xception2{ErrorCode: 2002, StructThing: &thrifttest.Xtruct{StringThing: "This is an Xception2"}}), + handler.EXPECT().TestOneway(gomock.Any(), int32(2)).Return(nil), + handler.EXPECT().TestVoid(gomock.Any()), + ) + var err error + if err = client.TestVoid(defaultCtx); err != nil { + t.Errorf("Unexpected error in TestVoid() call: %s", err) + } + + thing, err := client.TestString(defaultCtx, "thing") + if err != nil { + t.Errorf("Unexpected error in TestString() call: %s", err) + } + if thing != "thing" { + t.Errorf("Unexpected TestString() result, expected 'thing' got '%s' ", thing) + } + + bl, err := client.TestBool(defaultCtx, true) + if err != nil { + t.Errorf("Unexpected error in TestBool() call: %s", err) + } + if !bl { + t.Errorf("Unexpected TestBool() result expected true, got %v ", bl) + } + bl, err = client.TestBool(defaultCtx, false) + if err != nil { + t.Errorf("Unexpected error in TestBool() call: %s", err) + } + if bl { + t.Errorf("Unexpected TestBool() result expected false, got %v ", bl) + } + + b, err := client.TestByte(defaultCtx, 42) + if err != nil { + t.Errorf("Unexpected error in TestByte() call: %s", err) + } + if b != 42 { + t.Errorf("Unexpected TestByte() result expected 42, got %d ", b) + } + + i32, err := client.TestI32(defaultCtx, 4242) + if err != nil { + t.Errorf("Unexpected error in TestI32() call: %s", err) + } + if i32 != 4242 { + t.Errorf("Unexpected TestI32() result expected 4242, got %d ", i32) + } + + i64, err := client.TestI64(defaultCtx, 424242) + if err != nil { + t.Errorf("Unexpected error in TestI64() call: %s", err) + } + if i64 != 424242 { + t.Errorf("Unexpected TestI64() result expected 424242, got %d ", i64) + } + + d, err := client.TestDouble(defaultCtx, 42.42) + if err != nil { + t.Errorf("Unexpected error in TestDouble() call: %s", err) + } + if d != 42.42 { + t.Errorf("Unexpected TestDouble() result expected 42.42, got %f ", d) + } + + // TODO: add TestBinary() call + + xs := thrifttest.NewXtruct() + xs.StringThing = "thing" + xs.ByteThing = 42 + xs.I32Thing = 4242 + xs.I64Thing = 424242 + xsret, err := client.TestStruct(defaultCtx, xs) + if err != nil { + t.Errorf("Unexpected error in TestStruct() call: %s", 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(defaultCtx, x2) + if err != nil { + t.Errorf("Unexpected error in TestNest() call: %s", 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(defaultCtx, m) + if err != nil { + t.Errorf("Unexpected error in TestMap() call: %s", 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(defaultCtx, sm) + if err != nil { + t.Errorf("Unexpected error in TestStringMap() call: %s", err) + } + if !reflect.DeepEqual(sm, smret) { + t.Errorf("Unexpected TestStringMap() result expected %#v, got %#v ", sm, smret) + } + + s := []int32{1, 2, 42} + sret, err := client.TestSet(defaultCtx, s) + if err != nil { + t.Errorf("Unexpected error in TestSet() call: %s", err) + } + // Sets can be in any order, but Go slices are ordered, so reflect.DeepEqual won't work. + stemp := map[int32]struct{}{} + for _, val := range s { + stemp[val] = struct{}{} + } + for _, val := range sret { + if _, ok := stemp[val]; !ok { + t.Fatalf("Unexpected TestSet() result expected %#v, got %#v ", s, sret) + } + } + + l := []int32{1, 2, 42} + lret, err := client.TestList(defaultCtx, l) + if err != nil { + t.Errorf("Unexpected error in TestList() call: %s", err) + } + if !reflect.DeepEqual(l, lret) { + t.Errorf("Unexpected TestList() result expected %#v, got %#v ", l, lret) + } + + eret, err := client.TestEnum(defaultCtx, thrifttest.Numberz_TWO) + if err != nil { + t.Errorf("Unexpected error in TestEnum() call: %s", err) + } + if eret != thrifttest.Numberz_TWO { + t.Errorf("Unexpected TestEnum() result expected %#v, got %#v ", thrifttest.Numberz_TWO, eret) + } + + tret, err := client.TestTypedef(defaultCtx, thrifttest.UserId(42)) + if err != nil { + t.Errorf("Unexpected error in TestTypedef() call: %s", err) + } + if tret != thrifttest.UserId(42) { + t.Errorf("Unexpected TestTypedef() result expected %#v, got %#v ", thrifttest.UserId(42), tret) + } + + mapmap, err := client.TestMapMap(defaultCtx, 42) + if err != nil { + t.Errorf("Unexpected error in TestMapmap() call: %s", err) + } + if !reflect.DeepEqual(mapmap, rmapmap) { + t.Errorf("Unexpected TestMapmap() result expected %#v, got %#v ", rmapmap, mapmap) + } + + 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: %s", err) + } + if !reflect.DeepEqual(xxs, xxsret) { + t.Errorf("Unexpected TestMulti() result expected %#v, got %#v ", xxs, xxsret) + } + + err = client.TestException(defaultCtx, "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(defaultCtx, "TException") + 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") + 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(defaultCtx, "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(defaultCtx, 2) + if err != nil { + t.Errorf("Unexpected error in TestOneway() call: %s", err) + } + + //Make sure the connection still alive + if err = client.TestVoid(defaultCtx); err != nil { + t.Errorf("Unexpected error in TestVoid() call: %s", err) + } +} diff --git a/src/jaegertracing/thrift/test/go/src/common/context_test.go b/src/jaegertracing/thrift/test/go/src/common/context_test.go new file mode 100644 index 000000000..e64dbb9ad --- /dev/null +++ b/src/jaegertracing/thrift/test/go/src/common/context_test.go @@ -0,0 +1,98 @@ +/* + * 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 ( + "context" + "fmt" + "net" + "net/http" + "net/url" + "os" + "syscall" + "testing" + "thrift" + "time" +) + +type slowHttpHandler struct{} + +func (slowHttpHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { + time.Sleep(1 * time.Second) +} + +func TestHttpContextTimeout(t *testing.T) { + certPath = "../../../keys" + + unit := test_unit{"127.0.0.1", 9096, "", "http", "binary", false} + + server := &http.Server{Addr: unit.host + fmt.Sprintf(":%d", unit.port), Handler: slowHttpHandler{}} + go server.ListenAndServe() + + 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: %v", err) + return + } + defer trans.Close() + + unwrapErr := func(err error) error { + for { + switch err.(type) { + case thrift.TTransportException: + err = err.(thrift.TTransportException).Err() + case *url.Error: + err = err.(*url.Error).Err + case *net.OpError: + err = err.(*net.OpError).Err + case *os.SyscallError: + err = err.(*os.SyscallError).Err + default: + return err + } + } + } + + serverStartupDeadline := time.Now().Add(5 * time.Second) + for { + ctx, _ := context.WithTimeout(context.Background(), 50*time.Millisecond) + err = client.TestVoid(ctx) + err = unwrapErr(err) + if err != syscall.ECONNREFUSED || time.Now().After(serverStartupDeadline) { + break + } + time.Sleep(time.Millisecond) + } + + if err == nil { + t.Errorf("Request completed (should have timed out)") + return + } + + // We've got to switch on `err.Error()` here since go1.7 doesn't properly return + // `context.DeadlineExceeded` error and `http.errRequestCanceled` is not exported. + // See https://github.com/golang/go/issues/17711 + switch err.Error() { + case context.DeadlineExceeded.Error(), "net/http: request canceled": + // Expected error + default: + t.Errorf("Unexpected error: %s", err) + } +} diff --git a/src/jaegertracing/thrift/test/go/src/common/printing_handler.go b/src/jaegertracing/thrift/test/go/src/common/printing_handler.go new file mode 100644 index 000000000..2b22d0c97 --- /dev/null +++ b/src/jaegertracing/thrift/test/go/src/common/printing_handler.go @@ -0,0 +1,384 @@ +/* + * 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 ( + "context" + "encoding/hex" + "errors" + "fmt" + . "gen/thrifttest" + "time" +) + +var PrintingHandler = &printingHandler{} + +type printingHandler struct{} + +// Prints "testVoid()" and returns nothing. +func (p *printingHandler) TestVoid(ctx context.Context) (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(ctx context.Context, thing string) (r string, err error) { + fmt.Printf("testString(\"%s\")\n", thing) + return thing, nil +} + +// Prints 'testBool("%t")' with thing as 'true' or 'false' +// @param bool thing - the bool to print +// @return bool - returns the bool 'thing' +// +// Parameters: +// - Thing +func (p *printingHandler) TestBool(ctx context.Context, thing bool) (r bool, err error) { + fmt.Printf("testBool(%t)\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(ctx context.Context, 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(ctx context.Context, 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(ctx context.Context, 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(ctx context.Context, thing float64) (r float64, err error) { + fmt.Printf("testDouble(%f)\n", thing) + return thing, nil +} + +// Prints 'testBinary("%s")' where '%s' is a hex-formatted string of thing's data +// @param []byte thing - the binary to print +// @return []byte - returns the binary 'thing' +// +// Parameters: +// - Thing +func (p *printingHandler) TestBinary(ctx context.Context, thing []byte) (r []byte, err error) { + fmt.Printf("testBinary(%s)\n", hex.EncodeToString(thing)) + return thing, nil +} + +// Prints 'testStruct("{%s}")' where thing has been formatted into a string of comma separated values +// @param Xtruct thing - the Xtruct to print +// @return Xtruct - returns the Xtruct 'thing' +// +// Parameters: +// - Thing +func (p *printingHandler) TestStruct(ctx context.Context, 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(ctx context.Context, 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 +// separated by commas and new lines +// @param map<i32,i32> thing - the map<i32,i32> to print +// @return map<i32,i32> - returns the map<i32,i32> 'thing' +// +// Parameters: +// - Thing +func (p *printingHandler) TestMap(ctx context.Context, 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 +// separated by commas and new lines +// @param map<string,string> thing - the map<string,string> to print +// @return map<string,string> - returns the map<string,string> 'thing' +// +// Parameters: +// - Thing +func (p *printingHandler) TestStringMap(ctx context.Context, 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 +// separated by commas and new lines +// @param set<i32> thing - the set<i32> to print +// @return set<i32> - returns the set<i32> 'thing' +// +// Parameters: +// - Thing +func (p *printingHandler) TestSet(ctx context.Context, thing []int32) (r []int32, 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 +// separated by commas and new lines +// @param list<i32> thing - the list<i32> to print +// @return list<i32> - returns the list<i32> 'thing' +// +// Parameters: +// - Thing +func (p *printingHandler) TestList(ctx context.Context, 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(ctx context.Context, 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(ctx context.Context, 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<i32,map<i32,i32>> - 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(ctx context.Context, 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 => <empty Insanity struct>, }, +// } +// @return map<UserId, map<Numberz,Insanity>> - a map with the above values +// +// Parameters: +// - Argument +func (p *printingHandler) TestInsanity(ctx context.Context, argument *Insanity) (r map[UserId]map[Numberz]*Insanity, err error) { + fmt.Printf("testInsanity()\n") + r = make(map[UserId]map[Numberz]*Insanity) + r[1] = map[Numberz]*Insanity{ + 2: argument, + 3: argument, + } + r[2] = map[Numberz]*Insanity{ + 6: NewInsanity(), + } + return +} + +// Prints 'testMulti()' +// @param byte arg0 - +// @param i32 arg1 - +// @param i64 arg2 - +// @param map<i16, string> 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(ctx context.Context, 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(ctx context.Context, 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(ctx context.Context, 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(ctx context.Context, 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/src/jaegertracing/thrift/test/go/src/common/server.go b/src/jaegertracing/thrift/test/go/src/common/server.go new file mode 100644 index 000000000..c6674ae75 --- /dev/null +++ b/src/jaegertracing/thrift/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 ( + "compress/zlib" + "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 GetServerParams( + host string, + port int64, + domain_socket string, + transport string, + protocol string, + ssl bool, + certPath string, + handler thrifttest.ThriftTest) (thrift.TProcessor, thrift.TServerTransport, thrift.TTransportFactory, thrift.TProtocolFactory, error) { + + var 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() + case "header": + protocolFactory = thrift.NewTHeaderProtocolFactory() + default: + return nil, nil, nil, 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, nil, nil, 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, nil, nil, nil, err + } + + var transportFactory thrift.TTransportFactory + + switch transport { + case "http": + // there is no such factory, and we don't need any + transportFactory = nil + case "framed": + transportFactory = thrift.NewTTransportFactory() + transportFactory = thrift.NewTFramedTransportFactory(transportFactory) + case "buffered": + transportFactory = thrift.NewTBufferedTransportFactory(8192) + case "zlib": + transportFactory = thrift.NewTZlibTransportFactory(zlib.BestCompression) + case "": + transportFactory = thrift.NewTTransportFactory() + default: + return nil, nil, nil, nil, fmt.Errorf("Invalid transport specified %s", transport) + } + processor := thrifttest.NewThriftTestProcessor(handler) + + return processor, serverTransport, transportFactory, protocolFactory, nil +} diff --git a/src/jaegertracing/thrift/test/go/src/common/simple_handler.go b/src/jaegertracing/thrift/test/go/src/common/simple_handler.go new file mode 100644 index 000000000..0c9463da0 --- /dev/null +++ b/src/jaegertracing/thrift/test/go/src/common/simple_handler.go @@ -0,0 +1,155 @@ +/* + * 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) TestBool(thing []byte) (r []byte, 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) TestBinary(thing []byte) (r []byte, 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 []int32) (r []int32, 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 +} |