Skip to content

Commit

Permalink
simtest
Browse files Browse the repository at this point in the history
  • Loading branch information
sangaman committed Aug 13, 2020
1 parent a3a70ac commit a8a91e6
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 2 deletions.
4 changes: 2 additions & 2 deletions test/integration/OrderBook.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -252,12 +252,12 @@ describe('nomatching OrderBook', () => {
orderBook['removeOwnOrder']({
orderId: remainingOrder!.id,
pairId: order.pairId,
quantityToRemove: remainingOrder!.quantity - 100
quantityToRemove: remainingOrder!.quantity - 100,
});
orderBook['removeOwnOrder']({
orderId: remainingOrder!.id,
pairId: order.pairId,
quantityToRemove: 100
quantityToRemove: 100,
});

expect(() => orderBook['removeOwnOrder']({
Expand Down
81 changes: 81 additions & 0 deletions test/simulation/tests-integration.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ var integrationTestCases = []*testCase{
name: "order matching and swap connext",
test: testOrderMatchingAndSwapConnext,
},
{
name: "order replacement",
test: testOrderReplacement,
},
{
name: "internal match and invalidation",
test: testInternalMatchAndInvalidation,
Expand Down Expand Up @@ -252,6 +256,83 @@ func testOrderMatchingAndSwap(net *xudtest.NetworkHarness, ht *harnessTest) {
ht.act.disconnect(net.Alice, net.Bob)
}

func testOrderReplacement(net *xudtest.NetworkHarness, ht *harnessTest) {
// Connect Alice to Bob.
ht.act.connect(net.Alice, net.Bob)
ht.act.verifyConnectivity(net.Alice, net.Bob)

var originalQuantity uint64 = 1000000
originalPrice := 0.02
var newQuantity uint64 = 2000000
newPrice := 0.03

// Place an order on Alice.
req := &xudrpc.PlaceOrderRequest{
OrderId: "maker_order_id",
Price: originalPrice,
Quantity: originalQuantity,
PairId: "LTC/BTC",
Side: xudrpc.OrderSide_BUY,
}
ht.act.placeOrderAndBroadcast(net.Alice, net.Bob, req)

// Subscribe to orders on Bob
bobOrderChan := subscribeOrders(ht.ctx, net.Bob)

// Replace the order on Alice
req = &xudrpc.PlaceOrderRequest{
ReplaceOrderId: "maker_order_id",
Price: newPrice,
Quantity: newQuantity,
PairId: req.PairId,
Side: req.Side,
}
res, err := net.Bob.Client.PlaceOrderSync(ht.ctx, req)
ht.assert.NoError(err)
ht.assert.Len(res.InternalMatches, 0)
ht.assert.Len(res.SwapSuccesses, 0)
ht.assert.Len(res.SwapFailures, 0)
ht.assert.NotNil(res.RemainingOrder)
ht.assert.True(res.RemainingOrder.IsOwnOrder)

// Retrieve and verify the removed order event on Bob.
e := <-bobOrderChan
ht.assert.NoError(e.err)
ht.assert.NotNil(e.orderUpdate)
orderRemoval := e.orderUpdate.GetOrderRemoval()
ht.assert.NotNil(orderRemoval)
ht.assert.Equal(orderRemoval.Quantity, originalQuantity)
ht.assert.Equal(orderRemoval.LocalId, "maker_order_id")
ht.assert.Equal(orderRemoval.PairId, req.PairId)
ht.assert.False(orderRemoval.IsOwnOrder)

// Retrieve and verify the added order event on Bob.
e = <-bobOrderChan
ht.assert.NoError(e.err)
ht.assert.NotNil(e.orderUpdate)
peerOrder := e.orderUpdate.GetOrder()
ht.assert.NotNil(peerOrder)

// Verify the peer order.
ht.assert.Equal(peerOrder.Price, newPrice)
ht.assert.Equal(peerOrder.PairId, req.PairId)
ht.assert.Equal(peerOrder.Quantity, newQuantity)
ht.assert.Equal(peerOrder.Side, req.Side)
ht.assert.False(peerOrder.IsOwnOrder)
ht.assert.Equal(peerOrder.NodeIdentifier.NodePubKey, net.Alice.PubKey())

// Verify that only the replaced order is in the order books
srcNodeCount, destNodeCount, err := getOrdersCount(ht.ctx, net.Alice, net.Bob)
ht.assert.NoError(err)
ht.assert.Equal(srcNodeCount.Own, 1)
ht.assert.Equal(srcNodeCount.Peer, 0)
ht.assert.Equal(destNodeCount.Own, 0)
ht.assert.Equal(destNodeCount.Peer, 1)

// Cleanup.
ht.act.disconnect(net.Alice, net.Bob)
}

func waitConnextReady(node *xudtest.HarnessNode) error {
isReady := func() bool {
info, err := node.Client.GetInfo(context.Background(), &xudrpc.GetInfoRequest{})
Expand Down

0 comments on commit a8a91e6

Please sign in to comment.