github.com/m3db/m3@v1.5.0/src/dbnode/encoding/proto/encoder_test.go (about) 1 // Copyright (c) 2019 Uber Technologies, Inc. 2 // 3 // Permission is hereby granted, free of charge, to any person obtaining a copy 4 // of this software and associated documentation files (the "Software"), to deal 5 // in the Software without restriction, including without limitation the rights 6 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 // copies of the Software, and to permit persons to whom the Software is 8 // furnished to do so, subject to the following conditions: 9 // 10 // The above copyright notice and this permission notice shall be included in 11 // all copies or substantial portions of the Software. 12 // 13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 // THE SOFTWARE. 20 21 package proto 22 23 import ( 24 "testing" 25 "time" 26 27 "github.com/m3db/m3/src/dbnode/namespace" 28 "github.com/m3db/m3/src/dbnode/ts" 29 "github.com/m3db/m3/src/x/context" 30 xtime "github.com/m3db/m3/src/x/time" 31 32 dpb "github.com/golang/protobuf/protoc-gen-go/descriptor" 33 "github.com/jhump/protoreflect/desc" 34 "github.com/stretchr/testify/require" 35 ) 36 37 func TestCustomAndProtoFields(t *testing.T) { 38 testCases := []struct { 39 schema *desc.MessageDescriptor 40 expectedCustomFields []customFieldState 41 expectedNonCustomFields []marshalledField 42 }{ 43 { 44 schema: newVLMessageDescriptor(), 45 expectedCustomFields: []customFieldState{ 46 // latitude 47 { 48 fieldNum: 1, 49 fieldType: float64Field, 50 protoFieldType: dpb.FieldDescriptorProto_TYPE_DOUBLE, 51 }, 52 // longitude 53 { 54 fieldNum: 2, 55 fieldType: float64Field, 56 protoFieldType: dpb.FieldDescriptorProto_TYPE_DOUBLE, 57 }, 58 // numTrips 59 { 60 fieldNum: 3, 61 fieldType: signedInt64Field, 62 protoFieldType: dpb.FieldDescriptorProto_TYPE_INT64, 63 }, 64 // deliveryID 65 { 66 fieldNum: 4, 67 fieldType: bytesField, 68 protoFieldType: dpb.FieldDescriptorProto_TYPE_BYTES, 69 }, 70 }, 71 expectedNonCustomFields: []marshalledField{{fieldNum: 5}}, 72 }, 73 } 74 75 for _, tc := range testCases { 76 tszFields, nonCustomFields := customAndNonCustomFields(nil, nil, tc.schema) 77 require.Equal(t, tc.expectedCustomFields, tszFields) 78 require.Equal(t, tc.expectedNonCustomFields, nonCustomFields) 79 } 80 } 81 82 func TestClosedEncoderIsNotUsable(t *testing.T) { 83 enc := newTestEncoder(xtime.Now().Truncate(time.Second)) 84 enc.Close() 85 86 err := enc.Encode(ts.Datapoint{}, xtime.Second, nil) 87 require.Equal(t, errEncoderClosed, err) 88 89 _, err = enc.LastEncoded() 90 require.Equal(t, errEncoderClosed, err) 91 } 92 93 func TestEncoderIsNotCorruptedByInvalidWrites(t *testing.T) { 94 ctx := context.NewBackground() 95 defer ctx.Close() 96 97 start := xtime.Now().Truncate(time.Second) 98 enc := newTestEncoder(start) 99 enc.SetSchema(namespace.GetTestSchemaDescr(testVLSchema)) 100 101 vl := newVL(1.0, 2.0, 3, []byte("some-delivery-id"), nil) 102 vlBytes, err := vl.Marshal() 103 require.NoError(t, err) 104 105 dp := ts.Datapoint{TimestampNanos: start.Add(time.Second)} 106 err = enc.Encode(dp, xtime.Second, vlBytes) 107 require.NoError(t, err) 108 109 bytesBeforeBadWrite := getCurrEncoderBytes(ctx, t, enc) 110 111 dp = ts.Datapoint{TimestampNanos: start.Add(2 * time.Second)} 112 err = enc.Encode(dp, xtime.Second, []byte("not-valid-proto")) 113 require.Error(t, err) 114 115 bytesAfterBadWrite := getCurrEncoderBytes(ctx, t, enc) 116 // Ensure that the encoder detect that the protobuf message was invalid 117 // before writing any data. 118 require.Equal(t, bytesBeforeBadWrite, bytesAfterBadWrite) 119 } 120 121 func getCurrEncoderBytes(ctx context.Context, t *testing.T, enc *Encoder) []byte { 122 stream, ok := enc.Stream(ctx) 123 require.True(t, ok) 124 125 currSeg, err := stream.Segment() 126 require.NoError(t, err) 127 128 result := append([]byte(nil), currSeg.Head.Bytes()...) 129 if currSeg.Tail != nil { 130 result = append(result, currSeg.Tail.Bytes()...) 131 } 132 return result 133 }