github.com/matrixorigin/matrixone@v1.2.0/pkg/common/morpc/codec_header_test.go (about) 1 // Copyright 2021 - 2022 Matrix Origin 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package morpc 16 17 import ( 18 "context" 19 "sync/atomic" 20 "testing" 21 "time" 22 23 "github.com/fagongzi/goetty/v2/buf" 24 "github.com/matrixorigin/matrixone/pkg/txn/clock" 25 "github.com/matrixorigin/matrixone/pkg/util/trace" 26 "github.com/stretchr/testify/assert" 27 ) 28 29 func TestEncodeContext(t *testing.T) { 30 hc := &deadlineContextCodec{} 31 out := buf.NewByteBuf(8) 32 33 c, cancel := context.WithTimeout(context.Background(), time.Second) 34 defer cancel() 35 n, err := hc.Encode(&RPCMessage{Ctx: c}, out) 36 assert.Equal(t, 8, n) 37 assert.NoError(t, err) 38 assert.Equal(t, 8, out.GetWriteIndex()) 39 } 40 41 func TestDecodeContext(t *testing.T) { 42 hc := &deadlineContextCodec{} 43 v := buf.Int64ToBytes(int64(time.Second)) 44 msg := &RPCMessage{} 45 n, err := hc.Decode(msg, v) 46 assert.NoError(t, err) 47 assert.Equal(t, 8, n) 48 assert.NotNil(t, msg.Ctx) 49 ts, ok := msg.Ctx.Deadline() 50 assert.True(t, ok) 51 assert.True(t, !ts.IsZero()) 52 } 53 54 func TestEncodeAndDecodeTrace(t *testing.T) { 55 hc := &traceCodec{} 56 out := buf.NewByteBuf(8) 57 span := trace.SpanContextWithIDs(trace.TraceID{}, trace.SpanID{}) 58 n, err := hc.Encode(&RPCMessage{Ctx: trace.ContextWithSpanContext(context.Background(), span)}, out) 59 assert.Equal(t, 1+span.Size(), n) 60 assert.NoError(t, err) 61 62 msg := &RPCMessage{} 63 _, data := out.ReadBytes(1 + span.Size()) 64 65 n, err = hc.Decode(msg, nil) 66 assert.Equal(t, 0, n) 67 assert.Error(t, err) 68 69 n, err = hc.Decode(msg, data[:1]) 70 assert.Equal(t, 0, n) 71 assert.Error(t, err) 72 73 n, err = hc.Decode(msg, data) 74 assert.Equal(t, 1+span.Size(), n) 75 assert.NoError(t, err) 76 77 span.Kind = trace.SpanKindRemote 78 assert.Equal(t, span, trace.SpanFromContext(msg.Ctx).SpanContext()) 79 } 80 81 func TestEncodeAndDecodeClock(t *testing.T) { 82 var n1, n2 int64 83 f1 := func() int64 { 84 return atomic.LoadInt64(&n1) 85 } 86 f2 := func() int64 { 87 return atomic.LoadInt64(&n2) 88 } 89 c1 := &hlcCodec{clock: clock.NewHLCClock(f1, 0)} 90 c2 := &hlcCodec{clock: clock.NewHLCClock(f2, 0)} 91 92 n1 = 1 93 n2 = 2 94 95 out := buf.NewByteBuf(8) 96 n, err := c1.Encode(nil, out) 97 assert.NoError(t, err) 98 assert.Equal(t, 12, n) 99 100 _, data := out.ReadBytes(out.Readable()) 101 n, err = c2.Decode(nil, data) 102 assert.NoError(t, err) 103 assert.Equal(t, 12, n) 104 105 now, _ := c2.clock.Now() 106 assert.Equal(t, n2, now.PhysicalTime) 107 }