예제 #1
0
func BenchmarkTradeflow(b *testing.B) {
	b.StopTimer()

	// Mock new order request
	request := &proto.NewOrderRequest{
		Order: testOrder.MockOrder(),
	}
	request.Order.MarketConnector = "Simulator"
	data, merr := request.Marshal()
	if merr != nil {
		b.Fatal(merr)
	}

	////////
	// benchmark the whole journey of:
	// CL->OR:   Client send order protobuf to OrderRouter(OR)
	// OR->MC:   OrderRouter process order and dispatch persisted order entity or target MarketConnector
	// MC->FIX:  MarketConnector translate into NewOrderSingle FIX message based on the session with its counterparty
	// FIX->MC:  MarketConnector received FIX message on its order, here Simulator sending a fully FILL execution
	// EXEC->CL: MarketConnector publish processed and persisted Execution onto messaging bus, here our Client will listen to
	///////
	log.Println("benchmark count: ", b.N)
	b.StartTimer()
	for i := 0; i < b.N; i++ {
		if reply, err := msgbus.Request("order.NewOrderRequest", data, 2000*time.Millisecond); err != nil {
			b.Fatalf("an error '%s' was not expected when sending NewOrderRequest", err)
		} else {
			resp := new(proto.NewOrderResponse)
			if err := resp.Unmarshal(reply.Data); err != nil {
				b.Fatalf("an error '%s' was not expected when decoding NewOrderResponse", err)
			}

			if resp.ErrorMessage != nil && len(*resp.ErrorMessage) > 0 {
				b.Fatalf("unexpected NewOrderResponse error message: %s", *resp.ErrorMessage)
			}
		}
	}
}
예제 #2
0
func TestOrderRouterNewOrderRequest(t *testing.T) {

	svc := mockOrderRouter()
	svc.Start()
	defer svc.Close()

	// mock new order request
	request := &proto.NewOrderRequest{
		Order: testOrder.MockOrder(),
	}
	data, merr := request.Marshal()
	if merr != nil {
		t.Fatal(merr)
	}

	// send mock order, expecting reject due to market connector not up
	req := new(proto.NewOrderRequest)
	if err := req.Unmarshal(data); err != nil {
		t.Fatal(err)
	}
	if reply, err := svc.msgbus.Request("order.NewOrderRequest", data, 200*time.Millisecond); err != nil {
		t.Fatalf("an error '%s' was not expected when sending NewOrderRequest", err)
	} else {
		resp := new(proto.NewOrderResponse)
		if err := resp.Unmarshal(reply.Data); err != nil {
			t.Fatalf("an error '%s' was not expected when decoding NewOrderResponse", err)
		}

		if *resp.ErrorMessage != "LINK TO BROKER DOWN" {
			t.Fatalf("unexpected NewOrderResponse %s, expecting LINK TO BROKER DOWN", *resp.ErrorMessage)
		}
	}

	// mock a test market connector
	mcname := "MC.TestCase"
	hbMsg := pService.Heartbeat{
		Name:   mcname,
		Status: pService.RUNNING,
	}
	if hbMsgData, err := hbMsg.Marshal(); err != nil {
		t.Fatal(err)
	} else {
		svc.msgbusService.Publish("service.Heartbeat.MC.TestCase", hbMsgData)

		time.Sleep(100 * time.Millisecond)

		if reply, err := svc.msgbus.Request("order.NewOrderRequest", data, 200*time.Millisecond); err != nil {
			t.Fatalf("an error '%s' was not expected when sending NewOrderRequest", err)
		} else {
			resp := new(proto.NewOrderResponse)
			if err := resp.Unmarshal(reply.Data); err != nil {
				t.Fatalf("an error '%s' was not expected when decoding NewOrderResponse", err)
			}

			if resp.ErrorMessage != nil && len(*resp.ErrorMessage) > 0 {
				t.Fatalf("unexpected NewOrderResponse error message: %s", *resp.ErrorMessage)
			}

			if resp.Order == nil || resp.Order.OrderId != 2 {
				t.Fatalf("unexpected NewOrderResponse id not matching mock OrderId(%v), expecting 2")
			}

			if resp.Order == nil || resp.Order.OrderKey != 2 {
				t.Fatalf("unexpected NewOrderResponse OrderKey %v, expecting 2", resp.Order.OrderKey)
			}
		}
	}
}