func (bw *Reader) readStrictNameType(initial int32, off int64) (wire.Envelope, int64, error) { var e wire.Envelope if v := uint32(initial) & versionMask; v != version1 { return e, off, fmt.Errorf("cannot decode envelope of version: %v", v) } // This will truncate the bits that are not required. e.Type = wire.EnvelopeType(initial) var err error e.Name, off, err = bw.readString(off) return e, off, err }
func (bw *Reader) readNonStrictNameType() (wire.Envelope, int64, error) { var e wire.Envelope name, off, err := bw.readString(0) if err != nil { return e, off, err } e.Name = name typeID, off, err := bw.readByte(off) if err != nil { return e, off, err } e.Type = wire.EnvelopeType(typeID) return e, off, nil }
func TestClient(t *testing.T) { mockCtrl := gomock.NewController(t) defer mockCtrl.Finish() internalError := exception.ExceptionTypeInternalError unknownMethod := exception.ExceptionTypeUnknownMethod tests := []struct { desc string // Either transportError or decode* can be set. transportError error decodeEnvelope wire.Envelope decodeError error wantError error // expected error if any }{ { desc: "nothing went wrong", decodeEnvelope: wire.Envelope{ Name: "hello", Type: wire.Reply, SeqID: 1, Value: wire.NewValueStruct(wire.Struct{}), }, }, { desc: "decode error", decodeError: errors.New("great sadness"), wantError: errors.New("great sadness"), }, { desc: "internal error", decodeEnvelope: wire.Envelope{ Name: "hello", Type: wire.Exception, SeqID: 1, Value: wire.NewValueStruct(wire.Struct{Fields: []wire.Field{ {ID: 1, Value: wire.NewValueString("great sadness")}, {ID: 2, Value: wire.NewValueI32(6)}, // Internal error }}), }, wantError: &exception.TApplicationException{ Message: ptr.String("great sadness"), Type: &internalError, }, }, { desc: "unknown method", decodeEnvelope: wire.Envelope{ Name: "hello", Type: wire.Exception, SeqID: 1, Value: wire.NewValueStruct(wire.Struct{Fields: []wire.Field{ {ID: 1, Value: wire.NewValueString(`unknown method "hello"`)}, {ID: 2, Value: wire.NewValueI32(1)}, // Internal error }}), }, wantError: &exception.TApplicationException{ Message: ptr.String(`unknown method "hello"`), Type: &unknownMethod, }, }, { desc: "unknown envelope type", decodeEnvelope: wire.Envelope{ Name: "hello", Type: wire.EnvelopeType(12), SeqID: 1, Value: wire.NewValueStruct(wire.Struct{}), }, wantError: errUnknownEnvelopeType(12), }, { desc: "transport error", transportError: errors.New("great sadness"), wantError: errors.New("great sadness"), }, } for _, tt := range tests { proto := NewMockProtocol(mockCtrl) proto.EXPECT().EncodeEnveloped( wire.Envelope{ Name: "hello", Type: wire.Call, SeqID: 1, Value: wire.NewValueStruct(wire.Struct{Fields: []wire.Field{ {ID: 1, Value: wire.NewValueString("world")}, }}), }, gomock.Any(), ).Do(func(_ wire.Envelope, w io.Writer) { _, err := w.Write([]byte{1, 2, 3}) assert.NoError(t, err, tt.desc) }).Return(nil) transport := envelopetest.NewMockTransport(mockCtrl) if tt.transportError != nil { transport.EXPECT().Send([]byte{1, 2, 3}).Return(nil, tt.transportError) } else { transport.EXPECT().Send([]byte{1, 2, 3}).Return([]byte{4, 5, 6}, nil) proto.EXPECT().DecodeEnveloped(gomock.Any()). Return(tt.decodeEnvelope, tt.decodeError) } client := NewClient(proto, transport) _, err := client.Send("hello", wire.NewValueStruct(wire.Struct{Fields: []wire.Field{ {ID: 1, Value: wire.NewValueString("world")}, }})) assert.Equal(t, tt.wantError, err) } }
func (e errUnknownEnvelopeType) Error() string { return fmt.Sprintf("unknown envelope type: expected Reply, got %v", wire.EnvelopeType(e)) }
func (e errUnexpectedEnvelopeType) Error() string { return fmt.Sprintf("unexpected envelope type: %v", wire.EnvelopeType(e)) }
func (e fakeEnveloper) EnvelopeType() wire.EnvelopeType { return wire.EnvelopeType(e) }