diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 18:45:59 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 18:45:59 +0000 |
commit | 19fcec84d8d7d21e796c7624e521b60d28ee21ed (patch) | |
tree | 42d26aa27d1e3f7c0b8bd3fd14e7d7082f5008dc /src/jaegertracing/thrift/lib/go/test/tests | |
parent | Initial commit. (diff) | |
download | ceph-upstream.tar.xz ceph-upstream.zip |
Adding upstream version 16.2.11+ds.upstream/16.2.11+dsupstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'src/jaegertracing/thrift/lib/go/test/tests')
19 files changed, 3059 insertions, 0 deletions
diff --git a/src/jaegertracing/thrift/lib/go/test/tests/binary_key_test.go b/src/jaegertracing/thrift/lib/go/test/tests/binary_key_test.go new file mode 100644 index 000000000..aa9619391 --- /dev/null +++ b/src/jaegertracing/thrift/lib/go/test/tests/binary_key_test.go @@ -0,0 +1,31 @@ +/* + * 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 tests + +import ( + "binarykeytest" + "testing" +) + +func TestBinaryMapKeyGeneratesString(t *testing.T) { + s := binarykeytest.NewTestStruct() + //This will only compile if BinToString has type of map[string]string + s.BinToString = make(map[string]string) +} diff --git a/src/jaegertracing/thrift/lib/go/test/tests/client_error_test.go b/src/jaegertracing/thrift/lib/go/test/tests/client_error_test.go new file mode 100644 index 000000000..fdec4ea57 --- /dev/null +++ b/src/jaegertracing/thrift/lib/go/test/tests/client_error_test.go @@ -0,0 +1,873 @@ +/* + * 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 tests + +import ( + "context" + "errors" + "errortest" + "testing" + "thrift" + + "github.com/golang/mock/gomock" +) + +// TestCase: Comprehensive call and reply workflow in the client. +// Setup mock to fail at a certain position. Return true if position exists otherwise false. +func prepareClientCallReply(protocol *MockTProtocol, failAt int, failWith error) bool { + var err error = nil + + if failAt == 0 { + err = failWith + } + last := protocol.EXPECT().WriteMessageBegin("testStruct", thrift.CALL, int32(1)).Return(err) + if failAt == 0 { + return true + } + if failAt == 1 { + err = failWith + } + last = protocol.EXPECT().WriteStructBegin("testStruct_args").Return(err).After(last) + if failAt == 1 { + return true + } + if failAt == 2 { + err = failWith + } + last = protocol.EXPECT().WriteFieldBegin("thing", thrift.TType(thrift.STRUCT), int16(1)).Return(err).After(last) + if failAt == 2 { + return true + } + if failAt == 3 { + err = failWith + } + last = protocol.EXPECT().WriteStructBegin("TestStruct").Return(err).After(last) + if failAt == 3 { + return true + } + if failAt == 4 { + err = failWith + } + last = protocol.EXPECT().WriteFieldBegin("m", thrift.TType(thrift.MAP), int16(1)).Return(err).After(last) + if failAt == 4 { + return true + } + if failAt == 5 { + err = failWith + } + last = protocol.EXPECT().WriteMapBegin(thrift.TType(thrift.STRING), thrift.TType(thrift.STRING), 0).Return(err).After(last) + if failAt == 5 { + return true + } + if failAt == 6 { + err = failWith + } + last = protocol.EXPECT().WriteMapEnd().Return(err).After(last) + if failAt == 6 { + return true + } + if failAt == 7 { + err = failWith + } + last = protocol.EXPECT().WriteFieldEnd().Return(err).After(last) + if failAt == 7 { + return true + } + if failAt == 8 { + err = failWith + } + last = protocol.EXPECT().WriteFieldBegin("l", thrift.TType(thrift.LIST), int16(2)).Return(err).After(last) + if failAt == 8 { + return true + } + if failAt == 9 { + err = failWith + } + last = protocol.EXPECT().WriteListBegin(thrift.TType(thrift.STRING), 0).Return(err).After(last) + if failAt == 9 { + return true + } + if failAt == 10 { + err = failWith + } + last = protocol.EXPECT().WriteListEnd().Return(err).After(last) + if failAt == 10 { + return true + } + if failAt == 11 { + err = failWith + } + last = protocol.EXPECT().WriteFieldEnd().Return(err).After(last) + if failAt == 11 { + return true + } + if failAt == 12 { + err = failWith + } + + last = protocol.EXPECT().WriteFieldBegin("s", thrift.TType(thrift.SET), int16(3)).Return(err).After(last) + if failAt == 12 { + return true + } + if failAt == 13 { + err = failWith + } + last = protocol.EXPECT().WriteSetBegin(thrift.TType(thrift.STRING), 0).Return(err).After(last) + if failAt == 13 { + return true + } + if failAt == 14 { + err = failWith + } + last = protocol.EXPECT().WriteSetEnd().Return(err).After(last) + if failAt == 14 { + return true + } + if failAt == 15 { + err = failWith + } + last = protocol.EXPECT().WriteFieldEnd().Return(err).After(last) + if failAt == 15 { + return true + } + if failAt == 16 { + err = failWith + } + last = protocol.EXPECT().WriteFieldBegin("i", thrift.TType(thrift.I32), int16(4)).Return(err).After(last) + if failAt == 16 { + return true + } + if failAt == 17 { + err = failWith + } + last = protocol.EXPECT().WriteI32(int32(3)).Return(err).After(last) + if failAt == 17 { + return true + } + if failAt == 18 { + err = failWith + } + last = protocol.EXPECT().WriteFieldEnd().Return(err).After(last) + if failAt == 18 { + return true + } + if failAt == 19 { + err = failWith + } + last = protocol.EXPECT().WriteFieldStop().Return(err).After(last) + if failAt == 19 { + return true + } + if failAt == 20 { + err = failWith + } + last = protocol.EXPECT().WriteStructEnd().Return(err).After(last) + if failAt == 20 { + return true + } + if failAt == 21 { + err = failWith + } + last = protocol.EXPECT().WriteFieldEnd().Return(err).After(last) + if failAt == 21 { + return true + } + if failAt == 22 { + err = failWith + } + last = protocol.EXPECT().WriteFieldStop().Return(err).After(last) + if failAt == 22 { + return true + } + if failAt == 23 { + err = failWith + } + last = protocol.EXPECT().WriteStructEnd().Return(err).After(last) + if failAt == 23 { + return true + } + if failAt == 24 { + err = failWith + } + last = protocol.EXPECT().WriteMessageEnd().Return(err).After(last) + if failAt == 24 { + return true + } + if failAt == 25 { + err = failWith + } + last = protocol.EXPECT().Flush(context.Background()).Return(err).After(last) + if failAt == 25 { + return true + } + if failAt == 26 { + err = failWith + } + last = protocol.EXPECT().ReadMessageBegin().Return("testStruct", thrift.REPLY, int32(1), err).After(last) + if failAt == 26 { + return true + } + if failAt == 27 { + err = failWith + } + last = protocol.EXPECT().ReadStructBegin().Return("testStruct_args", err).After(last) + if failAt == 27 { + return true + } + if failAt == 28 { + err = failWith + } + last = protocol.EXPECT().ReadFieldBegin().Return("_", thrift.TType(thrift.STRUCT), int16(0), err).After(last) + if failAt == 28 { + return true + } + if failAt == 29 { + err = failWith + } + last = protocol.EXPECT().ReadStructBegin().Return("TestStruct", err).After(last) + if failAt == 29 { + return true + } + if failAt == 30 { + err = failWith + } + last = protocol.EXPECT().ReadFieldBegin().Return("m", thrift.TType(thrift.MAP), int16(1), err).After(last) + if failAt == 30 { + return true + } + if failAt == 31 { + err = failWith + } + last = protocol.EXPECT().ReadMapBegin().Return(thrift.TType(thrift.STRING), thrift.TType(thrift.STRING), 0, err).After(last) + if failAt == 31 { + return true + } + if failAt == 32 { + err = failWith + } + last = protocol.EXPECT().ReadMapEnd().Return(err).After(last) + if failAt == 32 { + return true + } + if failAt == 33 { + err = failWith + } + last = protocol.EXPECT().ReadFieldEnd().Return(err).After(last) + if failAt == 33 { + return true + } + if failAt == 34 { + err = failWith + } + last = protocol.EXPECT().ReadFieldBegin().Return("l", thrift.TType(thrift.LIST), int16(2), err).After(last) + if failAt == 34 { + return true + } + if failAt == 35 { + err = failWith + } + last = protocol.EXPECT().ReadListBegin().Return(thrift.TType(thrift.STRING), 0, err).After(last) + if failAt == 35 { + return true + } + if failAt == 36 { + err = failWith + } + last = protocol.EXPECT().ReadListEnd().Return(err).After(last) + if failAt == 36 { + return true + } + if failAt == 37 { + err = failWith + } + last = protocol.EXPECT().ReadFieldEnd().Return(err).After(last) + if failAt == 37 { + return true + } + if failAt == 38 { + err = failWith + } + last = protocol.EXPECT().ReadFieldBegin().Return("s", thrift.TType(thrift.SET), int16(3), err).After(last) + if failAt == 38 { + return true + } + if failAt == 39 { + err = failWith + } + last = protocol.EXPECT().ReadSetBegin().Return(thrift.TType(thrift.STRING), 0, err).After(last) + if failAt == 39 { + return true + } + if failAt == 40 { + err = failWith + } + last = protocol.EXPECT().ReadSetEnd().Return(err).After(last) + if failAt == 40 { + return true + } + if failAt == 41 { + err = failWith + } + last = protocol.EXPECT().ReadFieldEnd().Return(err).After(last) + if failAt == 41 { + return true + } + if failAt == 42 { + err = failWith + } + last = protocol.EXPECT().ReadFieldBegin().Return("i", thrift.TType(thrift.I32), int16(4), err).After(last) + if failAt == 42 { + return true + } + if failAt == 43 { + err = failWith + } + last = protocol.EXPECT().ReadI32().Return(int32(3), err).After(last) + if failAt == 43 { + return true + } + if failAt == 44 { + err = failWith + } + last = protocol.EXPECT().ReadFieldEnd().Return(err).After(last) + if failAt == 44 { + return true + } + if failAt == 45 { + err = failWith + } + last = protocol.EXPECT().ReadFieldBegin().Return("_", thrift.TType(thrift.STOP), int16(5), err).After(last) + if failAt == 45 { + return true + } + if failAt == 46 { + err = failWith + } + last = protocol.EXPECT().ReadStructEnd().Return(err).After(last) + if failAt == 46 { + return true + } + if failAt == 47 { + err = failWith + } + last = protocol.EXPECT().ReadFieldEnd().Return(err).After(last) + if failAt == 47 { + return true + } + if failAt == 48 { + err = failWith + } + last = protocol.EXPECT().ReadFieldBegin().Return("_", thrift.TType(thrift.STOP), int16(1), err).After(last) + if failAt == 48 { + return true + } + if failAt == 49 { + err = failWith + } + last = protocol.EXPECT().ReadStructEnd().Return(err).After(last) + if failAt == 49 { + return true + } + if failAt == 50 { + err = failWith + } + last = protocol.EXPECT().ReadMessageEnd().Return(err).After(last) + if failAt == 50 { + return true + } + return false +} + +// TestCase: Comprehensive call and reply workflow in the client. +// Expecting TTransportError on fail. +func TestClientReportTTransportErrors(t *testing.T) { + mockCtrl := gomock.NewController(t) + + thing := errortest.NewTestStruct() + thing.M = make(map[string]string) + thing.L = make([]string, 0) + thing.S = make([]string, 0) + thing.I = 3 + + err := thrift.NewTTransportException(thrift.TIMED_OUT, "test") + for i := 0; ; i++ { + protocol := NewMockTProtocol(mockCtrl) + if !prepareClientCallReply(protocol, i, err) { + return + } + client := errortest.NewErrorTestClient(thrift.NewTStandardClient(protocol, protocol)) + _, retErr := client.TestStruct(defaultCtx, thing) + mockCtrl.Finish() + mockCtrl = gomock.NewController(t) + err2, ok := retErr.(thrift.TTransportException) + if !ok { + t.Fatal("Expected a TTrasportException") + } + + if err2.TypeId() != thrift.TIMED_OUT { + t.Fatal("Expected TIMED_OUT error") + } + } +} + +// TestCase: Comprehensive call and reply workflow in the client. +// Expecting TTransportError on fail. +// Similar to TestClientReportTTransportErrors, but using legacy client constructor. +func TestClientReportTTransportErrorsLegacy(t *testing.T) { + mockCtrl := gomock.NewController(t) + transport := thrift.NewTMemoryBuffer() + thing := errortest.NewTestStruct() + thing.M = make(map[string]string) + thing.L = make([]string, 0) + thing.S = make([]string, 0) + thing.I = 3 + + err := thrift.NewTTransportException(thrift.TIMED_OUT, "test") + for i := 0; ; i++ { + protocol := NewMockTProtocol(mockCtrl) + if !prepareClientCallReply(protocol, i, err) { + return + } + client := errortest.NewErrorTestClientProtocol(transport, protocol, protocol) + _, retErr := client.TestStruct(defaultCtx, thing) + mockCtrl.Finish() + mockCtrl = gomock.NewController(t) + err2, ok := retErr.(thrift.TTransportException) + if !ok { + t.Fatal("Expected a TTrasportException") + } + + if err2.TypeId() != thrift.TIMED_OUT { + t.Fatal("Expected TIMED_OUT error") + } + } +} + +// TestCase: Comprehensive call and reply workflow in the client. +// Expecting TTProtocolErrors on fail. +func TestClientReportTProtocolErrors(t *testing.T) { + mockCtrl := gomock.NewController(t) + + thing := errortest.NewTestStruct() + thing.M = make(map[string]string) + thing.L = make([]string, 0) + thing.S = make([]string, 0) + thing.I = 3 + + err := thrift.NewTProtocolExceptionWithType(thrift.INVALID_DATA, errors.New("test")) + for i := 0; ; i++ { + protocol := NewMockTProtocol(mockCtrl) + if !prepareClientCallReply(protocol, i, err) { + return + } + client := errortest.NewErrorTestClient(thrift.NewTStandardClient(protocol, protocol)) + _, retErr := client.TestStruct(defaultCtx, thing) + mockCtrl.Finish() + mockCtrl = gomock.NewController(t) + err2, ok := retErr.(thrift.TProtocolException) + if !ok { + t.Fatal("Expected a TProtocolException") + } + if err2.TypeId() != thrift.INVALID_DATA { + t.Fatal("Expected INVALID_DATA error") + } + } +} + +// TestCase: Comprehensive call and reply workflow in the client. +// Expecting TTProtocolErrors on fail. +// Similar to TestClientReportTProtocolErrors, but using legacy client constructor. +func TestClientReportTProtocolErrorsLegacy(t *testing.T) { + mockCtrl := gomock.NewController(t) + transport := thrift.NewTMemoryBuffer() + thing := errortest.NewTestStruct() + thing.M = make(map[string]string) + thing.L = make([]string, 0) + thing.S = make([]string, 0) + thing.I = 3 + + err := thrift.NewTProtocolExceptionWithType(thrift.INVALID_DATA, errors.New("test")) + for i := 0; ; i++ { + protocol := NewMockTProtocol(mockCtrl) + if !prepareClientCallReply(protocol, i, err) { + return + } + client := errortest.NewErrorTestClientProtocol(transport, protocol, protocol) + _, retErr := client.TestStruct(defaultCtx, thing) + mockCtrl.Finish() + mockCtrl = gomock.NewController(t) + err2, ok := retErr.(thrift.TProtocolException) + if !ok { + t.Fatal("Expected a TProtocolException") + } + if err2.TypeId() != thrift.INVALID_DATA { + t.Fatal("Expected INVALID_DATA error") + } + } +} + +// TestCase: call and reply with exception workflow in the client. +// Setup mock to fail at a certain position. Return true if position exists otherwise false. +func prepareClientCallException(protocol *MockTProtocol, failAt int, failWith error) bool { + var err error = nil + + // No need to test failure in this block, because it is covered in other test cases + last := protocol.EXPECT().WriteMessageBegin("testString", thrift.CALL, int32(1)) + last = protocol.EXPECT().WriteStructBegin("testString_args").After(last) + last = protocol.EXPECT().WriteFieldBegin("s", thrift.TType(thrift.STRING), int16(1)).After(last) + last = protocol.EXPECT().WriteString("test").After(last) + last = protocol.EXPECT().WriteFieldEnd().After(last) + last = protocol.EXPECT().WriteFieldStop().After(last) + last = protocol.EXPECT().WriteStructEnd().After(last) + last = protocol.EXPECT().WriteMessageEnd().After(last) + last = protocol.EXPECT().Flush(context.Background()).After(last) + + // Reading the exception, might fail. + if failAt == 0 { + err = failWith + } + last = protocol.EXPECT().ReadMessageBegin().Return("testString", thrift.EXCEPTION, int32(1), err).After(last) + if failAt == 0 { + return true + } + if failAt == 1 { + err = failWith + } + last = protocol.EXPECT().ReadStructBegin().Return("TApplicationException", err).After(last) + if failAt == 1 { + return true + } + if failAt == 2 { + err = failWith + } + last = protocol.EXPECT().ReadFieldBegin().Return("message", thrift.TType(thrift.STRING), int16(1), err).After(last) + if failAt == 2 { + return true + } + if failAt == 3 { + err = failWith + } + last = protocol.EXPECT().ReadString().Return("test", err).After(last) + if failAt == 3 { + return true + } + if failAt == 4 { + err = failWith + } + last = protocol.EXPECT().ReadFieldEnd().Return(err).After(last) + if failAt == 4 { + return true + } + if failAt == 5 { + err = failWith + } + last = protocol.EXPECT().ReadFieldBegin().Return("type", thrift.TType(thrift.I32), int16(2), err).After(last) + if failAt == 5 { + return true + } + if failAt == 6 { + err = failWith + } + last = protocol.EXPECT().ReadI32().Return(int32(thrift.PROTOCOL_ERROR), err).After(last) + if failAt == 6 { + return true + } + if failAt == 7 { + err = failWith + } + last = protocol.EXPECT().ReadFieldEnd().Return(err).After(last) + if failAt == 7 { + return true + } + if failAt == 8 { + err = failWith + } + last = protocol.EXPECT().ReadFieldBegin().Return("_", thrift.TType(thrift.STOP), int16(2), err).After(last) + if failAt == 8 { + return true + } + if failAt == 9 { + err = failWith + } + last = protocol.EXPECT().ReadStructEnd().Return(err).After(last) + if failAt == 9 { + return true + } + if failAt == 10 { + err = failWith + } + last = protocol.EXPECT().ReadMessageEnd().Return(err).After(last) + if failAt == 10 { + return true + } + + return false +} + +// TestCase: call and reply with exception workflow in the client. +func TestClientCallException(t *testing.T) { + mockCtrl := gomock.NewController(t) + + err := thrift.NewTTransportException(thrift.TIMED_OUT, "test") + for i := 0; ; i++ { + protocol := NewMockTProtocol(mockCtrl) + willComplete := !prepareClientCallException(protocol, i, err) + + client := errortest.NewErrorTestClient(thrift.NewTStandardClient(protocol, protocol)) + _, retErr := client.TestString(defaultCtx, "test") + mockCtrl.Finish() + mockCtrl = gomock.NewController(t) + + if !willComplete { + err2, ok := retErr.(thrift.TTransportException) + if !ok { + t.Fatal("Expected a TTransportException") + } + if err2.TypeId() != thrift.TIMED_OUT { + t.Fatal("Expected TIMED_OUT error") + } + } else { + err2, ok := retErr.(thrift.TApplicationException) + if !ok { + t.Fatal("Expected a TApplicationException") + } + if err2.TypeId() != thrift.PROTOCOL_ERROR { + t.Fatal("Expected PROTOCOL_ERROR error") + } + break + } + } +} + +// TestCase: call and reply with exception workflow in the client. +// Similar to TestClientCallException, but using legacy client constructor. +func TestClientCallExceptionLegacy(t *testing.T) { + mockCtrl := gomock.NewController(t) + transport := thrift.NewTMemoryBuffer() + err := thrift.NewTTransportException(thrift.TIMED_OUT, "test") + for i := 0; ; i++ { + protocol := NewMockTProtocol(mockCtrl) + willComplete := !prepareClientCallException(protocol, i, err) + + client := errortest.NewErrorTestClientProtocol(transport, protocol, protocol) + _, retErr := client.TestString(defaultCtx, "test") + mockCtrl.Finish() + mockCtrl = gomock.NewController(t) + + if !willComplete { + err2, ok := retErr.(thrift.TTransportException) + if !ok { + t.Fatal("Expected a TTransportException") + } + if err2.TypeId() != thrift.TIMED_OUT { + t.Fatal("Expected TIMED_OUT error") + } + } else { + err2, ok := retErr.(thrift.TApplicationException) + if !ok { + t.Fatal("Expected a TApplicationException") + } + if err2.TypeId() != thrift.PROTOCOL_ERROR { + t.Fatal("Expected PROTOCOL_ERROR error") + } + break + } + } +} + +// TestCase: Mismatching sequence id has been received in the client. +func TestClientSeqIdMismatch(t *testing.T) { + mockCtrl := gomock.NewController(t) + protocol := NewMockTProtocol(mockCtrl) + gomock.InOrder( + protocol.EXPECT().WriteMessageBegin("testString", thrift.CALL, int32(1)), + protocol.EXPECT().WriteStructBegin("testString_args"), + protocol.EXPECT().WriteFieldBegin("s", thrift.TType(thrift.STRING), int16(1)), + protocol.EXPECT().WriteString("test"), + protocol.EXPECT().WriteFieldEnd(), + protocol.EXPECT().WriteFieldStop(), + protocol.EXPECT().WriteStructEnd(), + protocol.EXPECT().WriteMessageEnd(), + protocol.EXPECT().Flush(context.Background()), + protocol.EXPECT().ReadMessageBegin().Return("testString", thrift.REPLY, int32(2), nil), + ) + + client := errortest.NewErrorTestClient(thrift.NewTStandardClient(protocol, protocol)) + _, err := client.TestString(defaultCtx, "test") + mockCtrl.Finish() + appErr, ok := err.(thrift.TApplicationException) + if !ok { + t.Fatal("Expected TApplicationException") + } + if appErr.TypeId() != thrift.BAD_SEQUENCE_ID { + t.Fatal("Expected BAD_SEQUENCE_ID error") + } +} + +// TestCase: Mismatching sequence id has been received in the client. +// Similar to TestClientSeqIdMismatch, but using legacy client constructor. +func TestClientSeqIdMismatchLegeacy(t *testing.T) { + mockCtrl := gomock.NewController(t) + transport := thrift.NewTMemoryBuffer() + protocol := NewMockTProtocol(mockCtrl) + gomock.InOrder( + protocol.EXPECT().WriteMessageBegin("testString", thrift.CALL, int32(1)), + protocol.EXPECT().WriteStructBegin("testString_args"), + protocol.EXPECT().WriteFieldBegin("s", thrift.TType(thrift.STRING), int16(1)), + protocol.EXPECT().WriteString("test"), + protocol.EXPECT().WriteFieldEnd(), + protocol.EXPECT().WriteFieldStop(), + protocol.EXPECT().WriteStructEnd(), + protocol.EXPECT().WriteMessageEnd(), + protocol.EXPECT().Flush(context.Background()), + protocol.EXPECT().ReadMessageBegin().Return("testString", thrift.REPLY, int32(2), nil), + ) + + client := errortest.NewErrorTestClientProtocol(transport, protocol, protocol) + _, err := client.TestString(defaultCtx, "test") + mockCtrl.Finish() + appErr, ok := err.(thrift.TApplicationException) + if !ok { + t.Fatal("Expected TApplicationException") + } + if appErr.TypeId() != thrift.BAD_SEQUENCE_ID { + t.Fatal("Expected BAD_SEQUENCE_ID error") + } +} + +// TestCase: Wrong method name has been received in the client. +func TestClientWrongMethodName(t *testing.T) { + mockCtrl := gomock.NewController(t) + protocol := NewMockTProtocol(mockCtrl) + gomock.InOrder( + protocol.EXPECT().WriteMessageBegin("testString", thrift.CALL, int32(1)), + protocol.EXPECT().WriteStructBegin("testString_args"), + protocol.EXPECT().WriteFieldBegin("s", thrift.TType(thrift.STRING), int16(1)), + protocol.EXPECT().WriteString("test"), + protocol.EXPECT().WriteFieldEnd(), + protocol.EXPECT().WriteFieldStop(), + protocol.EXPECT().WriteStructEnd(), + protocol.EXPECT().WriteMessageEnd(), + protocol.EXPECT().Flush(context.Background()), + protocol.EXPECT().ReadMessageBegin().Return("unknown", thrift.REPLY, int32(1), nil), + ) + + client := errortest.NewErrorTestClient(thrift.NewTStandardClient(protocol, protocol)) + _, err := client.TestString(defaultCtx, "test") + mockCtrl.Finish() + appErr, ok := err.(thrift.TApplicationException) + if !ok { + t.Fatal("Expected TApplicationException") + } + if appErr.TypeId() != thrift.WRONG_METHOD_NAME { + t.Fatal("Expected WRONG_METHOD_NAME error") + } +} + +// TestCase: Wrong method name has been received in the client. +// Similar to TestClientWrongMethodName, but using legacy client constructor. +func TestClientWrongMethodNameLegacy(t *testing.T) { + mockCtrl := gomock.NewController(t) + transport := thrift.NewTMemoryBuffer() + protocol := NewMockTProtocol(mockCtrl) + gomock.InOrder( + protocol.EXPECT().WriteMessageBegin("testString", thrift.CALL, int32(1)), + protocol.EXPECT().WriteStructBegin("testString_args"), + protocol.EXPECT().WriteFieldBegin("s", thrift.TType(thrift.STRING), int16(1)), + protocol.EXPECT().WriteString("test"), + protocol.EXPECT().WriteFieldEnd(), + protocol.EXPECT().WriteFieldStop(), + protocol.EXPECT().WriteStructEnd(), + protocol.EXPECT().WriteMessageEnd(), + protocol.EXPECT().Flush(context.Background()), + protocol.EXPECT().ReadMessageBegin().Return("unknown", thrift.REPLY, int32(1), nil), + ) + + client := errortest.NewErrorTestClientProtocol(transport, protocol, protocol) + _, err := client.TestString(defaultCtx, "test") + mockCtrl.Finish() + appErr, ok := err.(thrift.TApplicationException) + if !ok { + t.Fatal("Expected TApplicationException") + } + if appErr.TypeId() != thrift.WRONG_METHOD_NAME { + t.Fatal("Expected WRONG_METHOD_NAME error") + } +} + +// TestCase: Wrong message type has been received in the client. +func TestClientWrongMessageType(t *testing.T) { + mockCtrl := gomock.NewController(t) + protocol := NewMockTProtocol(mockCtrl) + gomock.InOrder( + protocol.EXPECT().WriteMessageBegin("testString", thrift.CALL, int32(1)), + protocol.EXPECT().WriteStructBegin("testString_args"), + protocol.EXPECT().WriteFieldBegin("s", thrift.TType(thrift.STRING), int16(1)), + protocol.EXPECT().WriteString("test"), + protocol.EXPECT().WriteFieldEnd(), + protocol.EXPECT().WriteFieldStop(), + protocol.EXPECT().WriteStructEnd(), + protocol.EXPECT().WriteMessageEnd(), + protocol.EXPECT().Flush(context.Background()), + protocol.EXPECT().ReadMessageBegin().Return("testString", thrift.INVALID_TMESSAGE_TYPE, int32(1), nil), + ) + + client := errortest.NewErrorTestClient(thrift.NewTStandardClient(protocol, protocol)) + _, err := client.TestString(defaultCtx, "test") + mockCtrl.Finish() + appErr, ok := err.(thrift.TApplicationException) + if !ok { + t.Fatal("Expected TApplicationException") + } + if appErr.TypeId() != thrift.INVALID_MESSAGE_TYPE_EXCEPTION { + t.Fatal("Expected INVALID_MESSAGE_TYPE_EXCEPTION error") + } +} + +// TestCase: Wrong message type has been received in the client. +// Similar to TestClientWrongMessageType, but using legacy client constructor. +func TestClientWrongMessageTypeLegacy(t *testing.T) { + mockCtrl := gomock.NewController(t) + transport := thrift.NewTMemoryBuffer() + protocol := NewMockTProtocol(mockCtrl) + gomock.InOrder( + protocol.EXPECT().WriteMessageBegin("testString", thrift.CALL, int32(1)), + protocol.EXPECT().WriteStructBegin("testString_args"), + protocol.EXPECT().WriteFieldBegin("s", thrift.TType(thrift.STRING), int16(1)), + protocol.EXPECT().WriteString("test"), + protocol.EXPECT().WriteFieldEnd(), + protocol.EXPECT().WriteFieldStop(), + protocol.EXPECT().WriteStructEnd(), + protocol.EXPECT().WriteMessageEnd(), + protocol.EXPECT().Flush(context.Background()), + protocol.EXPECT().ReadMessageBegin().Return("testString", thrift.INVALID_TMESSAGE_TYPE, int32(1), nil), + ) + + client := errortest.NewErrorTestClientProtocol(transport, protocol, protocol) + _, err := client.TestString(defaultCtx, "test") + mockCtrl.Finish() + appErr, ok := err.(thrift.TApplicationException) + if !ok { + t.Fatal("Expected TApplicationException") + } + if appErr.TypeId() != thrift.INVALID_MESSAGE_TYPE_EXCEPTION { + t.Fatal("Expected INVALID_MESSAGE_TYPE_EXCEPTION error") + } +} diff --git a/src/jaegertracing/thrift/lib/go/test/tests/context.go b/src/jaegertracing/thrift/lib/go/test/tests/context.go new file mode 100644 index 000000000..a93a82b8f --- /dev/null +++ b/src/jaegertracing/thrift/lib/go/test/tests/context.go @@ -0,0 +1,26 @@ +/* + * 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 tests + +import ( + "context" +) + +var defaultCtx = context.Background() diff --git a/src/jaegertracing/thrift/lib/go/test/tests/encoding_json_test.go b/src/jaegertracing/thrift/lib/go/test/tests/encoding_json_test.go new file mode 100644 index 000000000..12d4566fb --- /dev/null +++ b/src/jaegertracing/thrift/lib/go/test/tests/encoding_json_test.go @@ -0,0 +1,79 @@ +/* + * 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 tests + +import ( + "encoding" + "encoding/json" + "testing" + "thrifttest" +) + +func TestEnumIsTextMarshaller(t *testing.T) { + one := thrifttest.Numberz_ONE + var tm encoding.TextMarshaler = one + b, err := tm.MarshalText() + if err != nil { + t.Fatalf("Unexpected error from MarshalText: %s", err) + } + if string(b) != one.String() { + t.Errorf("MarshalText(%s) = %s, expected = %s", one, b, one) + } +} + +func TestEnumIsTextUnmarshaller(t *testing.T) { + var tm encoding.TextUnmarshaler = thrifttest.NumberzPtr(thrifttest.Numberz_TWO) + err := tm.UnmarshalText([]byte("TWO")) + if err != nil { + t.Fatalf("Unexpected error from UnmarshalText(TWO): %s", err) + } + if *(tm.(*thrifttest.Numberz)) != thrifttest.Numberz_TWO { + t.Errorf("UnmarshalText(TWO) = %s", tm) + } + + err = tm.UnmarshalText([]byte("NAN")) + if err == nil { + t.Errorf("Error from UnmarshalText(NAN)") + } +} + +func TestJSONMarshalUnmarshal(t *testing.T) { + s1 := thrifttest.StructB{ + Aa: &thrifttest.StructA{S: "Aa"}, + Ab: &thrifttest.StructA{S: "Ab"}, + } + + b, err := json.Marshal(s1) + if err != nil { + t.Fatalf("Unexpected error from json.Marshal: %s", err) + } + + s2 := thrifttest.StructB{} + err = json.Unmarshal(b, &s2) + if err != nil { + t.Fatalf("Unexpected error from json.Unmarshal: %s", err) + } + + if *s1.Aa != *s2.Aa || *s1.Ab != *s2.Ab { + t.Logf("s1 = %+v", s1) + t.Logf("s2 = %+v", s2) + t.Errorf("json: Unmarshal(Marshal(s)) != s") + } +} diff --git a/src/jaegertracing/thrift/lib/go/test/tests/gotag_test.go b/src/jaegertracing/thrift/lib/go/test/tests/gotag_test.go new file mode 100644 index 000000000..ff2f14ecf --- /dev/null +++ b/src/jaegertracing/thrift/lib/go/test/tests/gotag_test.go @@ -0,0 +1,62 @@ +/* + * 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 tests + +import ( + "gotagtest" + "reflect" + "testing" +) + +func TestDefaultTag(t *testing.T) { + s := gotagtest.Tagged{} + st := reflect.TypeOf(s) + field, ok := st.FieldByName("StringThing") + if !ok || field.Tag.Get("json") != "string_thing" { + t.Error("Unexpected default tag value") + } +} + +func TestCustomTag(t *testing.T) { + s := gotagtest.Tagged{} + st := reflect.TypeOf(s) + field, ok := st.FieldByName("IntThing") + if !ok || field.Tag.Get("json") != "int_thing,string" { + t.Error("Unexpected custom tag value") + } +} + +func TestOptionalTag(t *testing.T) { + s := gotagtest.Tagged{} + st := reflect.TypeOf(s) + field, ok := st.FieldByName("OptionalIntThing") + if !ok || field.Tag.Get("json") != "optional_int_thing,omitempty" { + t.Error("Unexpected default tag value for optional field") + } +} + +func TestOptionalTagWithDefaultValue(t *testing.T) { + s := gotagtest.Tagged{} + st := reflect.TypeOf(s) + field, ok := st.FieldByName("OptionalBoolThing") + if !ok || field.Tag.Get("json") != "optional_bool_thing" { + t.Error("Unexpected default tag value for optional field that has a default value") + } +} diff --git a/src/jaegertracing/thrift/lib/go/test/tests/ignoreinitialisms_test.go b/src/jaegertracing/thrift/lib/go/test/tests/ignoreinitialisms_test.go new file mode 100644 index 000000000..3cd5f6509 --- /dev/null +++ b/src/jaegertracing/thrift/lib/go/test/tests/ignoreinitialisms_test.go @@ -0,0 +1,51 @@ +/* + * 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 tests + +import ( + "ignoreinitialismstest" + "reflect" + "testing" +) + +func TestIgnoreInitialismsFlagIsHonoured(t *testing.T) { + s := ignoreinitialismstest.IgnoreInitialismsTest{} + st := reflect.TypeOf(s) + _, ok := st.FieldByName("Id") + if !ok { + t.Error("Id attribute is missing!") + } + _, ok = st.FieldByName("MyId") + if !ok { + t.Error("MyId attribute is missing!") + } + _, ok = st.FieldByName("NumCpu") + if !ok { + t.Error("NumCpu attribute is missing!") + } + _, ok = st.FieldByName("NumGpu") + if !ok { + t.Error("NumGpu attribute is missing!") + } + _, ok = st.FieldByName("My_ID") + if !ok { + t.Error("My_ID attribute is missing!") + } +} diff --git a/src/jaegertracing/thrift/lib/go/test/tests/initialisms_test.go b/src/jaegertracing/thrift/lib/go/test/tests/initialisms_test.go new file mode 100644 index 000000000..40923d24c --- /dev/null +++ b/src/jaegertracing/thrift/lib/go/test/tests/initialisms_test.go @@ -0,0 +1,43 @@ +/* + * 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 tests + +import ( + "initialismstest" + "reflect" + "testing" +) + +func TestThatCommonInitialismsAreFixed(t *testing.T) { + s := initialismstest.InitialismsTest{} + st := reflect.TypeOf(s) + _, ok := st.FieldByName("UserID") + if !ok { + t.Error("UserID attribute is missing!") + } + _, ok = st.FieldByName("ServerURL") + if !ok { + t.Error("ServerURL attribute is missing!") + } + _, ok = st.FieldByName("ID") + if !ok { + t.Error("ID attribute is missing!") + } +} diff --git a/src/jaegertracing/thrift/lib/go/test/tests/multiplexed_protocol_test.go b/src/jaegertracing/thrift/lib/go/test/tests/multiplexed_protocol_test.go new file mode 100644 index 000000000..61ac62828 --- /dev/null +++ b/src/jaegertracing/thrift/lib/go/test/tests/multiplexed_protocol_test.go @@ -0,0 +1,197 @@ +/* + * 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 tests + +import ( + "context" + "multiplexedprotocoltest" + "net" + "testing" + "thrift" + "time" +) + +func FindAvailableTCPServerPort() net.Addr { + if l, err := net.Listen("tcp", "127.0.0.1:0"); err != nil { + panic("Could not find available server port") + } else { + defer l.Close() + return l.Addr() + } +} + +type FirstImpl struct{} + +func (f *FirstImpl) ReturnOne(ctx context.Context) (r int64, err error) { + return 1, nil +} + +type SecondImpl struct{} + +func (s *SecondImpl) ReturnTwo(ctx context.Context) (r int64, err error) { + return 2, nil +} + +func createTransport(addr net.Addr) (thrift.TTransport, error) { + socket := thrift.NewTSocketFromAddrTimeout(addr, TIMEOUT) + transport := thrift.NewTFramedTransport(socket) + err := transport.Open() + if err != nil { + return nil, err + } + return transport, nil +} + +func TestMultiplexedProtocolFirst(t *testing.T) { + processor := thrift.NewTMultiplexedProcessor() + protocolFactory := thrift.NewTBinaryProtocolFactoryDefault() + transportFactory := thrift.NewTTransportFactory() + transportFactory = thrift.NewTFramedTransportFactory(transportFactory) + addr := FindAvailableTCPServerPort() + serverTransport, err := thrift.NewTServerSocketTimeout(addr.String(), TIMEOUT) + if err != nil { + t.Fatal("Unable to create server socket", err) + } + server = thrift.NewTSimpleServer4(processor, serverTransport, transportFactory, protocolFactory) + + firstProcessor := multiplexedprotocoltest.NewFirstProcessor(&FirstImpl{}) + processor.RegisterProcessor("FirstService", firstProcessor) + + secondProcessor := multiplexedprotocoltest.NewSecondProcessor(&SecondImpl{}) + processor.RegisterProcessor("SecondService", secondProcessor) + + defer server.Stop() + go server.Serve() + time.Sleep(10 * time.Millisecond) + + transport, err := createTransport(addr) + if err != nil { + t.Fatal(err) + } + defer transport.Close() + protocol := thrift.NewTMultiplexedProtocol(thrift.NewTBinaryProtocolTransport(transport), "FirstService") + + client := multiplexedprotocoltest.NewFirstClient(thrift.NewTStandardClient(protocol, protocol)) + + ret, err := client.ReturnOne(defaultCtx) + if err != nil { + t.Fatal("Unable to call first server:", err) + } else if ret != 1 { + t.Fatal("Unexpected result from server: ", ret) + } +} + +func TestMultiplexedProtocolSecond(t *testing.T) { + processor := thrift.NewTMultiplexedProcessor() + protocolFactory := thrift.NewTBinaryProtocolFactoryDefault() + transportFactory := thrift.NewTTransportFactory() + transportFactory = thrift.NewTFramedTransportFactory(transportFactory) + addr := FindAvailableTCPServerPort() + serverTransport, err := thrift.NewTServerSocketTimeout(addr.String(), TIMEOUT) + if err != nil { + t.Fatal("Unable to create server socket", err) + } + server = thrift.NewTSimpleServer4(processor, serverTransport, transportFactory, protocolFactory) + + firstProcessor := multiplexedprotocoltest.NewFirstProcessor(&FirstImpl{}) + processor.RegisterProcessor("FirstService", firstProcessor) + + secondProcessor := multiplexedprotocoltest.NewSecondProcessor(&SecondImpl{}) + processor.RegisterProcessor("SecondService", secondProcessor) + + defer server.Stop() + go server.Serve() + time.Sleep(10 * time.Millisecond) + + transport, err := createTransport(addr) + if err != nil { + t.Fatal(err) + } + defer transport.Close() + protocol := thrift.NewTMultiplexedProtocol(thrift.NewTBinaryProtocolTransport(transport), "SecondService") + + client := multiplexedprotocoltest.NewSecondClient(thrift.NewTStandardClient(protocol, protocol)) + + ret, err := client.ReturnTwo(defaultCtx) + if err != nil { + t.Fatal("Unable to call second server:", err) + } else if ret != 2 { + t.Fatal("Unexpected result from server: ", ret) + } +} + +func TestMultiplexedProtocolLegacy(t *testing.T) { + processor := thrift.NewTMultiplexedProcessor() + protocolFactory := thrift.NewTBinaryProtocolFactoryDefault() + transportFactory := thrift.NewTTransportFactory() + transportFactory = thrift.NewTFramedTransportFactory(transportFactory) + addr := FindAvailableTCPServerPort() + serverTransport, err := thrift.NewTServerSocketTimeout(addr.String(), TIMEOUT) + if err != nil { + t.Fatal("Unable to create server socket", err) + } + server = thrift.NewTSimpleServer4(processor, serverTransport, transportFactory, protocolFactory) + + firstProcessor := multiplexedprotocoltest.NewFirstProcessor(&FirstImpl{}) + processor.RegisterProcessor("FirstService", firstProcessor) + + secondProcessor := multiplexedprotocoltest.NewSecondProcessor(&SecondImpl{}) + processor.RegisterProcessor("SecondService", secondProcessor) + + defer server.Stop() + go server.Serve() + time.Sleep(10 * time.Millisecond) + + transport, err := createTransport(addr) + if err != nil { + t.Error(err) + return + } + defer transport.Close() + + protocol := thrift.NewTBinaryProtocolTransport(transport) + client := multiplexedprotocoltest.NewSecondClient(thrift.NewTStandardClient(protocol, protocol)) + + ret, err := client.ReturnTwo(defaultCtx) + //expect error since default processor is not registered + if err == nil { + t.Fatal("Expecting error") + } + + //register default processor and call again + processor.RegisterDefault(multiplexedprotocoltest.NewSecondProcessor(&SecondImpl{})) + transport, err = createTransport(addr) + if err != nil { + t.Error(err) + return + } + defer transport.Close() + + protocol = thrift.NewTBinaryProtocolTransport(transport) + client = multiplexedprotocoltest.NewSecondClient(thrift.NewTStandardClient(protocol, protocol)) + + ret, err = client.ReturnTwo(defaultCtx) + if err != nil { + t.Fatal("Unable to call legacy server:", err) + } + if ret != 2 { + t.Fatal("Unexpected result from server: ", ret) + } +} diff --git a/src/jaegertracing/thrift/lib/go/test/tests/names_test.go b/src/jaegertracing/thrift/lib/go/test/tests/names_test.go new file mode 100644 index 000000000..90b63a3b4 --- /dev/null +++ b/src/jaegertracing/thrift/lib/go/test/tests/names_test.go @@ -0,0 +1,35 @@ +/* + * 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 tests + +import ( + "namestest" + "reflect" + "testing" +) + +func TestThatAttributeNameSubstituionDoesNotOccur(t *testing.T) { + s := namestest.NamesTest{} + st := reflect.TypeOf(s) + _, ok := st.FieldByName("Type") + if !ok { + t.Error("Type attribute is missing!") + } +} diff --git a/src/jaegertracing/thrift/lib/go/test/tests/one_way_test.go b/src/jaegertracing/thrift/lib/go/test/tests/one_way_test.go new file mode 100644 index 000000000..48d0bbe38 --- /dev/null +++ b/src/jaegertracing/thrift/lib/go/test/tests/one_way_test.go @@ -0,0 +1,91 @@ +/* + * 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 tests + +import ( + "context" + "fmt" + "net" + "onewaytest" + "testing" + "thrift" + "time" +) + +func findPort() net.Addr { + if l, err := net.Listen("tcp", "127.0.0.1:0"); err != nil { + panic("Could not find available server port") + } else { + defer l.Close() + return l.Addr() + } +} + +type impl struct{} + +func (i *impl) Hi(ctx context.Context, in int64, s string) (err error) { fmt.Println("Hi!"); return } +func (i *impl) Emptyfunc(ctx context.Context) (err error) { return } +func (i *impl) EchoInt(ctx context.Context, param int64) (r int64, err error) { return param, nil } + +const TIMEOUT = time.Second + +var addr net.Addr +var server *thrift.TSimpleServer +var client *onewaytest.OneWayClient + +func TestInitOneway(t *testing.T) { + var err error + addr = findPort() + serverTransport, err := thrift.NewTServerSocketTimeout(addr.String(), TIMEOUT) + if err != nil { + t.Fatal("Unable to create server socket", err) + } + processor := onewaytest.NewOneWayProcessor(&impl{}) + server = thrift.NewTSimpleServer2(processor, serverTransport) + + go server.Serve() + time.Sleep(10 * time.Millisecond) +} + +func TestInitOnewayClient(t *testing.T) { + transport := thrift.NewTSocketFromAddrTimeout(addr, TIMEOUT) + protocol := thrift.NewTBinaryProtocolTransport(transport) + client = onewaytest.NewOneWayClient(thrift.NewTStandardClient(protocol, protocol)) + err := transport.Open() + if err != nil { + t.Fatal("Unable to open client socket", err) + } +} + +func TestCallOnewayServer(t *testing.T) { + //call oneway function + err := client.Hi(defaultCtx, 1, "") + if err != nil { + t.Fatal("Unexpected error: ", err) + } + //There is no way to detect protocol problems with single oneway call so we call it second time + i, err := client.EchoInt(defaultCtx, 42) + if err != nil { + t.Fatal("Unexpected error: ", err) + } + if i != 42 { + t.Fatal("Unexpected returned value: ", i) + } +} diff --git a/src/jaegertracing/thrift/lib/go/test/tests/optional_fields_test.go b/src/jaegertracing/thrift/lib/go/test/tests/optional_fields_test.go new file mode 100644 index 000000000..34ad6605a --- /dev/null +++ b/src/jaegertracing/thrift/lib/go/test/tests/optional_fields_test.go @@ -0,0 +1,280 @@ +/* + * 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 tests + +import ( + "bytes" + gomock "github.com/golang/mock/gomock" + "optionalfieldstest" + "testing" + "thrift" +) + +func TestIsSetReturnFalseOnCreation(t *testing.T) { + ao := optionalfieldstest.NewAllOptional() + if ao.IsSetS() { + t.Errorf("Optional field S is set on initialization") + } + if ao.IsSetI() { + t.Errorf("Optional field I is set on initialization") + } + if ao.IsSetB() { + t.Errorf("Optional field B is set on initialization") + } + if ao.IsSetS2() { + t.Errorf("Optional field S2 is set on initialization") + } + if ao.IsSetI2() { + t.Errorf("Optional field I2 is set on initialization") + } + if ao.IsSetB2() { + t.Errorf("Optional field B2 is set on initialization") + } + if ao.IsSetAa() { + t.Errorf("Optional field Aa is set on initialization") + } + if ao.IsSetL() { + t.Errorf("Optional field L is set on initialization") + } + if ao.IsSetL2() { + t.Errorf("Optional field L2 is set on initialization") + } + if ao.IsSetM() { + t.Errorf("Optional field M is set on initialization") + } + if ao.IsSetM2() { + t.Errorf("Optional field M2 is set on initialization") + } + if ao.IsSetBin() { + t.Errorf("Optional field Bin is set on initialization") + } + if ao.IsSetBin2() { + t.Errorf("Optional field Bin2 is set on initialization") + } +} + +func TestDefaultValuesOnCreation(t *testing.T) { + ao := optionalfieldstest.NewAllOptional() + if ao.GetS() != "DEFAULT" { + t.Errorf("Unexpected default value %#v for field S", ao.GetS()) + } + if ao.GetI() != 42 { + t.Errorf("Unexpected default value %#v for field I", ao.GetI()) + } + if ao.GetB() != false { + t.Errorf("Unexpected default value %#v for field B", ao.GetB()) + } + if ao.GetS2() != "" { + t.Errorf("Unexpected default value %#v for field S2", ao.GetS2()) + } + if ao.GetI2() != 0 { + t.Errorf("Unexpected default value %#v for field I2", ao.GetI2()) + } + if ao.GetB2() != false { + t.Errorf("Unexpected default value %#v for field B2", ao.GetB2()) + } + if l := ao.GetL(); len(l) != 0 { + t.Errorf("Unexpected default value %#v for field L", l) + } + if l := ao.GetL2(); len(l) != 2 || l[0] != 1 || l[1] != 2 { + t.Errorf("Unexpected default value %#v for field L2", l) + } + //FIXME: should we return empty map here? + if m := ao.GetM(); m != nil { + t.Errorf("Unexpected default value %#v for field M", m) + } + if m := ao.GetM2(); len(m) != 2 || m[1] != 2 || m[3] != 4 { + t.Errorf("Unexpected default value %#v for field M2", m) + } + if bv := ao.GetBin(); bv != nil { + t.Errorf("Unexpected default value %#v for field Bin", bv) + } + if bv := ao.GetBin2(); !bytes.Equal(bv, []byte("asdf")) { + t.Errorf("Unexpected default value %#v for field Bin2", bv) + } +} + +func TestInitialValuesOnCreation(t *testing.T) { + ao := optionalfieldstest.NewAllOptional() + if ao.S != "DEFAULT" { + t.Errorf("Unexpected initial value %#v for field S", ao.S) + } + if ao.I != 42 { + t.Errorf("Unexpected initial value %#v for field I", ao.I) + } + if ao.B != false { + t.Errorf("Unexpected initial value %#v for field B", ao.B) + } + if ao.S2 != nil { + t.Errorf("Unexpected initial value %#v for field S2", ao.S2) + } + if ao.I2 != nil { + t.Errorf("Unexpected initial value %#v for field I2", ao.I2) + } + if ao.B2 != nil { + t.Errorf("Unexpected initial value %#v for field B2", ao.B2) + } + if ao.L != nil || len(ao.L) != 0 { + t.Errorf("Unexpected initial value %#v for field L", ao.L) + } + if ao.L2 != nil { + t.Errorf("Unexpected initial value %#v for field L2", ao.L2) + } + if ao.M != nil { + t.Errorf("Unexpected initial value %#v for field M", ao.M) + } + if ao.M2 != nil { + t.Errorf("Unexpected initial value %#v for field M2", ao.M2) + } + if ao.Bin != nil || len(ao.Bin) != 0 { + t.Errorf("Unexpected initial value %#v for field Bin", ao.Bin) + } + if !bytes.Equal(ao.Bin2, []byte("asdf")) { + t.Errorf("Unexpected initial value %#v for field Bin2", ao.Bin2) + } +} + +func TestIsSetReturnTrueAfterUpdate(t *testing.T) { + ao := optionalfieldstest.NewAllOptional() + ao.S = "somevalue" + ao.I = 123 + ao.B = true + ao.Aa = optionalfieldstest.NewStructA() + if !ao.IsSetS() { + t.Errorf("Field S should be set") + } + if !ao.IsSetI() { + t.Errorf("Field I should be set") + } + if !ao.IsSetB() { + t.Errorf("Field B should be set") + } + if !ao.IsSetAa() { + t.Errorf("Field aa should be set") + } +} + +func TestListNotEmpty(t *testing.T) { + ao := optionalfieldstest.NewAllOptional() + ao.L = []int64{1, 2, 3} + if !ao.IsSetL() { + t.Errorf("Field L should be set") + } +} + +//Make sure that optional fields are not being serialized +func TestNoOptionalUnsetFieldsOnWire(t *testing.T) { + mockCtrl := gomock.NewController(t) + defer mockCtrl.Finish() + proto := NewMockTProtocol(mockCtrl) + gomock.InOrder( + proto.EXPECT().WriteStructBegin("all_optional").Return(nil), + proto.EXPECT().WriteFieldStop().Return(nil), + proto.EXPECT().WriteStructEnd().Return(nil), + ) + ao := optionalfieldstest.NewAllOptional() + ao.Write(proto) +} + +func TestNoSetToDefaultFieldsOnWire(t *testing.T) { + mockCtrl := gomock.NewController(t) + defer mockCtrl.Finish() + proto := NewMockTProtocol(mockCtrl) + gomock.InOrder( + proto.EXPECT().WriteStructBegin("all_optional").Return(nil), + proto.EXPECT().WriteFieldStop().Return(nil), + proto.EXPECT().WriteStructEnd().Return(nil), + ) + ao := optionalfieldstest.NewAllOptional() + ao.I = 42 + ao.Write(proto) +} + +//Make sure that only one field is being serialized when set to non-default +func TestOneISetFieldOnWire(t *testing.T) { + mockCtrl := gomock.NewController(t) + defer mockCtrl.Finish() + proto := NewMockTProtocol(mockCtrl) + gomock.InOrder( + proto.EXPECT().WriteStructBegin("all_optional").Return(nil), + proto.EXPECT().WriteFieldBegin("i", thrift.TType(thrift.I64), int16(2)).Return(nil), + proto.EXPECT().WriteI64(int64(123)).Return(nil), + proto.EXPECT().WriteFieldEnd().Return(nil), + proto.EXPECT().WriteFieldStop().Return(nil), + proto.EXPECT().WriteStructEnd().Return(nil), + ) + ao := optionalfieldstest.NewAllOptional() + ao.I = 123 + ao.Write(proto) +} + +func TestOneLSetFieldOnWire(t *testing.T) { + mockCtrl := gomock.NewController(t) + defer mockCtrl.Finish() + proto := NewMockTProtocol(mockCtrl) + gomock.InOrder( + proto.EXPECT().WriteStructBegin("all_optional").Return(nil), + proto.EXPECT().WriteFieldBegin("l", thrift.TType(thrift.LIST), int16(9)).Return(nil), + proto.EXPECT().WriteListBegin(thrift.TType(thrift.I64), 2).Return(nil), + proto.EXPECT().WriteI64(int64(1)).Return(nil), + proto.EXPECT().WriteI64(int64(2)).Return(nil), + proto.EXPECT().WriteListEnd().Return(nil), + proto.EXPECT().WriteFieldEnd().Return(nil), + proto.EXPECT().WriteFieldStop().Return(nil), + proto.EXPECT().WriteStructEnd().Return(nil), + ) + ao := optionalfieldstest.NewAllOptional() + ao.L = []int64{1, 2} + ao.Write(proto) +} + +func TestOneBinSetFieldOnWire(t *testing.T) { + mockCtrl := gomock.NewController(t) + defer mockCtrl.Finish() + proto := NewMockTProtocol(mockCtrl) + gomock.InOrder( + proto.EXPECT().WriteStructBegin("all_optional").Return(nil), + proto.EXPECT().WriteFieldBegin("bin", thrift.TType(thrift.STRING), int16(13)).Return(nil), + proto.EXPECT().WriteBinary([]byte("somebytestring")).Return(nil), + proto.EXPECT().WriteFieldEnd().Return(nil), + proto.EXPECT().WriteFieldStop().Return(nil), + proto.EXPECT().WriteStructEnd().Return(nil), + ) + ao := optionalfieldstest.NewAllOptional() + ao.Bin = []byte("somebytestring") + ao.Write(proto) +} + +func TestOneEmptyBinSetFieldOnWire(t *testing.T) { + mockCtrl := gomock.NewController(t) + defer mockCtrl.Finish() + proto := NewMockTProtocol(mockCtrl) + gomock.InOrder( + proto.EXPECT().WriteStructBegin("all_optional").Return(nil), + proto.EXPECT().WriteFieldBegin("bin", thrift.TType(thrift.STRING), int16(13)).Return(nil), + proto.EXPECT().WriteBinary([]byte{}).Return(nil), + proto.EXPECT().WriteFieldEnd().Return(nil), + proto.EXPECT().WriteFieldStop().Return(nil), + proto.EXPECT().WriteStructEnd().Return(nil), + ) + ao := optionalfieldstest.NewAllOptional() + ao.Bin = []byte{} + ao.Write(proto) +} diff --git a/src/jaegertracing/thrift/lib/go/test/tests/protocol_mock.go b/src/jaegertracing/thrift/lib/go/test/tests/protocol_mock.go new file mode 100644 index 000000000..51d7a02ff --- /dev/null +++ b/src/jaegertracing/thrift/lib/go/test/tests/protocol_mock.go @@ -0,0 +1,513 @@ +/* + * 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: thrift (interfaces: TProtocol) + +package tests + +import ( + "context" + thrift "thrift" + + gomock "github.com/golang/mock/gomock" +) + +// Mock of TProtocol interface +type MockTProtocol struct { + ctrl *gomock.Controller + recorder *_MockTProtocolRecorder +} + +// Recorder for MockTProtocol (not exported) +type _MockTProtocolRecorder struct { + mock *MockTProtocol +} + +func NewMockTProtocol(ctrl *gomock.Controller) *MockTProtocol { + mock := &MockTProtocol{ctrl: ctrl} + mock.recorder = &_MockTProtocolRecorder{mock} + return mock +} + +func (_m *MockTProtocol) EXPECT() *_MockTProtocolRecorder { + return _m.recorder +} + +func (_m *MockTProtocol) Flush(ctx context.Context) error { + ret := _m.ctrl.Call(_m, "Flush") + ret0, _ := ret[0].(error) + return ret0 +} + +func (_mr *_MockTProtocolRecorder) Flush(ctx context.Context) *gomock.Call { + return _mr.mock.ctrl.RecordCall(_mr.mock, "Flush") +} + +func (_m *MockTProtocol) ReadBinary() ([]byte, error) { + ret := _m.ctrl.Call(_m, "ReadBinary") + ret0, _ := ret[0].([]byte) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +func (_mr *_MockTProtocolRecorder) ReadBinary() *gomock.Call { + return _mr.mock.ctrl.RecordCall(_mr.mock, "ReadBinary") +} + +func (_m *MockTProtocol) ReadBool() (bool, error) { + ret := _m.ctrl.Call(_m, "ReadBool") + ret0, _ := ret[0].(bool) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +func (_mr *_MockTProtocolRecorder) ReadBool() *gomock.Call { + return _mr.mock.ctrl.RecordCall(_mr.mock, "ReadBool") +} + +func (_m *MockTProtocol) ReadByte() (int8, error) { + ret := _m.ctrl.Call(_m, "ReadByte") + ret0, _ := ret[0].(int8) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +func (_mr *_MockTProtocolRecorder) ReadByte() *gomock.Call { + return _mr.mock.ctrl.RecordCall(_mr.mock, "ReadByte") +} + +func (_m *MockTProtocol) ReadDouble() (float64, error) { + ret := _m.ctrl.Call(_m, "ReadDouble") + ret0, _ := ret[0].(float64) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +func (_mr *_MockTProtocolRecorder) ReadDouble() *gomock.Call { + return _mr.mock.ctrl.RecordCall(_mr.mock, "ReadDouble") +} + +func (_m *MockTProtocol) ReadFieldBegin() (string, thrift.TType, int16, error) { + ret := _m.ctrl.Call(_m, "ReadFieldBegin") + ret0, _ := ret[0].(string) + ret1, _ := ret[1].(thrift.TType) + ret2, _ := ret[2].(int16) + ret3, _ := ret[3].(error) + return ret0, ret1, ret2, ret3 +} + +func (_mr *_MockTProtocolRecorder) ReadFieldBegin() *gomock.Call { + return _mr.mock.ctrl.RecordCall(_mr.mock, "ReadFieldBegin") +} + +func (_m *MockTProtocol) ReadFieldEnd() error { + ret := _m.ctrl.Call(_m, "ReadFieldEnd") + ret0, _ := ret[0].(error) + return ret0 +} + +func (_mr *_MockTProtocolRecorder) ReadFieldEnd() *gomock.Call { + return _mr.mock.ctrl.RecordCall(_mr.mock, "ReadFieldEnd") +} + +func (_m *MockTProtocol) ReadI16() (int16, error) { + ret := _m.ctrl.Call(_m, "ReadI16") + ret0, _ := ret[0].(int16) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +func (_mr *_MockTProtocolRecorder) ReadI16() *gomock.Call { + return _mr.mock.ctrl.RecordCall(_mr.mock, "ReadI16") +} + +func (_m *MockTProtocol) ReadI32() (int32, error) { + ret := _m.ctrl.Call(_m, "ReadI32") + ret0, _ := ret[0].(int32) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +func (_mr *_MockTProtocolRecorder) ReadI32() *gomock.Call { + return _mr.mock.ctrl.RecordCall(_mr.mock, "ReadI32") +} + +func (_m *MockTProtocol) ReadI64() (int64, error) { + ret := _m.ctrl.Call(_m, "ReadI64") + ret0, _ := ret[0].(int64) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +func (_mr *_MockTProtocolRecorder) ReadI64() *gomock.Call { + return _mr.mock.ctrl.RecordCall(_mr.mock, "ReadI64") +} + +func (_m *MockTProtocol) ReadListBegin() (thrift.TType, int, error) { + ret := _m.ctrl.Call(_m, "ReadListBegin") + ret0, _ := ret[0].(thrift.TType) + ret1, _ := ret[1].(int) + ret2, _ := ret[2].(error) + return ret0, ret1, ret2 +} + +func (_mr *_MockTProtocolRecorder) ReadListBegin() *gomock.Call { + return _mr.mock.ctrl.RecordCall(_mr.mock, "ReadListBegin") +} + +func (_m *MockTProtocol) ReadListEnd() error { + ret := _m.ctrl.Call(_m, "ReadListEnd") + ret0, _ := ret[0].(error) + return ret0 +} + +func (_mr *_MockTProtocolRecorder) ReadListEnd() *gomock.Call { + return _mr.mock.ctrl.RecordCall(_mr.mock, "ReadListEnd") +} + +func (_m *MockTProtocol) ReadMapBegin() (thrift.TType, thrift.TType, int, error) { + ret := _m.ctrl.Call(_m, "ReadMapBegin") + ret0, _ := ret[0].(thrift.TType) + ret1, _ := ret[1].(thrift.TType) + ret2, _ := ret[2].(int) + ret3, _ := ret[3].(error) + return ret0, ret1, ret2, ret3 +} + +func (_mr *_MockTProtocolRecorder) ReadMapBegin() *gomock.Call { + return _mr.mock.ctrl.RecordCall(_mr.mock, "ReadMapBegin") +} + +func (_m *MockTProtocol) ReadMapEnd() error { + ret := _m.ctrl.Call(_m, "ReadMapEnd") + ret0, _ := ret[0].(error) + return ret0 +} + +func (_mr *_MockTProtocolRecorder) ReadMapEnd() *gomock.Call { + return _mr.mock.ctrl.RecordCall(_mr.mock, "ReadMapEnd") +} + +func (_m *MockTProtocol) ReadMessageBegin() (string, thrift.TMessageType, int32, error) { + ret := _m.ctrl.Call(_m, "ReadMessageBegin") + ret0, _ := ret[0].(string) + ret1, _ := ret[1].(thrift.TMessageType) + ret2, _ := ret[2].(int32) + ret3, _ := ret[3].(error) + return ret0, ret1, ret2, ret3 +} + +func (_mr *_MockTProtocolRecorder) ReadMessageBegin() *gomock.Call { + return _mr.mock.ctrl.RecordCall(_mr.mock, "ReadMessageBegin") +} + +func (_m *MockTProtocol) ReadMessageEnd() error { + ret := _m.ctrl.Call(_m, "ReadMessageEnd") + ret0, _ := ret[0].(error) + return ret0 +} + +func (_mr *_MockTProtocolRecorder) ReadMessageEnd() *gomock.Call { + return _mr.mock.ctrl.RecordCall(_mr.mock, "ReadMessageEnd") +} + +func (_m *MockTProtocol) ReadSetBegin() (thrift.TType, int, error) { + ret := _m.ctrl.Call(_m, "ReadSetBegin") + ret0, _ := ret[0].(thrift.TType) + ret1, _ := ret[1].(int) + ret2, _ := ret[2].(error) + return ret0, ret1, ret2 +} + +func (_mr *_MockTProtocolRecorder) ReadSetBegin() *gomock.Call { + return _mr.mock.ctrl.RecordCall(_mr.mock, "ReadSetBegin") +} + +func (_m *MockTProtocol) ReadSetEnd() error { + ret := _m.ctrl.Call(_m, "ReadSetEnd") + ret0, _ := ret[0].(error) + return ret0 +} + +func (_mr *_MockTProtocolRecorder) ReadSetEnd() *gomock.Call { + return _mr.mock.ctrl.RecordCall(_mr.mock, "ReadSetEnd") +} + +func (_m *MockTProtocol) ReadString() (string, error) { + ret := _m.ctrl.Call(_m, "ReadString") + ret0, _ := ret[0].(string) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +func (_mr *_MockTProtocolRecorder) ReadString() *gomock.Call { + return _mr.mock.ctrl.RecordCall(_mr.mock, "ReadString") +} + +func (_m *MockTProtocol) ReadStructBegin() (string, error) { + ret := _m.ctrl.Call(_m, "ReadStructBegin") + ret0, _ := ret[0].(string) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +func (_mr *_MockTProtocolRecorder) ReadStructBegin() *gomock.Call { + return _mr.mock.ctrl.RecordCall(_mr.mock, "ReadStructBegin") +} + +func (_m *MockTProtocol) ReadStructEnd() error { + ret := _m.ctrl.Call(_m, "ReadStructEnd") + ret0, _ := ret[0].(error) + return ret0 +} + +func (_mr *_MockTProtocolRecorder) ReadStructEnd() *gomock.Call { + return _mr.mock.ctrl.RecordCall(_mr.mock, "ReadStructEnd") +} + +func (_m *MockTProtocol) Skip(_param0 thrift.TType) error { + ret := _m.ctrl.Call(_m, "Skip", _param0) + ret0, _ := ret[0].(error) + return ret0 +} + +func (_mr *_MockTProtocolRecorder) Skip(arg0 interface{}) *gomock.Call { + return _mr.mock.ctrl.RecordCall(_mr.mock, "Skip", arg0) +} + +func (_m *MockTProtocol) Transport() thrift.TTransport { + ret := _m.ctrl.Call(_m, "Transport") + ret0, _ := ret[0].(thrift.TTransport) + return ret0 +} + +func (_mr *_MockTProtocolRecorder) Transport() *gomock.Call { + return _mr.mock.ctrl.RecordCall(_mr.mock, "Transport") +} + +func (_m *MockTProtocol) WriteBinary(_param0 []byte) error { + ret := _m.ctrl.Call(_m, "WriteBinary", _param0) + ret0, _ := ret[0].(error) + return ret0 +} + +func (_mr *_MockTProtocolRecorder) WriteBinary(arg0 interface{}) *gomock.Call { + return _mr.mock.ctrl.RecordCall(_mr.mock, "WriteBinary", arg0) +} + +func (_m *MockTProtocol) WriteBool(_param0 bool) error { + ret := _m.ctrl.Call(_m, "WriteBool", _param0) + ret0, _ := ret[0].(error) + return ret0 +} + +func (_mr *_MockTProtocolRecorder) WriteBool(arg0 interface{}) *gomock.Call { + return _mr.mock.ctrl.RecordCall(_mr.mock, "WriteBool", arg0) +} + +func (_m *MockTProtocol) WriteByte(_param0 int8) error { + ret := _m.ctrl.Call(_m, "WriteByte", _param0) + ret0, _ := ret[0].(error) + return ret0 +} + +func (_mr *_MockTProtocolRecorder) WriteByte(arg0 interface{}) *gomock.Call { + return _mr.mock.ctrl.RecordCall(_mr.mock, "WriteByte", arg0) +} + +func (_m *MockTProtocol) WriteDouble(_param0 float64) error { + ret := _m.ctrl.Call(_m, "WriteDouble", _param0) + ret0, _ := ret[0].(error) + return ret0 +} + +func (_mr *_MockTProtocolRecorder) WriteDouble(arg0 interface{}) *gomock.Call { + return _mr.mock.ctrl.RecordCall(_mr.mock, "WriteDouble", arg0) +} + +func (_m *MockTProtocol) WriteFieldBegin(_param0 string, _param1 thrift.TType, _param2 int16) error { + ret := _m.ctrl.Call(_m, "WriteFieldBegin", _param0, _param1, _param2) + ret0, _ := ret[0].(error) + return ret0 +} + +func (_mr *_MockTProtocolRecorder) WriteFieldBegin(arg0, arg1, arg2 interface{}) *gomock.Call { + return _mr.mock.ctrl.RecordCall(_mr.mock, "WriteFieldBegin", arg0, arg1, arg2) +} + +func (_m *MockTProtocol) WriteFieldEnd() error { + ret := _m.ctrl.Call(_m, "WriteFieldEnd") + ret0, _ := ret[0].(error) + return ret0 +} + +func (_mr *_MockTProtocolRecorder) WriteFieldEnd() *gomock.Call { + return _mr.mock.ctrl.RecordCall(_mr.mock, "WriteFieldEnd") +} + +func (_m *MockTProtocol) WriteFieldStop() error { + ret := _m.ctrl.Call(_m, "WriteFieldStop") + ret0, _ := ret[0].(error) + return ret0 +} + +func (_mr *_MockTProtocolRecorder) WriteFieldStop() *gomock.Call { + return _mr.mock.ctrl.RecordCall(_mr.mock, "WriteFieldStop") +} + +func (_m *MockTProtocol) WriteI16(_param0 int16) error { + ret := _m.ctrl.Call(_m, "WriteI16", _param0) + ret0, _ := ret[0].(error) + return ret0 +} + +func (_mr *_MockTProtocolRecorder) WriteI16(arg0 interface{}) *gomock.Call { + return _mr.mock.ctrl.RecordCall(_mr.mock, "WriteI16", arg0) +} + +func (_m *MockTProtocol) WriteI32(_param0 int32) error { + ret := _m.ctrl.Call(_m, "WriteI32", _param0) + ret0, _ := ret[0].(error) + return ret0 +} + +func (_mr *_MockTProtocolRecorder) WriteI32(arg0 interface{}) *gomock.Call { + return _mr.mock.ctrl.RecordCall(_mr.mock, "WriteI32", arg0) +} + +func (_m *MockTProtocol) WriteI64(_param0 int64) error { + ret := _m.ctrl.Call(_m, "WriteI64", _param0) + ret0, _ := ret[0].(error) + return ret0 +} + +func (_mr *_MockTProtocolRecorder) WriteI64(arg0 interface{}) *gomock.Call { + return _mr.mock.ctrl.RecordCall(_mr.mock, "WriteI64", arg0) +} + +func (_m *MockTProtocol) WriteListBegin(_param0 thrift.TType, _param1 int) error { + ret := _m.ctrl.Call(_m, "WriteListBegin", _param0, _param1) + ret0, _ := ret[0].(error) + return ret0 +} + +func (_mr *_MockTProtocolRecorder) WriteListBegin(arg0, arg1 interface{}) *gomock.Call { + return _mr.mock.ctrl.RecordCall(_mr.mock, "WriteListBegin", arg0, arg1) +} + +func (_m *MockTProtocol) WriteListEnd() error { + ret := _m.ctrl.Call(_m, "WriteListEnd") + ret0, _ := ret[0].(error) + return ret0 +} + +func (_mr *_MockTProtocolRecorder) WriteListEnd() *gomock.Call { + return _mr.mock.ctrl.RecordCall(_mr.mock, "WriteListEnd") +} + +func (_m *MockTProtocol) WriteMapBegin(_param0 thrift.TType, _param1 thrift.TType, _param2 int) error { + ret := _m.ctrl.Call(_m, "WriteMapBegin", _param0, _param1, _param2) + ret0, _ := ret[0].(error) + return ret0 +} + +func (_mr *_MockTProtocolRecorder) WriteMapBegin(arg0, arg1, arg2 interface{}) *gomock.Call { + return _mr.mock.ctrl.RecordCall(_mr.mock, "WriteMapBegin", arg0, arg1, arg2) +} + +func (_m *MockTProtocol) WriteMapEnd() error { + ret := _m.ctrl.Call(_m, "WriteMapEnd") + ret0, _ := ret[0].(error) + return ret0 +} + +func (_mr *_MockTProtocolRecorder) WriteMapEnd() *gomock.Call { + return _mr.mock.ctrl.RecordCall(_mr.mock, "WriteMapEnd") +} + +func (_m *MockTProtocol) WriteMessageBegin(_param0 string, _param1 thrift.TMessageType, _param2 int32) error { + ret := _m.ctrl.Call(_m, "WriteMessageBegin", _param0, _param1, _param2) + ret0, _ := ret[0].(error) + return ret0 +} + +func (_mr *_MockTProtocolRecorder) WriteMessageBegin(arg0, arg1, arg2 interface{}) *gomock.Call { + return _mr.mock.ctrl.RecordCall(_mr.mock, "WriteMessageBegin", arg0, arg1, arg2) +} + +func (_m *MockTProtocol) WriteMessageEnd() error { + ret := _m.ctrl.Call(_m, "WriteMessageEnd") + ret0, _ := ret[0].(error) + return ret0 +} + +func (_mr *_MockTProtocolRecorder) WriteMessageEnd() *gomock.Call { + return _mr.mock.ctrl.RecordCall(_mr.mock, "WriteMessageEnd") +} + +func (_m *MockTProtocol) WriteSetBegin(_param0 thrift.TType, _param1 int) error { + ret := _m.ctrl.Call(_m, "WriteSetBegin", _param0, _param1) + ret0, _ := ret[0].(error) + return ret0 +} + +func (_mr *_MockTProtocolRecorder) WriteSetBegin(arg0, arg1 interface{}) *gomock.Call { + return _mr.mock.ctrl.RecordCall(_mr.mock, "WriteSetBegin", arg0, arg1) +} + +func (_m *MockTProtocol) WriteSetEnd() error { + ret := _m.ctrl.Call(_m, "WriteSetEnd") + ret0, _ := ret[0].(error) + return ret0 +} + +func (_mr *_MockTProtocolRecorder) WriteSetEnd() *gomock.Call { + return _mr.mock.ctrl.RecordCall(_mr.mock, "WriteSetEnd") +} + +func (_m *MockTProtocol) WriteString(_param0 string) error { + ret := _m.ctrl.Call(_m, "WriteString", _param0) + ret0, _ := ret[0].(error) + return ret0 +} + +func (_mr *_MockTProtocolRecorder) WriteString(arg0 interface{}) *gomock.Call { + return _mr.mock.ctrl.RecordCall(_mr.mock, "WriteString", arg0) +} + +func (_m *MockTProtocol) WriteStructBegin(_param0 string) error { + ret := _m.ctrl.Call(_m, "WriteStructBegin", _param0) + ret0, _ := ret[0].(error) + return ret0 +} + +func (_mr *_MockTProtocolRecorder) WriteStructBegin(arg0 interface{}) *gomock.Call { + return _mr.mock.ctrl.RecordCall(_mr.mock, "WriteStructBegin", arg0) +} + +func (_m *MockTProtocol) WriteStructEnd() error { + ret := _m.ctrl.Call(_m, "WriteStructEnd") + ret0, _ := ret[0].(error) + return ret0 +} + +func (_mr *_MockTProtocolRecorder) WriteStructEnd() *gomock.Call { + return _mr.mock.ctrl.RecordCall(_mr.mock, "WriteStructEnd") +} diff --git a/src/jaegertracing/thrift/lib/go/test/tests/protocols_test.go b/src/jaegertracing/thrift/lib/go/test/tests/protocols_test.go new file mode 100644 index 000000000..cffd9c3f7 --- /dev/null +++ b/src/jaegertracing/thrift/lib/go/test/tests/protocols_test.go @@ -0,0 +1,97 @@ +/* + * 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 tests + +import ( + "testing" + "thrift" + "thrifttest" +) + +func RunSocketTestSuite(t *testing.T, protocolFactory thrift.TProtocolFactory, + transportFactory thrift.TTransportFactory) { + // server + var err error + addr = FindAvailableTCPServerPort() + serverTransport, err := thrift.NewTServerSocketTimeout(addr.String(), TIMEOUT) + if err != nil { + t.Fatal("Unable to create server socket", err) + } + processor := thrifttest.NewThriftTestProcessor(NewThriftTestHandler()) + server = thrift.NewTSimpleServer4(processor, serverTransport, transportFactory, protocolFactory) + server.Listen() + + go server.Serve() + + // client + var transport thrift.TTransport = thrift.NewTSocketFromAddrTimeout(addr, TIMEOUT) + transport, err = transportFactory.GetTransport(transport) + if err != nil { + t.Fatal(err) + } + var protocol thrift.TProtocol = protocolFactory.GetProtocol(transport) + thriftTestClient := thrifttest.NewThriftTestClient(thrift.NewTStandardClient(protocol, protocol)) + err = transport.Open() + if err != nil { + t.Fatal("Unable to open client socket", err) + } + + driver := NewThriftTestDriver(t, thriftTestClient) + driver.Start() +} + +// Run test suite using TJSONProtocol +func TestTJSONProtocol(t *testing.T) { + RunSocketTestSuite(t, + thrift.NewTJSONProtocolFactory(), + thrift.NewTTransportFactory()) + RunSocketTestSuite(t, + thrift.NewTJSONProtocolFactory(), + thrift.NewTBufferedTransportFactory(8912)) + RunSocketTestSuite(t, + thrift.NewTJSONProtocolFactory(), + thrift.NewTFramedTransportFactory(thrift.NewTTransportFactory())) +} + +// Run test suite using TBinaryProtocol +func TestTBinaryProtocol(t *testing.T) { + RunSocketTestSuite(t, + thrift.NewTBinaryProtocolFactoryDefault(), + thrift.NewTTransportFactory()) + RunSocketTestSuite(t, + thrift.NewTBinaryProtocolFactoryDefault(), + thrift.NewTBufferedTransportFactory(8912)) + RunSocketTestSuite(t, + thrift.NewTBinaryProtocolFactoryDefault(), + thrift.NewTFramedTransportFactory(thrift.NewTTransportFactory())) +} + +// Run test suite using TCompactBinaryProtocol +func TestTCompactProtocol(t *testing.T) { + RunSocketTestSuite(t, + thrift.NewTCompactProtocolFactory(), + thrift.NewTTransportFactory()) + RunSocketTestSuite(t, + thrift.NewTCompactProtocolFactory(), + thrift.NewTBufferedTransportFactory(8912)) + RunSocketTestSuite(t, + thrift.NewTCompactProtocolFactory(), + thrift.NewTFramedTransportFactory(thrift.NewTTransportFactory())) +} diff --git a/src/jaegertracing/thrift/lib/go/test/tests/required_fields_test.go b/src/jaegertracing/thrift/lib/go/test/tests/required_fields_test.go new file mode 100644 index 000000000..3fa414ad8 --- /dev/null +++ b/src/jaegertracing/thrift/lib/go/test/tests/required_fields_test.go @@ -0,0 +1,130 @@ +/* + * 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 tests + +import ( + "context" + "github.com/golang/mock/gomock" + "optionalfieldstest" + "requiredfieldtest" + "testing" + "thrift" +) + +func TestRequiredField_SucecssWhenSet(t *testing.T) { + // create a new RequiredField instance with the required field set + source := &requiredfieldtest.RequiredField{Name: "this is a test"} + sourceData, err := thrift.NewTSerializer().Write(context.Background(), source) + if err != nil { + t.Fatalf("failed to serialize %T: %v", source, err) + } + + d := thrift.NewTDeserializer() + err = d.Read(&requiredfieldtest.RequiredField{}, sourceData) + if err != nil { + t.Fatalf("Did not expect an error when trying to deserialize the requiredfieldtest.RequiredField: %v", err) + } +} + +func TestRequiredField_ErrorWhenMissing(t *testing.T) { + // create a new OtherThing instance, without setting the required field + source := &requiredfieldtest.OtherThing{} + sourceData, err := thrift.NewTSerializer().Write(context.Background(), source) + if err != nil { + t.Fatalf("failed to serialize %T: %v", source, err) + } + + // attempt to deserialize into a different type (which should fail) + d := thrift.NewTDeserializer() + err = d.Read(&requiredfieldtest.RequiredField{}, sourceData) + if err == nil { + t.Fatal("Expected an error when trying to deserialize an object which is missing a required field") + } +} + +func TestStructReadRequiredFields(t *testing.T) { + mockCtrl := gomock.NewController(t) + protocol := NewMockTProtocol(mockCtrl) + testStruct := optionalfieldstest.NewStructC() + + // None of required fields are set + gomock.InOrder( + protocol.EXPECT().ReadStructBegin().Return("StructC", nil), + protocol.EXPECT().ReadFieldBegin().Return("_", thrift.TType(thrift.STOP), int16(1), nil), + protocol.EXPECT().ReadStructEnd().Return(nil), + ) + + err := testStruct.Read(protocol) + mockCtrl.Finish() + mockCtrl = gomock.NewController(t) + if err == nil { + t.Fatal("Expected read to fail") + } + err2, ok := err.(thrift.TProtocolException) + if !ok { + t.Fatal("Expected a TProtocolException") + } + if err2.TypeId() != thrift.INVALID_DATA { + t.Fatal("Expected INVALID_DATA TProtocolException") + } + + // One of the required fields is set + gomock.InOrder( + protocol.EXPECT().ReadStructBegin().Return("StructC", nil), + protocol.EXPECT().ReadFieldBegin().Return("I", thrift.TType(thrift.I32), int16(2), nil), + protocol.EXPECT().ReadI32().Return(int32(1), nil), + protocol.EXPECT().ReadFieldEnd().Return(nil), + protocol.EXPECT().ReadFieldBegin().Return("_", thrift.TType(thrift.STOP), int16(1), nil), + protocol.EXPECT().ReadStructEnd().Return(nil), + ) + + err = testStruct.Read(protocol) + mockCtrl.Finish() + mockCtrl = gomock.NewController(t) + if err == nil { + t.Fatal("Expected read to fail") + } + err2, ok = err.(thrift.TProtocolException) + if !ok { + t.Fatal("Expected a TProtocolException") + } + if err2.TypeId() != thrift.INVALID_DATA { + t.Fatal("Expected INVALID_DATA TProtocolException") + } + + // Both of the required fields are set + gomock.InOrder( + protocol.EXPECT().ReadStructBegin().Return("StructC", nil), + protocol.EXPECT().ReadFieldBegin().Return("i", thrift.TType(thrift.I32), int16(2), nil), + protocol.EXPECT().ReadI32().Return(int32(1), nil), + protocol.EXPECT().ReadFieldEnd().Return(nil), + protocol.EXPECT().ReadFieldBegin().Return("s2", thrift.TType(thrift.STRING), int16(4), nil), + protocol.EXPECT().ReadString().Return("test", nil), + protocol.EXPECT().ReadFieldEnd().Return(nil), + protocol.EXPECT().ReadFieldBegin().Return("_", thrift.TType(thrift.STOP), int16(1), nil), + protocol.EXPECT().ReadStructEnd().Return(nil), + ) + + err = testStruct.Read(protocol) + mockCtrl.Finish() + if err != nil { + t.Fatal("Expected read to succeed") + } +} diff --git a/src/jaegertracing/thrift/lib/go/test/tests/struct_args_rets_test.go b/src/jaegertracing/thrift/lib/go/test/tests/struct_args_rets_test.go new file mode 100644 index 000000000..81e9b2658 --- /dev/null +++ b/src/jaegertracing/thrift/lib/go/test/tests/struct_args_rets_test.go @@ -0,0 +1,36 @@ +/* + * 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 tests + +import ( + st "servicestest" +) + +//this function is never called, it will fail to compile if check is failed +func staticCheckStructArgsResults() { + //Check that struct args and results are passed by reference + var sa *st.StructA = &st.StructA{} + var iface st.AServ + var err error + + sa, err = iface.StructAFunc_1structA(defaultCtx, sa) + _ = err + _ = sa +} diff --git a/src/jaegertracing/thrift/lib/go/test/tests/thrifttest_driver.go b/src/jaegertracing/thrift/lib/go/test/tests/thrifttest_driver.go new file mode 100644 index 000000000..de54cbcaa --- /dev/null +++ b/src/jaegertracing/thrift/lib/go/test/tests/thrifttest_driver.go @@ -0,0 +1,236 @@ +/* + * 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 tests + +import ( + "reflect" + "testing" + "thrifttest" +) + +type ThriftTestDriver struct { + client thrifttest.ThriftTest + t *testing.T +} + +func NewThriftTestDriver(t *testing.T, client thrifttest.ThriftTest) *ThriftTestDriver { + return &ThriftTestDriver{client, t} +} + +func (p *ThriftTestDriver) Start() { + client := p.client + t := p.t + + if client.TestVoid(defaultCtx) != nil { + t.Fatal("TestVoid failed") + } + + if r, err := client.TestString(defaultCtx, "Test"); r != "Test" || err != nil { + t.Fatal("TestString with simple text failed") + } + + if r, err := client.TestString(defaultCtx, ""); r != "" || err != nil { + t.Fatal("TestString with empty text failed") + } + + stringTest := "Afrikaans, Alemannisch, Aragonés, العربية, مصرى, " + + "Asturianu, Aymar aru, Azərbaycan, Башҡорт, Boarisch, Žemaitėška, " + + "Беларуская, Беларуская (тарашкевіца), Български, Bamanankan, " + + "বাংলা, Brezhoneg, Bosanski, Català, Mìng-dĕ̤ng-ngṳ̄, Нохчийн, " + + "Cebuano, ᏣᎳᎩ, Česky, Словѣ́ньскъ / ⰔⰎⰑⰂⰡⰐⰠⰔⰍⰟ, Чӑвашла, Cymraeg, " + + "Dansk, Zazaki, ދިވެހިބަސް, Ελληνικά, Emiliàn e rumagnòl, English, " + + "Esperanto, Español, Eesti, Euskara, فارسی, Suomi, Võro, Føroyskt, " + + "Français, Arpetan, Furlan, Frysk, Gaeilge, 贛語, Gàidhlig, Galego, " + + "Avañe'ẽ, ગુજરાતી, Gaelg, עברית, हिन्दी, Fiji Hindi, Hrvatski, " + + "Kreyòl ayisyen, Magyar, Հայերեն, Interlingua, Bahasa Indonesia, " + + "Ilokano, Ido, Íslenska, Italiano, 日本語, Lojban, Basa Jawa, " + + "ქართული, Kongo, Kalaallisut, ಕನ್ನಡ, 한국어, Къарачай-Малкъар, " + + "Ripoarisch, Kurdî, Коми, Kernewek, Кыргызча, Latina, Ladino, " + + "Lëtzebuergesch, Limburgs, Lingála, ລາວ, Lietuvių, Latviešu, Basa " + + "Banyumasan, Malagasy, Македонски, മലയാളം, मराठी, مازِرونی, Bahasa " + + "Melayu, Nnapulitano, Nedersaksisch, नेपाल भाषा, Nederlands, " + + "Norsk (nynorsk), Norsk (bokmål), Nouormand, Diné bizaad, " + + "Occitan, Иронау, Papiamentu, Deitsch, Polski, پنجابی, پښتو, " + + "Norfuk / Pitkern, Português, Runa Simi, Rumantsch, Romani, Română, " + + "Русский, Саха тыла, Sardu, Sicilianu, Scots, Sámegiella, Simple " + + "English, Slovenčina, Slovenščina, Српски / Srpski, Seeltersk, " + + "Svenska, Kiswahili, தமிழ், తెలుగు, Тоҷикӣ, ไทย, Türkmençe, Tagalog, " + + "Türkçe, Татарча/Tatarça, Українська, اردو, Tiếng Việt, Volapük, " + + "Walon, Winaray, 吴语, isiXhosa, ייִדיש, Yorùbá, Zeêuws, 中文, " + + "Bân-lâm-gú, 粵語" + + if r, err := client.TestString(defaultCtx, stringTest); r != stringTest || err != nil { + t.Fatal("TestString with all languages failed") + } + + specialCharacters := "quote: \" backslash:" + + " backspace: \b formfeed: \f newline: \n return: \r tab: " + + " now-all-of-them-together: '\\\b\n\r\t'" + + " now-a-bunch-of-junk: !@#$%&()(&%$#{}{}<><><" + + " char-to-test-json-parsing: ]] \"]] \\\" }}}{ [[[ " + + if r, err := client.TestString(defaultCtx, specialCharacters); r != specialCharacters || err != nil { + t.Fatal("TestString with specialCharacters failed") + } + + if r, err := client.TestByte(defaultCtx, 1); r != 1 || err != nil { + t.Fatal("TestByte(1) failed") + } + if r, err := client.TestByte(defaultCtx, 0); r != 0 || err != nil { + t.Fatal("TestByte(0) failed") + } + if r, err := client.TestByte(defaultCtx, -1); r != -1 || err != nil { + t.Fatal("TestByte(-1) failed") + } + if r, err := client.TestByte(defaultCtx, -127); r != -127 || err != nil { + t.Fatal("TestByte(-127) failed") + } + + if r, err := client.TestI32(defaultCtx, -1); r != -1 || err != nil { + t.Fatal("TestI32(-1) failed") + } + if r, err := client.TestI32(defaultCtx, 1); r != 1 || err != nil { + t.Fatal("TestI32(1) failed") + } + + if r, err := client.TestI64(defaultCtx, -5); r != -5 || err != nil { + t.Fatal("TestI64(-5) failed") + } + if r, err := client.TestI64(defaultCtx, 5); r != 5 || err != nil { + t.Fatal("TestI64(5) failed") + } + if r, err := client.TestI64(defaultCtx, -34359738368); r != -34359738368 || err != nil { + t.Fatal("TestI64(-34359738368) failed") + } + + if r, err := client.TestDouble(defaultCtx, -5.2098523); r != -5.2098523 || err != nil { + t.Fatal("TestDouble(-5.2098523) failed") + } + if r, err := client.TestDouble(defaultCtx, -7.012052175215044); r != -7.012052175215044 || err != nil { + t.Fatal("TestDouble(-7.012052175215044) failed") + } + + // TODO: add testBinary() call + + out := thrifttest.NewXtruct() + out.StringThing = "Zero" + out.ByteThing = 1 + out.I32Thing = -3 + out.I64Thing = 1000000 + if r, err := client.TestStruct(defaultCtx, out); !reflect.DeepEqual(r, out) || err != nil { + t.Fatal("TestStruct failed") + } + + out2 := thrifttest.NewXtruct2() + out2.ByteThing = 1 + out2.StructThing = out + out2.I32Thing = 5 + if r, err := client.TestNest(defaultCtx, out2); !reflect.DeepEqual(r, out2) || err != nil { + t.Fatal("TestNest failed") + } + + mapout := make(map[int32]int32) + for i := int32(0); i < 5; i++ { + mapout[i] = i - 10 + } + if r, err := client.TestMap(defaultCtx, mapout); !reflect.DeepEqual(r, mapout) || err != nil { + t.Fatal("TestMap failed") + } + + mapTestInput := map[string]string{ + "a": "123", "a b": "with spaces ", "same": "same", "0": "numeric key", + "longValue": stringTest, stringTest: "long key", + } + if r, err := client.TestStringMap(defaultCtx, mapTestInput); !reflect.DeepEqual(r, mapTestInput) || err != nil { + t.Fatal("TestStringMap failed") + } + + setTestInput := []int32{1, 2, 3} + if r, err := client.TestSet(defaultCtx, setTestInput); !reflect.DeepEqual(r, setTestInput) || err != nil { + t.Fatal("TestSet failed") + } + + listTest := []int32{1, 2, 3} + if r, err := client.TestList(defaultCtx, listTest); !reflect.DeepEqual(r, listTest) || err != nil { + t.Fatal("TestList failed") + } + + if r, err := client.TestEnum(defaultCtx, thrifttest.Numberz_ONE); r != thrifttest.Numberz_ONE || err != nil { + t.Fatal("TestEnum failed") + } + + if r, err := client.TestTypedef(defaultCtx, 69); r != 69 || err != nil { + t.Fatal("TestTypedef failed") + } + + mapMapTest := map[int32]map[int32]int32{ + 4: {1: 1, 2: 2, 3: 3, 4: 4}, + -4: {-4: -4, -3: -3, -2: -2, -1: -1}, + } + if r, err := client.TestMapMap(defaultCtx, 1); !reflect.DeepEqual(r, mapMapTest) || err != nil { + t.Fatal("TestMapMap failed") + } + + crazyX1 := thrifttest.NewXtruct() + crazyX1.StringThing = "Goodbye4" + crazyX1.ByteThing = 4 + crazyX1.I32Thing = 4 + crazyX1.I64Thing = 4 + + crazyX2 := thrifttest.NewXtruct() + crazyX2.StringThing = "Hello2" + crazyX2.ByteThing = 2 + crazyX2.I32Thing = 2 + crazyX2.I64Thing = 2 + + crazy := thrifttest.NewInsanity() + crazy.UserMap = map[thrifttest.Numberz]thrifttest.UserId{5: 5, 8: 8} + crazy.Xtructs = []*thrifttest.Xtruct{crazyX1, crazyX2} + + crazyEmpty := thrifttest.NewInsanity() + crazyEmpty.UserMap = map[thrifttest.Numberz]thrifttest.UserId{} + crazyEmpty.Xtructs = []*thrifttest.Xtruct{} + + insanity := map[thrifttest.UserId]map[thrifttest.Numberz]*thrifttest.Insanity{ + 1: {thrifttest.Numberz_TWO: crazy, thrifttest.Numberz_THREE: crazy}, + 2: {thrifttest.Numberz_SIX: crazyEmpty}, + } + if r, err := client.TestInsanity(defaultCtx, crazy); !reflect.DeepEqual(r, insanity) || err != nil { + t.Fatal("TestInsanity failed") + } + + if err := client.TestException(defaultCtx, "TException"); err == nil { + t.Fatal("TestException TException failed") + } + + if err, ok := client.TestException(defaultCtx, "Xception").(*thrifttest.Xception); ok == false || err == nil { + t.Fatal("TestException Xception failed") + } else if err.ErrorCode != 1001 || err.Message != "Xception" { + t.Fatal("TestException Xception failed") + } + + if err := client.TestException(defaultCtx, "no Exception"); err != nil { + t.Fatal("TestException no Exception failed") + } + + if err := client.TestOneway(defaultCtx, 0); err != nil { + t.Fatal("TestOneway failed") + } +} diff --git a/src/jaegertracing/thrift/lib/go/test/tests/thrifttest_handler.go b/src/jaegertracing/thrift/lib/go/test/tests/thrifttest_handler.go new file mode 100644 index 000000000..31b9ee23e --- /dev/null +++ b/src/jaegertracing/thrift/lib/go/test/tests/thrifttest_handler.go @@ -0,0 +1,210 @@ +/* + * 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 tests + +import ( + "context" + "errors" + "thrift" + "thrifttest" + "time" +) + +type SecondServiceHandler struct { +} + +func NewSecondServiceHandler() *SecondServiceHandler { + return &SecondServiceHandler{} +} + +func (p *SecondServiceHandler) BlahBlah(ctx context.Context) (err error) { + return nil +} + +func (p *SecondServiceHandler) SecondtestString(ctx context.Context, thing string) (r string, err error) { + return thing, nil +} + +type ThriftTestHandler struct { +} + +func NewThriftTestHandler() *ThriftTestHandler { + return &ThriftTestHandler{} +} + +func (p *ThriftTestHandler) TestVoid(ctx context.Context) (err error) { + return nil +} + +func (p *ThriftTestHandler) TestString(ctx context.Context, thing string) (r string, err error) { + return thing, nil +} + +func (p *ThriftTestHandler) TestBool(ctx context.Context, thing bool) (r bool, err error) { + return thing, nil +} + +func (p *ThriftTestHandler) TestByte(ctx context.Context, thing int8) (r int8, err error) { + return thing, nil +} + +func (p *ThriftTestHandler) TestI32(ctx context.Context, thing int32) (r int32, err error) { + return thing, nil +} + +func (p *ThriftTestHandler) TestI64(ctx context.Context, thing int64) (r int64, err error) { + return thing, nil +} + +func (p *ThriftTestHandler) TestDouble(ctx context.Context, thing float64) (r float64, err error) { + return thing, nil +} + +func (p *ThriftTestHandler) TestBinary(ctx context.Context, thing []byte) (r []byte, err error) { + return thing, nil +} + +func (p *ThriftTestHandler) TestStruct(ctx context.Context, thing *thrifttest.Xtruct) (r *thrifttest.Xtruct, err error) { + return thing, nil +} + +func (p *ThriftTestHandler) TestNest(ctx context.Context, thing *thrifttest.Xtruct2) (r *thrifttest.Xtruct2, err error) { + return thing, nil +} + +func (p *ThriftTestHandler) TestMap(ctx context.Context, thing map[int32]int32) (r map[int32]int32, err error) { + return thing, nil +} + +func (p *ThriftTestHandler) TestStringMap(ctx context.Context, thing map[string]string) (r map[string]string, err error) { + return thing, nil +} + +func (p *ThriftTestHandler) TestSet(ctx context.Context, thing []int32) (r []int32, err error) { + return thing, nil +} + +func (p *ThriftTestHandler) TestList(ctx context.Context, thing []int32) (r []int32, err error) { + return thing, nil +} + +func (p *ThriftTestHandler) TestEnum(ctx context.Context, thing thrifttest.Numberz) (r thrifttest.Numberz, err error) { + return thing, nil +} + +func (p *ThriftTestHandler) TestTypedef(ctx context.Context, thing thrifttest.UserId) (r thrifttest.UserId, err error) { + return thing, nil +} + +func (p *ThriftTestHandler) TestMapMap(ctx context.Context, hello int32) (r map[int32]map[int32]int32, err error) { + r = make(map[int32]map[int32]int32) + pos := make(map[int32]int32) + neg := make(map[int32]int32) + + for i := int32(1); i < 5; i++ { + pos[i] = i + neg[-i] = -i + } + r[4] = pos + r[-4] = neg + + return r, nil +} + +func (p *ThriftTestHandler) TestInsanity(ctx context.Context, argument *thrifttest.Insanity) (r map[thrifttest.UserId]map[thrifttest.Numberz]*thrifttest.Insanity, err error) { + hello := thrifttest.NewXtruct() + hello.StringThing = "Hello2" + hello.ByteThing = 2 + hello.I32Thing = 2 + hello.I64Thing = 2 + + goodbye := thrifttest.NewXtruct() + goodbye.StringThing = "Goodbye4" + goodbye.ByteThing = 4 + goodbye.I32Thing = 4 + goodbye.I64Thing = 4 + + crazy := thrifttest.NewInsanity() + crazy.UserMap = make(map[thrifttest.Numberz]thrifttest.UserId) + crazy.UserMap[thrifttest.Numberz_EIGHT] = 8 + crazy.UserMap[thrifttest.Numberz_FIVE] = 5 + crazy.Xtructs = []*thrifttest.Xtruct{goodbye, hello} + + first_map := make(map[thrifttest.Numberz]*thrifttest.Insanity) + second_map := make(map[thrifttest.Numberz]*thrifttest.Insanity) + + first_map[thrifttest.Numberz_TWO] = crazy + first_map[thrifttest.Numberz_THREE] = crazy + + looney := thrifttest.NewInsanity() + second_map[thrifttest.Numberz_SIX] = looney + + var insane = make(map[thrifttest.UserId]map[thrifttest.Numberz]*thrifttest.Insanity) + insane[1] = first_map + insane[2] = second_map + + return insane, nil +} + +func (p *ThriftTestHandler) TestMulti(ctx context.Context, arg0 int8, arg1 int32, arg2 int64, arg3 map[int16]string, arg4 thrifttest.Numberz, arg5 thrifttest.UserId) (r *thrifttest.Xtruct, err error) { + r = thrifttest.NewXtruct() + r.StringThing = "Hello2" + r.ByteThing = arg0 + r.I32Thing = arg1 + r.I64Thing = arg2 + return r, nil +} + +func (p *ThriftTestHandler) TestException(ctx context.Context, arg string) (err error) { + if arg == "Xception" { + x := thrifttest.NewXception() + x.ErrorCode = 1001 + x.Message = arg + return x + } else if arg == "TException" { + return thrift.TException(errors.New(arg)) + } else { + return nil + } +} + +func (p *ThriftTestHandler) TestMultiException(ctx context.Context, arg0 string, arg1 string) (r *thrifttest.Xtruct, err error) { + if arg0 == "Xception" { + x := thrifttest.NewXception() + x.ErrorCode = 1001 + x.Message = "This is an Xception" + return nil, x + } else if arg0 == "Xception2" { + x2 := thrifttest.NewXception2() + x2.ErrorCode = 2002 + x2.StructThing = thrifttest.NewXtruct() + x2.StructThing.StringThing = "This is an Xception2" + return nil, x2 + } + + res := thrifttest.NewXtruct() + res.StringThing = arg1 + return res, nil +} + +func (p *ThriftTestHandler) TestOneway(ctx context.Context, secondsToSleep int32) (err error) { + time.Sleep(time.Second * time.Duration(secondsToSleep)) + return nil +} diff --git a/src/jaegertracing/thrift/lib/go/test/tests/union_binary_test.go b/src/jaegertracing/thrift/lib/go/test/tests/union_binary_test.go new file mode 100644 index 000000000..bdae2cb92 --- /dev/null +++ b/src/jaegertracing/thrift/lib/go/test/tests/union_binary_test.go @@ -0,0 +1,36 @@ +/* + * 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 tests + +import ( + "testing" + "unionbinarytest" +) + + +// See https://issues.apache.org/jira/browse/THRIFT-4573 +func TestUnionBinary(t *testing.T) { + s := unionbinarytest.NewSample() + s.U1 = map[string]string{} + s.U2 = []byte{} + if n := s.CountSetFieldsSample(); n != 2 { + t.Errorf("Expected 2 set fields, got %d!", n) + } +} diff --git a/src/jaegertracing/thrift/lib/go/test/tests/union_default_value_test.go b/src/jaegertracing/thrift/lib/go/test/tests/union_default_value_test.go new file mode 100644 index 000000000..2dcbf4e44 --- /dev/null +++ b/src/jaegertracing/thrift/lib/go/test/tests/union_default_value_test.go @@ -0,0 +1,33 @@ +/* + * 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 tests + +import ( + "testing" + "uniondefaultvaluetest" +) + +func TestUnionDefaultValue(t *testing.T) { + s := uniondefaultvaluetest.NewTestStruct() + d := s.GetDescendant() + if d == nil { + t.Error("Default Union value not set!") + } +} |