Exemple #1
0
// TestCase: Mismatching sequence id has been received in the client.
func TestClientSeqIdMismatch(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(),
		protocol.EXPECT().ReadMessageBegin().Return("testString", thrift.REPLY, int32(2), nil),
	)

	client := errortest.NewErrorTestClientProtocol(transport, protocol, protocol)
	_, err := client.TestString("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")
	}
}
Exemple #2
0
// TestCase: Wrong message type has been received in the client.
func TestClientWrongMessageType(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(),
		protocol.EXPECT().ReadMessageBegin().Return("testString", thrift.INVALID_TMESSAGE_TYPE, int32(1), nil),
	)

	client := errortest.NewErrorTestClientProtocol(transport, protocol, protocol)
	_, err := client.TestString("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")
	}
}
Exemple #3
0
// 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")
		mockCtrl.Finish()

		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
		}
	}
}
Exemple #4
0
// TestCase: Comprehensive call and reply workflow in the client.
// 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]bool)
	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(thing)
		mockCtrl.Finish()
		err2, ok := retErr.(thrift.TProtocolException)
		if !ok {
			t.Fatal("Expected a TProtocolException")
		}
		if err2.TypeId() != thrift.INVALID_DATA {
			t.Fatal("Expected INVALID_DATA error")
		}
	}
}
Exemple #5
0
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]bool)
	thing.I = 3

	err := thrift.NewTTransportException(thrift.TIMED_OUT, "test")
	for i := 0; ; i++ {
		protocol := NewMockTProtocol(mockCtrl)
		if !prepareClientProtocolFailure(protocol, i, err) {
			return
		}
		client := errortest.NewErrorTestClientProtocol(transport, protocol, protocol)
		_, retErr := client.TestStruct(thing)
		err2, ok := retErr.(thrift.TTransportException)
		if !ok {
			t.Fatal("Expected a TTrasportException")
		}

		if err2.TypeId() != err.TypeId() {
			t.Fatal("Expected a same error type id")
		}

		mockCtrl.Finish()
	}
}
Exemple #6
0
func formatError(fname string, seqId, exceptionId int32, err error) []byte {
	buffer := thrift.NewTMemoryBuffer()
	trans := thrift.NewTFramedTransport(buffer)
	oprot := thrift.NewTBinaryProtocolTransport(trans)
	a := thrift.NewTApplicationException(exceptionId, err.Error())
	oprot.WriteMessageBegin(fname, thrift.EXCEPTION, seqId)
	a.Write(oprot)
	oprot.WriteMessageEnd()
	oprot.Flush()
	return buffer.Bytes()
}
Exemple #7
0
func TestWireFormatWithSetPayload(t *testing.T) {
	var transport *thrift.TMemoryBuffer

	var protocols = []struct {
		name    string
		builder protocolBuilder
	}{
		{
			"TBinaryProtocol",
			func() thrift.TProtocol {
				return thrift.NewTBinaryProtocolTransport(transport)
			},
		},
		{
			"TCompactProtocol",
			func() thrift.TProtocol {
				return thrift.NewTCompactProtocol(transport)
			},
		},
		{
			"TJSONProtocol",
			func() thrift.TProtocol {
				return thrift.NewTJSONProtocol(transport)
			},
		},
		{
			"TSimpleJSONProtocol",
			func() thrift.TProtocol {
				return thrift.NewTSimpleJSONProtocol(transport)
			},
		},
	}

	for i, definition := range protocols {
		transport = thrift.NewTMemoryBuffer()
		defer transport.Close()
		protocol := definition.builder()
		name := definition.name

		emission := NewContainerOfEnums()
		emission.First = UndefinedTwo
		emission.Second = DefinedTwo
		emission.Third = HeterogeneousTwo
		emission.OptionalFourth = UndefinedThree
		emission.OptionalFifth = DefinedThree
		emission.OptionalSixth = HeterogeneousThree

		if err := emission.Write(protocol); err != nil {
			t.Fatalf("%d (%s): Could not emit simple %q to JSON.", i, name, emission)
		}

		if err := protocol.Flush(); err != nil {
			t.Fatalf("%d (%s): Could not flush emission.", i, name)
		}

		incoming := NewContainerOfEnums()

		if err := incoming.Read(protocol); err != nil {
			t.Fatalf("%d (%s): Could not read from buffer: %q", i, name, err)
		}

		if emission.First != incoming.First {
			t.Errorf("%d (%s) emission.First (%q) != incoming.First (%q)", i, name, emission.First, incoming.First)
		}

		if emission.Second != incoming.Second {
			t.Errorf("%d (%s) emission.Second (%q) != incoming.Second (%q)", i, name, emission.Second, incoming.Second)
		}

		if emission.Third != incoming.Third {
			t.Errorf("%d (%s) emission.Third (%q) != incoming.Third (%q)", i, name, emission.Third, incoming.Third)
		}

		if emission.OptionalFourth != incoming.OptionalFourth {
			t.Errorf("%d (%s) emission.OptionalFourth (%q) != incoming.OptionalFourth (%q)", i, name, emission.OptionalFourth, incoming.OptionalFourth)
		}

		if emission.OptionalFifth != incoming.OptionalFifth {
			t.Errorf("%d (%s) emission.OptionalFifth (%q) != incoming.OptionalFifth (%q)", i, name, emission.OptionalFifth, incoming.OptionalFifth)
		}

		if emission.OptionalSixth != incoming.OptionalSixth {
			t.Errorf("%d (%s) emission.OptionalSixth (%q) != incoming.OptionalSixth (%q)", i, name, emission.OptionalSixth, incoming.OptionalSixth)
		}

		if emission.DefaultSeventh != incoming.DefaultSeventh {
			t.Errorf("%d (%s) emission.DefaultSeventh (%q) != incoming.DefaultSeventh (%q)", i, name, emission.DefaultSeventh, incoming.DefaultSeventh)
		}

		if emission.DefaultEighth != incoming.DefaultEighth {
			t.Errorf("%d (%s) emission.DefaultEighth (%q) != incoming.DefaultEighth (%q)", i, name, emission.DefaultEighth, incoming.DefaultEighth)
		}

		if emission.DefaultNineth != incoming.DefaultNineth {
			t.Errorf("%d (%s) emission.DefaultNineth (%q) != incoming.DefaultNineth (%q)", i, name, emission.DefaultNineth, incoming.DefaultNineth)
		}

		if emission.IsSetOptionalFourth() != incoming.IsSetOptionalFourth() {
			t.Errorf("%d (%s) emission.IsSetOptionalFourth (%q) != incoming.IsSetOptionalFourth (%q)", i, name, emission.IsSetOptionalFourth(), incoming.IsSetOptionalFourth())
		}

		if emission.IsSetOptionalFifth() != incoming.IsSetOptionalFifth() {
			t.Errorf("%d (%s) emission.IsSetOptionalFifth (%q) != incoming.IsSetOptionalFifth (%q)", i, name, emission.IsSetOptionalFifth(), incoming.IsSetOptionalFifth())
		}

		if emission.IsSetOptionalSixth() != incoming.IsSetOptionalSixth() {
			t.Errorf("%d (%s) emission.IsSetOptionalSixth (%q) != incoming.IsSetOptionalSixth (%q)", i, name, emission.IsSetOptionalSixth(), incoming.IsSetOptionalSixth())
		}

		if emission.IsSetDefaultSeventh() != incoming.IsSetDefaultSeventh() {
			t.Errorf("%d (%s) emission.IsSetDefaultSeventh (%q) != incoming.IsSetDefaultSeventh (%q)", i, name, emission.IsSetDefaultSeventh(), incoming.IsSetDefaultSeventh())
		}

		if emission.IsSetDefaultEighth() != incoming.IsSetDefaultEighth() {
			t.Errorf("%d (%s) emission.IsSetDefaultEighth (%q) != incoming.IsSetDefaultEighth (%q)", i, name, emission.IsSetDefaultEighth(), incoming.IsSetDefaultEighth())
		}

		if emission.IsSetDefaultNineth() != incoming.IsSetDefaultNineth() {
			t.Errorf("%d (%s) emission.IsSetDefaultNineth (%q) != incoming.IsSetDefaultNineth (%q)", i, name, emission.IsSetDefaultNineth(), incoming.IsSetDefaultNineth())
		}
	}
}