예제 #1
0
// 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)
}
예제 #2
0
func Test_InternalWaitress(t *testing.T) {
	w := dummy.NewInternalWaitress(time.Minute * 5)
	session, err := uuid.NewV4()
	assert.NoError(t, err)
	assert.NoError(t, w.WaitResult(*session, 1, 1))
	res, _, _, err := w.GetResult(*session, 1)
	assert.NoError(t, err)
	assert.Equal(t, uint32(1), res)
}
예제 #3
0
func Test_ProcessorAck1(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()

	ackMsgId1, _ := proc.NewMessageID(*session, time.Now(), 1)
	ackMsgId2, _ := proc.NewMessageID(*session, time.Now(), 1)
	reqMsgId, err := proc.NewMessageID(*session, time.Now(), 0)
	assert.NoError(t, (&mock.MsgsAck{[]int64{
		ackMsgId1,
		ackMsgId2,
	}}).Box().Write(reqEncoder))
	res := &testProcessorResult{}

	// Register two fake acks in waitress
	assert.NoError(t, proc.WaitAck(*session, ackMsgId1))
	assert.NoError(t, proc.WaitAck(*session, ackMsgId2))

	// Fire request
	err = proc.Process(tl.RequestContext{*conn, *session, reqMsgId, 2},
		reqEncoder.Bytes(), res)
	assert.NoError(t, err)

	assert.Empty(t, res.requests, "requests should be empty")
	assert.Empty(t, res.responses, "responses should be empty")

	// check acks in waitress
	ackRes1, err := proc.GetAck(*session, ackMsgId1)
	assert.NoError(t, err)
	assert.True(t, ackRes1)
	ackRes2, err := proc.GetAck(*session, ackMsgId2)
	assert.NoError(t, err)
	assert.True(t, ackRes2)
}
예제 #4
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)
}
예제 #5
0
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)
}