gorealis v2 refactor (#5)

* Changing default timeout for start maintenance.

* Upgrading dependencies to gorealis v2 and thrift  0.12.0

* Refactored to update to gorealis v2.
This commit is contained in:
Renan DelValle 2018-12-27 11:31:51 -08:00 committed by GitHub
parent ad4dd9606e
commit 6ab5c9334d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
1335 changed files with 137431 additions and 61530 deletions

View file

@ -20,11 +20,13 @@
package tests
import (
"github.com/golang/mock/gomock"
"context"
"errors"
"errortest"
"testing"
"thrift"
"github.com/golang/mock/gomock"
)
// TestCase: Comprehensive call and reply workflow in the client.
@ -211,7 +213,7 @@ func prepareClientCallReply(protocol *MockTProtocol, failAt int, failWith error)
if failAt == 25 {
err = failWith
}
last = protocol.EXPECT().Flush().Return(err).After(last)
last = protocol.EXPECT().Flush(context.Background()).Return(err).After(last)
if failAt == 25 {
return true
}
@ -397,12 +399,44 @@ func prepareClientCallReply(protocol *MockTProtocol, failAt int, failWith error)
// Expecting TTransportError on fail.
func TestClientReportTTransportErrors(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(map[string]struct{})
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")
@ -412,8 +446,9 @@ func TestClientReportTTransportErrors(t *testing.T) {
return
}
client := errortest.NewErrorTestClientProtocol(transport, protocol, protocol)
_, retErr := client.TestStruct(thing)
_, retErr := client.TestStruct(defaultCtx, thing)
mockCtrl.Finish()
mockCtrl = gomock.NewController(t)
err2, ok := retErr.(thrift.TTransportException)
if !ok {
t.Fatal("Expected a TTrasportException")
@ -429,12 +464,43 @@ func TestClientReportTTransportErrors(t *testing.T) {
// Expecting TTProtocolErrors on fail.
func TestClientReportTProtocolErrors(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(map[string]struct{})
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"))
@ -444,8 +510,9 @@ func TestClientReportTProtocolErrors(t *testing.T) {
return
}
client := errortest.NewErrorTestClientProtocol(transport, protocol, protocol)
_, retErr := client.TestStruct(thing)
_, retErr := client.TestStruct(defaultCtx, thing)
mockCtrl.Finish()
mockCtrl = gomock.NewController(t)
err2, ok := retErr.(thrift.TProtocolException)
if !ok {
t.Fatal("Expected a TProtocolException")
@ -470,7 +537,7 @@ func prepareClientCallException(protocol *MockTProtocol, failAt int, failWith er
last = protocol.EXPECT().WriteFieldStop().After(last)
last = protocol.EXPECT().WriteStructEnd().After(last)
last = protocol.EXPECT().WriteMessageEnd().After(last)
last = protocol.EXPECT().Flush().After(last)
last = protocol.EXPECT().Flush(context.Background()).After(last)
// Reading the exception, might fail.
if failAt == 0 {
@ -557,16 +624,52 @@ func prepareClientCallException(protocol *MockTProtocol, failAt int, failWith er
// TestCase: call and reply with exception workflow in the client.
func TestClientCallException(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("test")
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)
@ -591,6 +694,36 @@ func TestClientCallException(t *testing.T) {
// 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)
@ -603,12 +736,12 @@ func TestClientSeqIdMismatch(t *testing.T) {
protocol.EXPECT().WriteFieldStop(),
protocol.EXPECT().WriteStructEnd(),
protocol.EXPECT().WriteMessageEnd(),
protocol.EXPECT().Flush(),
protocol.EXPECT().Flush(context.Background()),
protocol.EXPECT().ReadMessageBegin().Return("testString", thrift.REPLY, int32(2), nil),
)
client := errortest.NewErrorTestClientProtocol(transport, protocol, protocol)
_, err := client.TestString("test")
_, err := client.TestString(defaultCtx, "test")
mockCtrl.Finish()
appErr, ok := err.(thrift.TApplicationException)
if !ok {
@ -621,6 +754,36 @@ func TestClientSeqIdMismatch(t *testing.T) {
// 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)
@ -633,12 +796,12 @@ func TestClientWrongMethodName(t *testing.T) {
protocol.EXPECT().WriteFieldStop(),
protocol.EXPECT().WriteStructEnd(),
protocol.EXPECT().WriteMessageEnd(),
protocol.EXPECT().Flush(),
protocol.EXPECT().Flush(context.Background()),
protocol.EXPECT().ReadMessageBegin().Return("unknown", thrift.REPLY, int32(1), nil),
)
client := errortest.NewErrorTestClientProtocol(transport, protocol, protocol)
_, err := client.TestString("test")
_, err := client.TestString(defaultCtx, "test")
mockCtrl.Finish()
appErr, ok := err.(thrift.TApplicationException)
if !ok {
@ -651,6 +814,36 @@ func TestClientWrongMethodName(t *testing.T) {
// 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)
@ -663,12 +856,12 @@ func TestClientWrongMessageType(t *testing.T) {
protocol.EXPECT().WriteFieldStop(),
protocol.EXPECT().WriteStructEnd(),
protocol.EXPECT().WriteMessageEnd(),
protocol.EXPECT().Flush(),
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("test")
_, err := client.TestString(defaultCtx, "test")
mockCtrl.Finish()
appErr, ok := err.(thrift.TApplicationException)
if !ok {

View file

@ -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()

View file

@ -20,6 +20,7 @@
package tests
import (
"context"
"multiplexedprotocoltest"
"net"
"testing"
@ -38,24 +39,32 @@ func FindAvailableTCPServerPort() net.Addr {
type FirstImpl struct{}
func (f *FirstImpl) ReturnOne() (r int64, err error) {
func (f *FirstImpl) ReturnOne(ctx context.Context) (r int64, err error) {
return 1, nil
}
type SecondImpl struct{}
func (s *SecondImpl) ReturnTwo() (r int64, err error) {
func (s *SecondImpl) ReturnTwo(ctx context.Context) (r int64, err error) {
return 2, nil
}
var processor = thrift.NewTMultiplexedProcessor()
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 TestInitTwoServers(t *testing.T) {
var err error
func TestMultiplexedProtocolFirst(t *testing.T) {
processor := thrift.NewTMultiplexedProcessor()
protocolFactory := thrift.NewTBinaryProtocolFactoryDefault()
transportFactory := thrift.NewTTransportFactory()
transportFactory = thrift.NewTFramedTransportFactory(transportFactory)
addr = FindAvailableTCPServerPort()
addr := FindAvailableTCPServerPort()
serverTransport, err := thrift.NewTServerSocketTimeout(addr.String(), TIMEOUT)
if err != nil {
t.Fatal("Unable to create server socket", err)
@ -68,82 +77,117 @@ func TestInitTwoServers(t *testing.T) {
secondProcessor := multiplexedprotocoltest.NewSecondProcessor(&SecondImpl{})
processor.RegisterProcessor("SecondService", secondProcessor)
defer server.Stop()
go server.Serve()
time.Sleep(10 * time.Millisecond)
}
var firstClient *multiplexedprotocoltest.FirstClient
func TestInitClient1(t *testing.T) {
socket := thrift.NewTSocketFromAddrTimeout(addr, TIMEOUT)
transport := thrift.NewTFramedTransport(socket)
var protocol thrift.TProtocol = thrift.NewTBinaryProtocolTransport(transport)
protocol = thrift.NewTMultiplexedProtocol(protocol, "FirstService")
firstClient = multiplexedprotocoltest.NewFirstClientProtocol(transport, protocol, protocol)
err := transport.Open()
transport, err := createTransport(addr)
if err != nil {
t.Fatal("Unable to open client socket", err)
t.Fatal(err)
}
}
defer transport.Close()
protocol := thrift.NewTMultiplexedProtocol(thrift.NewTBinaryProtocolTransport(transport), "FirstService")
var secondClient *multiplexedprotocoltest.SecondClient
client := multiplexedprotocoltest.NewFirstClient(thrift.NewTStandardClient(protocol, protocol))
func TestInitClient2(t *testing.T) {
socket := thrift.NewTSocketFromAddrTimeout(addr, TIMEOUT)
transport := thrift.NewTFramedTransport(socket)
var protocol thrift.TProtocol = thrift.NewTBinaryProtocolTransport(transport)
protocol = thrift.NewTMultiplexedProtocol(protocol, "SecondService")
secondClient = multiplexedprotocoltest.NewSecondClientProtocol(transport, protocol, protocol)
err := transport.Open()
if err != nil {
t.Fatal("Unable to open client socket", err)
}
}
//create client without service prefix
func createLegacyClient(t *testing.T) *multiplexedprotocoltest.SecondClient {
socket := thrift.NewTSocketFromAddrTimeout(addr, TIMEOUT)
transport := thrift.NewTFramedTransport(socket)
var protocol thrift.TProtocol = thrift.NewTBinaryProtocolTransport(transport)
legacyClient := multiplexedprotocoltest.NewSecondClientProtocol(transport, protocol, protocol)
err := transport.Open()
if err != nil {
t.Fatal("Unable to open client socket", err)
}
return legacyClient
}
func TestCallFirst(t *testing.T) {
ret, err := firstClient.ReturnOne()
ret, err := client.ReturnOne(defaultCtx)
if err != nil {
t.Fatal("Unable to call first server:", err)
}
if ret != 1 {
} else if ret != 1 {
t.Fatal("Unexpected result from server: ", ret)
}
}
func TestCallSecond(t *testing.T) {
ret, err := secondClient.ReturnTwo()
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)
}
if ret != 2 {
} else if ret != 2 {
t.Fatal("Unexpected result from server: ", ret)
}
}
func TestCallLegacy(t *testing.T) {
legacyClient := createLegacyClient(t)
ret, err := legacyClient.ReturnTwo()
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{}))
legacyClient = createLegacyClient(t)
ret, err = legacyClient.ReturnTwo()
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)
}
@ -151,9 +195,3 @@ func TestCallLegacy(t *testing.T) {
t.Fatal("Unexpected result from server: ", ret)
}
}
func TestShutdownServerAndClients(t *testing.T) {
firstClient.Transport.Close()
secondClient.Transport.Close()
server.Stop()
}

View file

@ -20,6 +20,7 @@
package tests
import (
"context"
"fmt"
"net"
"onewaytest"
@ -39,9 +40,9 @@ func findPort() net.Addr {
type impl struct{}
func (i *impl) Hi(in int64, s string) (err error) { fmt.Println("Hi!"); return }
func (i *impl) Emptyfunc() (err error) { return }
func (i *impl) EchoInt(param int64) (r int64, err error) { return param, nil }
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
@ -66,7 +67,7 @@ func TestInitOneway(t *testing.T) {
func TestInitOnewayClient(t *testing.T) {
transport := thrift.NewTSocketFromAddrTimeout(addr, TIMEOUT)
protocol := thrift.NewTBinaryProtocolTransport(transport)
client = onewaytest.NewOneWayClientProtocol(transport, protocol, protocol)
client = onewaytest.NewOneWayClient(thrift.NewTStandardClient(protocol, protocol))
err := transport.Open()
if err != nil {
t.Fatal("Unable to open client socket", err)
@ -75,12 +76,12 @@ func TestInitOnewayClient(t *testing.T) {
func TestCallOnewayServer(t *testing.T) {
//call oneway function
err := client.Hi(1, "")
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(42)
i, err := client.EchoInt(defaultCtx, 42)
if err != nil {
t.Fatal("Unexpected error: ", err)
}

View file

@ -23,7 +23,9 @@
package tests
import (
"context"
thrift "thrift"
gomock "github.com/golang/mock/gomock"
)
@ -48,13 +50,13 @@ func (_m *MockTProtocol) EXPECT() *_MockTProtocolRecorder {
return _m.recorder
}
func (_m *MockTProtocol) Flush() error {
func (_m *MockTProtocol) Flush(ctx context.Context) error {
ret := _m.ctrl.Call(_m, "Flush")
ret0, _ := ret[0].(error)
return ret0
}
func (_mr *_MockTProtocolRecorder) Flush() *gomock.Call {
func (_mr *_MockTProtocolRecorder) Flush(ctx context.Context) *gomock.Call {
return _mr.mock.ctrl.RecordCall(_mr.mock, "Flush")
}

View file

@ -47,7 +47,7 @@ func RunSocketTestSuite(t *testing.T, protocolFactory thrift.TProtocolFactory,
t.Fatal(err)
}
var protocol thrift.TProtocol = protocolFactory.GetProtocol(transport)
thriftTestClient := thrifttest.NewThriftTestClientProtocol(transport, protocol, protocol)
thriftTestClient := thrifttest.NewThriftTestClient(thrift.NewTStandardClient(protocol, protocol))
err = transport.Open()
if err != nil {
t.Fatal("Unable to open client socket", err)

View file

@ -20,12 +20,45 @@
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)
@ -40,6 +73,7 @@ func TestStructReadRequiredFields(t *testing.T) {
err := testStruct.Read(protocol)
mockCtrl.Finish()
mockCtrl = gomock.NewController(t)
if err == nil {
t.Fatal("Expected read to fail")
}
@ -63,6 +97,7 @@ func TestStructReadRequiredFields(t *testing.T) {
err = testStruct.Read(protocol)
mockCtrl.Finish()
mockCtrl = gomock.NewController(t)
if err == nil {
t.Fatal("Expected read to fail")
}

View file

@ -30,7 +30,7 @@ func staticCheckStructArgsResults() {
var iface st.AServ
var err error
sa, err = iface.StructAFunc_1structA(sa)
sa, err = iface.StructAFunc_1structA(defaultCtx, sa)
_ = err
_ = sa
}

View file

@ -38,15 +38,15 @@ func (p *ThriftTestDriver) Start() {
client := p.client
t := p.t
if client.TestVoid() != nil {
if client.TestVoid(defaultCtx) != nil {
t.Fatal("TestVoid failed")
}
if r, err := client.TestString("Test"); r != "Test" || err != nil {
if r, err := client.TestString(defaultCtx, "Test"); r != "Test" || err != nil {
t.Fatal("TestString with simple text failed")
}
if r, err := client.TestString(""); r != "" || err != nil {
if r, err := client.TestString(defaultCtx, ""); r != "" || err != nil {
t.Fatal("TestString with empty text failed")
}
@ -76,7 +76,7 @@ func (p *ThriftTestDriver) Start() {
"Walon, Winaray, 吴语, isiXhosa, ייִדיש, Yorùbá, Zeêuws, 中文, " +
"Bân-lâm-gú, 粵語"
if r, err := client.TestString(stringTest); r != stringTest || err != nil {
if r, err := client.TestString(defaultCtx, stringTest); r != stringTest || err != nil {
t.Fatal("TestString with all languages failed")
}
@ -86,44 +86,44 @@ func (p *ThriftTestDriver) Start() {
" now-a-bunch-of-junk: !@#$%&()(&%$#{}{}<><><" +
" char-to-test-json-parsing: ]] \"]] \\\" }}}{ [[[ "
if r, err := client.TestString(specialCharacters); r != specialCharacters || err != nil {
if r, err := client.TestString(defaultCtx, specialCharacters); r != specialCharacters || err != nil {
t.Fatal("TestString with specialCharacters failed")
}
if r, err := client.TestByte(1); r != 1 || err != nil {
if r, err := client.TestByte(defaultCtx, 1); r != 1 || err != nil {
t.Fatal("TestByte(1) failed")
}
if r, err := client.TestByte(0); r != 0 || err != nil {
if r, err := client.TestByte(defaultCtx, 0); r != 0 || err != nil {
t.Fatal("TestByte(0) failed")
}
if r, err := client.TestByte(-1); r != -1 || err != nil {
if r, err := client.TestByte(defaultCtx, -1); r != -1 || err != nil {
t.Fatal("TestByte(-1) failed")
}
if r, err := client.TestByte(-127); r != -127 || err != nil {
if r, err := client.TestByte(defaultCtx, -127); r != -127 || err != nil {
t.Fatal("TestByte(-127) failed")
}
if r, err := client.TestI32(-1); r != -1 || err != nil {
if r, err := client.TestI32(defaultCtx, -1); r != -1 || err != nil {
t.Fatal("TestI32(-1) failed")
}
if r, err := client.TestI32(1); r != 1 || err != nil {
if r, err := client.TestI32(defaultCtx, 1); r != 1 || err != nil {
t.Fatal("TestI32(1) failed")
}
if r, err := client.TestI64(-5); r != -5 || err != nil {
if r, err := client.TestI64(defaultCtx, -5); r != -5 || err != nil {
t.Fatal("TestI64(-5) failed")
}
if r, err := client.TestI64(5); r != 5 || err != nil {
if r, err := client.TestI64(defaultCtx, 5); r != 5 || err != nil {
t.Fatal("TestI64(5) failed")
}
if r, err := client.TestI64(-34359738368); r != -34359738368 || err != nil {
if r, err := client.TestI64(defaultCtx, -34359738368); r != -34359738368 || err != nil {
t.Fatal("TestI64(-34359738368) failed")
}
if r, err := client.TestDouble(-5.2098523); r != -5.2098523 || err != nil {
if r, err := client.TestDouble(defaultCtx, -5.2098523); r != -5.2098523 || err != nil {
t.Fatal("TestDouble(-5.2098523) failed")
}
if r, err := client.TestDouble(-7.012052175215044); r != -7.012052175215044 || err != nil {
if r, err := client.TestDouble(defaultCtx, -7.012052175215044); r != -7.012052175215044 || err != nil {
t.Fatal("TestDouble(-7.012052175215044) failed")
}
@ -134,7 +134,7 @@ func (p *ThriftTestDriver) Start() {
out.ByteThing = 1
out.I32Thing = -3
out.I64Thing = 1000000
if r, err := client.TestStruct(out); !reflect.DeepEqual(r, out) || err != nil {
if r, err := client.TestStruct(defaultCtx, out); !reflect.DeepEqual(r, out) || err != nil {
t.Fatal("TestStruct failed")
}
@ -142,7 +142,7 @@ func (p *ThriftTestDriver) Start() {
out2.ByteThing = 1
out2.StructThing = out
out2.I32Thing = 5
if r, err := client.TestNest(out2); !reflect.DeepEqual(r, out2) || err != nil {
if r, err := client.TestNest(defaultCtx, out2); !reflect.DeepEqual(r, out2) || err != nil {
t.Fatal("TestNest failed")
}
@ -150,7 +150,7 @@ func (p *ThriftTestDriver) Start() {
for i := int32(0); i < 5; i++ {
mapout[i] = i - 10
}
if r, err := client.TestMap(mapout); !reflect.DeepEqual(r, mapout) || err != nil {
if r, err := client.TestMap(defaultCtx, mapout); !reflect.DeepEqual(r, mapout) || err != nil {
t.Fatal("TestMap failed")
}
@ -158,25 +158,25 @@ func (p *ThriftTestDriver) Start() {
"a": "123", "a b": "with spaces ", "same": "same", "0": "numeric key",
"longValue": stringTest, stringTest: "long key",
}
if r, err := client.TestStringMap(mapTestInput); !reflect.DeepEqual(r, mapTestInput) || err != nil {
if r, err := client.TestStringMap(defaultCtx, mapTestInput); !reflect.DeepEqual(r, mapTestInput) || err != nil {
t.Fatal("TestStringMap failed")
}
setTestInput := map[int32]struct{}{1: {}, 2: {}, 3: {}}
if r, err := client.TestSet(setTestInput); !reflect.DeepEqual(r, setTestInput) || err != nil {
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(listTest); !reflect.DeepEqual(r, listTest) || err != nil {
if r, err := client.TestList(defaultCtx, listTest); !reflect.DeepEqual(r, listTest) || err != nil {
t.Fatal("TestList failed")
}
if r, err := client.TestEnum(thrifttest.Numberz_ONE); r != thrifttest.Numberz_ONE || err != nil {
if r, err := client.TestEnum(defaultCtx, thrifttest.Numberz_ONE); r != thrifttest.Numberz_ONE || err != nil {
t.Fatal("TestEnum failed")
}
if r, err := client.TestTypedef(69); r != 69 || err != nil {
if r, err := client.TestTypedef(defaultCtx, 69); r != 69 || err != nil {
t.Fatal("TestTypedef failed")
}
@ -184,7 +184,7 @@ func (p *ThriftTestDriver) Start() {
4: {1: 1, 2: 2, 3: 3, 4: 4},
-4: {-4: -4, -3: -3, -2: -2, -1: -1},
}
if r, err := client.TestMapMap(1); !reflect.DeepEqual(r, mapMapTest) || err != nil {
if r, err := client.TestMapMap(defaultCtx, 1); !reflect.DeepEqual(r, mapMapTest) || err != nil {
t.Fatal("TestMapMap failed")
}
@ -212,25 +212,25 @@ func (p *ThriftTestDriver) Start() {
1: {thrifttest.Numberz_TWO: crazy, thrifttest.Numberz_THREE: crazy},
2: {thrifttest.Numberz_SIX: crazyEmpty},
}
if r, err := client.TestInsanity(crazy); !reflect.DeepEqual(r, insanity) || err != nil {
if r, err := client.TestInsanity(defaultCtx, crazy); !reflect.DeepEqual(r, insanity) || err != nil {
t.Fatal("TestInsanity failed")
}
if err := client.TestException("TException"); err == nil {
if err := client.TestException(defaultCtx, "TException"); err == nil {
t.Fatal("TestException TException failed")
}
if err, ok := client.TestException("Xception").(*thrifttest.Xception); ok == false || err == nil {
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("no Exception"); err != nil {
if err := client.TestException(defaultCtx, "no Exception"); err != nil {
t.Fatal("TestException no Exception failed")
}
if err := client.TestOneway(0); err != nil {
if err := client.TestOneway(defaultCtx, 0); err != nil {
t.Fatal("TestOneway failed")
}
}

View file

@ -20,6 +20,7 @@
package tests
import (
"context"
"errors"
"thrift"
"thrifttest"
@ -33,11 +34,11 @@ func NewSecondServiceHandler() *SecondServiceHandler {
return &SecondServiceHandler{}
}
func (p *SecondServiceHandler) BlahBlah() (err error) {
func (p *SecondServiceHandler) BlahBlah(ctx context.Context) (err error) {
return nil
}
func (p *SecondServiceHandler) SecondtestString(thing string) (r string, err error) {
func (p *SecondServiceHandler) SecondtestString(ctx context.Context, thing string) (r string, err error) {
return thing, nil
}
@ -48,71 +49,71 @@ func NewThriftTestHandler() *ThriftTestHandler {
return &ThriftTestHandler{}
}
func (p *ThriftTestHandler) TestVoid() (err error) {
func (p *ThriftTestHandler) TestVoid(ctx context.Context) (err error) {
return nil
}
func (p *ThriftTestHandler) TestString(thing string) (r string, err error) {
func (p *ThriftTestHandler) TestString(ctx context.Context, thing string) (r string, err error) {
return thing, nil
}
func (p *ThriftTestHandler) TestBool(thing bool) (r bool, err error) {
func (p *ThriftTestHandler) TestBool(ctx context.Context, thing bool) (r bool, err error) {
return thing, nil
}
func (p *ThriftTestHandler) TestByte(thing int8) (r int8, err error) {
func (p *ThriftTestHandler) TestByte(ctx context.Context, thing int8) (r int8, err error) {
return thing, nil
}
func (p *ThriftTestHandler) TestI32(thing int32) (r int32, err error) {
func (p *ThriftTestHandler) TestI32(ctx context.Context, thing int32) (r int32, err error) {
return thing, nil
}
func (p *ThriftTestHandler) TestI64(thing int64) (r int64, err error) {
func (p *ThriftTestHandler) TestI64(ctx context.Context, thing int64) (r int64, err error) {
return thing, nil
}
func (p *ThriftTestHandler) TestDouble(thing float64) (r float64, err error) {
func (p *ThriftTestHandler) TestDouble(ctx context.Context, thing float64) (r float64, err error) {
return thing, nil
}
func (p *ThriftTestHandler) TestBinary(thing []byte) (r []byte, err error) {
func (p *ThriftTestHandler) TestBinary(ctx context.Context, thing []byte) (r []byte, err error) {
return thing, nil
}
func (p *ThriftTestHandler) TestStruct(thing *thrifttest.Xtruct) (r *thrifttest.Xtruct, err error) {
func (p *ThriftTestHandler) TestStruct(ctx context.Context, thing *thrifttest.Xtruct) (r *thrifttest.Xtruct, err error) {
return thing, nil
}
func (p *ThriftTestHandler) TestNest(thing *thrifttest.Xtruct2) (r *thrifttest.Xtruct2, err error) {
func (p *ThriftTestHandler) TestNest(ctx context.Context, thing *thrifttest.Xtruct2) (r *thrifttest.Xtruct2, err error) {
return thing, nil
}
func (p *ThriftTestHandler) TestMap(thing map[int32]int32) (r map[int32]int32, err error) {
func (p *ThriftTestHandler) TestMap(ctx context.Context, thing map[int32]int32) (r map[int32]int32, err error) {
return thing, nil
}
func (p *ThriftTestHandler) TestStringMap(thing map[string]string) (r map[string]string, err error) {
func (p *ThriftTestHandler) TestStringMap(ctx context.Context, thing map[string]string) (r map[string]string, err error) {
return thing, nil
}
func (p *ThriftTestHandler) TestSet(thing map[int32]struct{}) (r map[int32]struct{}, err error) {
func (p *ThriftTestHandler) TestSet(ctx context.Context, thing []int32) (r []int32, err error) {
return thing, nil
}
func (p *ThriftTestHandler) TestList(thing []int32) (r []int32, err error) {
func (p *ThriftTestHandler) TestList(ctx context.Context, thing []int32) (r []int32, err error) {
return thing, nil
}
func (p *ThriftTestHandler) TestEnum(thing thrifttest.Numberz) (r thrifttest.Numberz, err error) {
func (p *ThriftTestHandler) TestEnum(ctx context.Context, thing thrifttest.Numberz) (r thrifttest.Numberz, err error) {
return thing, nil
}
func (p *ThriftTestHandler) TestTypedef(thing thrifttest.UserId) (r thrifttest.UserId, err error) {
func (p *ThriftTestHandler) TestTypedef(ctx context.Context, thing thrifttest.UserId) (r thrifttest.UserId, err error) {
return thing, nil
}
func (p *ThriftTestHandler) TestMapMap(hello int32) (r map[int32]map[int32]int32, err error) {
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)
@ -127,7 +128,7 @@ func (p *ThriftTestHandler) TestMapMap(hello int32) (r map[int32]map[int32]int32
return r, nil
}
func (p *ThriftTestHandler) TestInsanity(argument *thrifttest.Insanity) (r map[thrifttest.UserId]map[thrifttest.Numberz]*thrifttest.Insanity, err error) {
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
@ -162,7 +163,7 @@ func (p *ThriftTestHandler) TestInsanity(argument *thrifttest.Insanity) (r map[t
return insane, nil
}
func (p *ThriftTestHandler) TestMulti(arg0 int8, arg1 int32, arg2 int64, arg3 map[int16]string, arg4 thrifttest.Numberz, arg5 thrifttest.UserId) (r *thrifttest.Xtruct, err error) {
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
@ -171,7 +172,7 @@ func (p *ThriftTestHandler) TestMulti(arg0 int8, arg1 int32, arg2 int64, arg3 ma
return r, nil
}
func (p *ThriftTestHandler) TestException(arg string) (err error) {
func (p *ThriftTestHandler) TestException(ctx context.Context, arg string) (err error) {
if arg == "Xception" {
x := thrifttest.NewXception()
x.ErrorCode = 1001
@ -184,7 +185,7 @@ func (p *ThriftTestHandler) TestException(arg string) (err error) {
}
}
func (p *ThriftTestHandler) TestMultiException(arg0 string, arg1 string) (r *thrifttest.Xtruct, err error) {
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
@ -203,7 +204,7 @@ func (p *ThriftTestHandler) TestMultiException(arg0 string, arg1 string) (r *thr
return res, nil
}
func (p *ThriftTestHandler) TestOneway(secondsToSleep int32) (err error) {
func (p *ThriftTestHandler) TestOneway(ctx context.Context, secondsToSleep int32) (err error) {
time.Sleep(time.Second * time.Duration(secondsToSleep))
return nil
}

View file

@ -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)
}
}