func Test_ReadLong(t *testing.T) { data := []byte{0x24, 0x46, 0x49, 0x3c, 0x47, 0x5, 0x2, 0x0} var val int64 = 568753465673252 proto := bin.NewDecoder(bytes.NewBuffer(data)) res, err := proto.ReadLong() assert.NoError(t, err) assert.Equal(t, res, val) }
func Test_ReadTypeBegin(t *testing.T) { data := []byte{0x7f, 0x2, 0xe2, 0x5c} var val uint32 = 0x5ce2027f proto := bin.NewDecoder(bytes.NewBuffer(data)) res, err := proto.ReadTypeBegin() assert.NoError(t, err) assert.Equal(t, res, val) }
func Test_ReadBytesShort(t *testing.T) { var val string = strings.Repeat("test", 31) var data []byte = []byte{0x7c, 0x74, 0x65, 0x73, 0x74, 0x74, 0x65, 0x73, 0x74, 0x74, 0x65, 0x73, 0x74, 0x74, 0x65, 0x73, 0x74, 0x74, 0x65, 0x73, 0x74, 0x74, 0x65, 0x73, 0x74, 0x74, 0x65, 0x73, 0x74, 0x74, 0x65, 0x73, 0x74, 0x74, 0x65, 0x73, 0x74, 0x74, 0x65, 0x73, 0x74, 0x74, 0x65, 0x73, 0x74, 0x74, 0x65, 0x73, 0x74, 0x74, 0x65, 0x73, 0x74, 0x74, 0x65, 0x73, 0x74, 0x74, 0x65, 0x73, 0x74, 0x74, 0x65, 0x73, 0x74, 0x74, 0x65, 0x73, 0x74, 0x74, 0x65, 0x73, 0x74, 0x74, 0x65, 0x73, 0x74, 0x74, 0x65, 0x73, 0x74, 0x74, 0x65, 0x73, 0x74, 0x74, 0x65, 0x73, 0x74, 0x74, 0x65, 0x73, 0x74, 0x74, 0x65, 0x73, 0x74, 0x74, 0x65, 0x73, 0x74, 0x74, 0x65, 0x73, 0x74, 0x74, 0x65, 0x73, 0x74, 0x74, 0x65, 0x73, 0x74, 0x74, 0x65, 0x73, 0x74, 0x74, 0x65, 0x73, 0x74, 0x74, 0x65, 0x73, 0x74, 0x0, 0x0, 0x0} proto := bin.NewDecoder(bytes.NewBuffer(data)) res, err := proto.ReadBytes() assert.NoError(t, err) assert.Equal(t, val, string(res)) }
func Test_ReadStringLong(t *testing.T) { var val string = strings.Repeat("tes", 101) var data []byte = []byte{0xfe, 0x2f, 0x1, 0x0, 0x74, 0x65, 0x73, 0x74, 0x65, 0x73, 0x74, 0x65, 0x73, 0x74, 0x65, 0x73, 0x74, 0x65, 0x73, 0x74, 0x65, 0x73, 0x74, 0x65, 0x73, 0x74, 0x65, 0x73, 0x74, 0x65, 0x73, 0x74, 0x65, 0x73, 0x74, 0x65, 0x73, 0x74, 0x65, 0x73, 0x74, 0x65, 0x73, 0x74, 0x65, 0x73, 0x74, 0x65, 0x73, 0x74, 0x65, 0x73, 0x74, 0x65, 0x73, 0x74, 0x65, 0x73, 0x74, 0x65, 0x73, 0x74, 0x65, 0x73, 0x74, 0x65, 0x73, 0x74, 0x65, 0x73, 0x74, 0x65, 0x73, 0x74, 0x65, 0x73, 0x74, 0x65, 0x73, 0x74, 0x65, 0x73, 0x74, 0x65, 0x73, 0x74, 0x65, 0x73, 0x74, 0x65, 0x73, 0x74, 0x65, 0x73, 0x74, 0x65, 0x73, 0x74, 0x65, 0x73, 0x74, 0x65, 0x73, 0x74, 0x65, 0x73, 0x74, 0x65, 0x73, 0x74, 0x65, 0x73, 0x74, 0x65, 0x73, 0x74, 0x65, 0x73, 0x74, 0x65, 0x73, 0x74, 0x65, 0x73, 0x74, 0x65, 0x73, 0x74, 0x65, 0x73, 0x74, 0x65, 0x73, 0x74, 0x65, 0x73, 0x74, 0x65, 0x73, 0x74, 0x65, 0x73, 0x74, 0x65, 0x73, 0x74, 0x65, 0x73, 0x74, 0x65, 0x73, 0x74, 0x65, 0x73, 0x74, 0x65, 0x73, 0x74, 0x65, 0x73, 0x74, 0x65, 0x73, 0x74, 0x65, 0x73, 0x74, 0x65, 0x73, 0x74, 0x65, 0x73, 0x74, 0x65, 0x73, 0x74, 0x65, 0x73, 0x74, 0x65, 0x73, 0x74, 0x65, 0x73, 0x74, 0x65, 0x73, 0x74, 0x65, 0x73, 0x74, 0x65, 0x73, 0x74, 0x65, 0x73, 0x74, 0x65, 0x73, 0x74, 0x65, 0x73, 0x74, 0x65, 0x73, 0x74, 0x65, 0x73, 0x74, 0x65, 0x73, 0x74, 0x65, 0x73, 0x74, 0x65, 0x73, 0x74, 0x65, 0x73, 0x74, 0x65, 0x73, 0x74, 0x65, 0x73, 0x74, 0x65, 0x73, 0x74, 0x65, 0x73, 0x74, 0x65, 0x73, 0x74, 0x65, 0x73, 0x74, 0x65, 0x73, 0x74, 0x65, 0x73, 0x74, 0x65, 0x73, 0x74, 0x65, 0x73, 0x74, 0x65, 0x73, 0x74, 0x65, 0x73, 0x74, 0x65, 0x73, 0x74, 0x65, 0x73, 0x74, 0x65, 0x73, 0x74, 0x65, 0x73, 0x74, 0x65, 0x73, 0x74, 0x65, 0x73, 0x74, 0x65, 0x73, 0x74, 0x65, 0x73, 0x74, 0x65, 0x73, 0x74, 0x65, 0x73, 0x74, 0x65, 0x73, 0x74, 0x65, 0x73, 0x74, 0x65, 0x73, 0x74, 0x65, 0x73, 0x74, 0x65, 0x73, 0x74, 0x65, 0x73, 0x74, 0x65, 0x73, 0x0} proto := bin.NewDecoder(bytes.NewBuffer(data)) res, err := proto.ReadString() assert.NoError(t, err) assert.Equal(t, val, res) }
func Test_ReadDouble(t *testing.T) { data := []byte{0xbf, 0x7d, 0x1d, 0x38, 0x77, 0x37, 0xb6, 0x40} var val float64 = 5687.4657 proto := bin.NewDecoder(bytes.NewBuffer(data)) res, err := proto.ReadDouble() assert.NoError(t, err) assert.Equal(t, res, val) }
func Test_ReadInt(t *testing.T) { data := []byte{0x59, 0x1, 0x0, 0x0} var val int32 = 345 proto := bin.NewDecoder(bytes.NewBuffer(data)) res, err := proto.ReadInt() assert.NoError(t, err) assert.Equal(t, res, val) }
func Test_BytesBoxRead(t *testing.T) { data := []byte{0x9f, 0x37, 0xe1, 0xe, 0x4, 0x74, 0x65, 0x73, 0x74, 0x0, 0x0, 0x0} val := builtin.NewBytes([]byte("test")) proto := bin.NewDecoder(bytes.NewBuffer(data)) res := (&builtin.Bytes{}).Box() assert.NoError(t, res.Read(proto, nil, uuid.UUID{})) assert.Equal(t, val, res.Strip()) }
func Test_Bytes_Read(t *testing.T) { data := []byte{0x4, 0x74, 0x65, 0x73, 0x74, 0x0, 0x0, 0x0} val := []byte("test") proto := bin.NewDecoder(bytes.NewBuffer(data)) res := &builtin.Bytes{} assert.NoError(t, res.Read(proto, nil, uuid.UUID{})) assert.Equal(t, val, res.Strip().([]byte)) }
func Test_IntBoxRead(t *testing.T) { data := []byte{0xda, 0x9b, 0x50, 0xa8, 0xda, 0xbc, 0x34, 0x13} val := builtin.NewInt(322223322) proto := bin.NewDecoder(bytes.NewBuffer(data)) res := (&builtin.Int{}).Box() assert.NoError(t, res.Read(proto, nil, uuid.UUID{})) assert.Equal(t, val, res.Strip()) }
func Test_Int_Read(t *testing.T) { data := []byte{0xda, 0xbc, 0x34, 0x13} val := int32(322223322) proto := bin.NewDecoder(bytes.NewBuffer(data)) res := &builtin.Int{} assert.NoError(t, res.Read(proto, nil, uuid.UUID{})) assert.Equal(t, val, res.Strip()) }
func Test_LongBoxRead(t *testing.T) { data := []byte{0xba, 0x6c, 0x7, 0x22, 0xda, 0xbc, 0x34, 0x13, 0x0, 0x0, 0x0, 0x0} val := builtin.NewLong(322223322) proto := bin.NewDecoder(bytes.NewBuffer(data)) res := (&builtin.Long{}).Box() assert.NoError(t, res.Read(proto, nil, uuid.UUID{})) assert.Equal(t, val, res.Strip()) }
func Test_DoubleBoxRead(t *testing.T) { data := []byte{0x54, 0xc1, 0x10, 0x22, 0xef, 0xc9, 0xc3, 0x42, 0x95, 0x77, 0xdf, 0x40} val := builtin.NewDouble(32222.3322) proto := bin.NewDecoder(bytes.NewBuffer(data)) res := (&builtin.Double{}).Box() assert.NoError(t, res.Read(proto, nil, uuid.UUID{})) assert.Equal(t, val, res.Strip()) }
func ReadCheck(t *testing.T, expect tl.Bare, data []byte) { empty := expect.Box().Empty() box := empty.(tl.Box) proto := bin.NewDecoder(bytes.NewBuffer(data)) assert.NoError(t, box.Read(proto, nil, uuid.UUID{})) bare, err := box.Bare() assert.NoError(t, err) assert.EqualValues(t, expect, bare) }
func Test_Read_SingleMultiIntR4_BadN(t *testing.T) { expect := &mock.SingleMultiInt32R4{} data := []byte{ 0xf9, 0x6e, 0xac, 0x3a, 0x3, 0x0, 0x0, 0x0, 0x4, 0x0, 0x0, 0x0} proto := bin.NewDecoder(bytes.NewBuffer(data)) err := expect.Box().Read(proto, nil, uuid.UUID{}) assert.Error(t, err) }
func Test_StringBoxRead(t *testing.T) { data := []byte{0x24, 0x6e, 0x28, 0xb5, 0x4, 0x74, 0x65, 0x73, 0x74, 0x0, 0x0, 0x0} val := builtin.NewString("test") proto := bin.NewDecoder(bytes.NewBuffer(data)) res := (&builtin.String{}).Box() assert.NoError(t, res.Read(proto, nil, uuid.UUID{})) assert.Equal(t, val, res.Strip()) }
// Try to read IntBox func Test_ObjectBox_Read(t *testing.T) { data := []byte{0xda, 0x9b, 0x50, 0xa8, 0xda, 0xbc, 0x34, 0x13} o := builtin.ObjectBox{} proto := bin.NewDecoder(bytes.NewBuffer(data)) err := o.Read(proto, nil, uuid.UUID{}) assert.NoError(t, err) bare, err := o.Bare() assert.NoError(t, err) assert.NotNil(t, bare) assert.IsType(t, &builtin.Int{}, bare) assert.IsType(t, &builtin.IntBox{}, bare.Box()) }
// Test request handling with RPCResult. func Test_Processor_WithRPCResult(t *testing.T) { proc := mock.NewProcessorFactory( &TestHandler{}, bin.NewEncoderFactory(), bin.NewDecoderFactory(), dummy.NewInternalWaitress(time.Minute*5), tl.NewBaseIDBrokerFactory(0), ).GetProcessor(context.TODO(), 10) // Prepare request reqEncoder := bin.NewEncoder(new(bytes.Buffer)) conn, _ := uuid.NewV4() session, _ := uuid.NewV4() reqMsgId, _ := proc.NewMessageID(*session, time.Now(), 0) assert.NoError(t, (&mock.FuncEmpty{}).Box().Write(reqEncoder)) res := &testProcessorResult{} // Fire request err := proc.Process(tl.RequestContext{*conn, *session, reqMsgId, 3}, reqEncoder.Bytes(), res) assert.NoError(t, err) assert.Equal(t, len(res.requests), 0, "Requests should be zero") assert.Equal(t, len(res.responses), 2, "Responses should be one ack and one RpcResult") // Check ack response checkAck(t, proc, res.responses[0].ResponseContext, res.responses[0].data) // check result with temporary waitress resWaitress := dummy.NewInternalWaitress(time.Minute * 5) assert.NoError(t, resWaitress.WaitResult( *session, reqMsgId, mock.TL_ID_FUNC_EMPTY_RES)) resResCtx, resMsgData := res.responses[1].ResponseContext, res.responses[1].data assert.EqualValues(t, 5, resResCtx.SeqNo) assert.EqualValues(t, 1, resResCtx.MessageID&1) resBox := &builtin.ObjectBox{} assert.NoError(t, resBox.Read(bin.NewDecoder(bytes.NewBuffer(resMsgData)), resWaitress, *session)) resBare, err := resBox.Bare() assert.NoError(t, err) assert.EqualValues(t, reqMsgId, resBare.(*builtin.RpcResult).ReqMsgId) // Waitress should wait for one ack waitressRes, err := proc.GetAck(*session, resResCtx.MessageID) assert.NoError(t, err) assert.False(t, waitressRes) }
func checkAck( t *testing.T, waitress tl.Waitress, ackMsgCtx tl.ResponseContext, ackMsgData []byte, ) { reqMsgId := ackMsgCtx.Request.MessageID session := ackMsgCtx.Request.Session assert.EqualValues(t, 0, ackMsgCtx.SeqNo%4, "ack SeqNo %% 4 remainder should be 0 but %d", ackMsgCtx.SeqNo%4) assert.EqualValues(t, 1, ackMsgCtx.MessageID&1, "ack MessageID & 1 should be 1 but it %d", ackMsgCtx.MessageID&1) ackBox := &builtin.ObjectBox{} assert.NoError(t, ackBox.Read( bin.NewDecoder(bytes.NewBuffer(ackMsgData)), waitress, session)) ackBare, err := ackBox.Bare() assert.NoError(t, err) assert.EqualValues(t, reqMsgId, ackBare.(*mock.MsgsAck).MsgIds[0]) }
func Test_Processor_MessageContaitner(t *testing.T) { proc := mock.NewProcessorFactory( &TestHandler{}, bin.NewEncoderFactory(), bin.NewDecoderFactory(), dummy.NewInternalWaitress(time.Minute*5), tl.NewBaseIDBrokerFactory(0), ).GetProcessor(context.TODO(), 10) // Prepare request reqEncoder := bin.NewEncoder(new(bytes.Buffer)) conn, _ := uuid.NewV4() session, _ := uuid.NewV4() reqMsgId, _ := proc.NewMessageID(*session, time.Now(), 0) assert.NoError(t, (&mock.MsgContainer{ []mock.Message{ mock.Message{34, 3, 67, *(builtin.NewObject(&mock.FuncEmpty{}))}, }}).Box().Write(reqEncoder)) res := &testProcessorResult{} // fire err := proc.Process( tl.RequestContext{*conn, *session, reqMsgId, 2}, reqEncoder.Bytes(), res) assert.NoError(t, err) assert.Empty(t, res.responses, "responses should be empty") // Check requests // check result resResCtx, resMsgData := res.requests[0].RequestContext, res.requests[0].data assert.EqualValues(t, 3, resResCtx.SeqNo) assert.EqualValues(t, 34, 34) resBox := &builtin.ObjectBox{} assert.NoError(t, resBox.Read(bin.NewDecoder(bytes.NewBuffer(resMsgData)), proc, *session)) resBare, err := resBox.Bare() assert.NoError(t, err) assert.EqualValues(t, &mock.FuncEmpty{}, resBare) }
func Test_RPCResultBox_Read1(t *testing.T) { waitress := dummy.NewInternalWaitress(time.Minute) data := []byte{ 0x1, 0x6d, 0x5c, 0xf3, 0x59, 0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x15, 0xc4, 0xb5, 0x1c, 0x2, 0x0, 0x0, 0x0, 0x22, 0x0, 0x0, 0x0, 0x23, 0x0, 0x0, 0x0} bind_uuid, err := uuid.NewV4() assert.NoError(t, err) assert.NoError(t, waitress.WaitResult(*bind_uuid, 345, TL_ID_MOCK_RPC_RESULT)) decoder := bin.NewDecoder(bytes.NewBuffer(data)) res := &builtin.RpcResultBox{} assert.NoError(t, res.Read(decoder, waitress, *bind_uuid)) assert.Equal(t, (&builtin.RpcResult{ ReqMsgId: 345, Result: &MockRPCResult{[]int32{34, 35}}, }).Box(), res) }