func TestNewMarketConnectorNewOrderRequest(t *testing.T) { //config sc := common.NewConfig() sc.MessageBusURL = "nats://localhost:22222" sc.DatabaseDriver = "memstore" //start MC svc := NewMarketConnector(sc) svc.Start() time.Sleep(100 * time.Millisecond) // mock a NewOrderRequest, really should come from OrderRouter request := &proto.NewOrderRequest{ Order: testOrder.MockOrder(), } request.Order.OrderStatus = proto.OrderStatus_ORDER_RECEIVED request.Order.SubmitDatetime = time.Now().UTC().Format(time.RFC3339Nano) request.Order.MessageType = proto.Order_NEW request.Order.OrderKey = int32(321) svc.app.OrderStore.OrderCreate(request.Order) // subscribe to check if we can receive a fill execution report from sim broker recvExecutionReport := false svc.app.MessageBus.Subscribe("order.Execution", func(m *messagebus.Msg) { recvExecutionReport = true exec := new(proto.Execution) if err := exec.Unmarshal(m.Data); err == nil { if exec.ClientOrderId != "321.1" { t.Fatalf("unexpected execution report ClOrdId %v, expecting 321.1", exec.ClientOrderId) } if exec.OrderId != 1 { t.Fatalf("unexpected execution report OrderId %v, expecting 1", exec.OrderId) } } else { t.Fatalf("unexpected execution report: %v", err) } }) // send above mock NewOrderRequest data, _ := request.Marshal() svc.app.MessageBus.Publish("order.NewOrderRequest.MC."+MarketConnectorName, data) time.Sleep(100 * time.Millisecond) //stop and disconnect MC svc.Close() if recvExecutionReport == false { t.Fatal("No execution report received from mock order new request") } }
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) } } } }
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) } } } }