code.vegaprotocol.io/vega@v0.79.0/datanode/sqlsubscribers/trades_test.go (about)

     1  // Copyright (C) 2023 Gobalsky Labs Limited
     2  //
     3  // This program is free software: you can redistribute it and/or modify
     4  // it under the terms of the GNU Affero General Public License as
     5  // published by the Free Software Foundation, either version 3 of the
     6  // License, or (at your option) any later version.
     7  //
     8  // This program is distributed in the hope that it will be useful,
     9  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    10  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    11  // GNU Affero General Public License for more details.
    12  //
    13  // You should have received a copy of the GNU Affero General Public License
    14  // along with this program.  If not, see <http://www.gnu.org/licenses/>.
    15  
    16  package sqlsubscribers
    17  
    18  import (
    19  	"context"
    20  	"testing"
    21  	"time"
    22  
    23  	"code.vegaprotocol.io/vega/core/events"
    24  	"code.vegaprotocol.io/vega/core/types"
    25  	"code.vegaprotocol.io/vega/datanode/entities"
    26  	"code.vegaprotocol.io/vega/libs/num"
    27  
    28  	"github.com/stretchr/testify/assert"
    29  )
    30  
    31  func TestSubscriberSequenceNumber(t *testing.T) {
    32  	ts := testStore{}
    33  	sub := NewTradesSubscriber(&ts)
    34  
    35  	now := time.Now()
    36  	nowPlusOne := time.Now().Add(time.Second)
    37  
    38  	sub.SetVegaTime(now)
    39  
    40  	tradeEvent := events.NewTradeEvent(context.Background(), newTrade())
    41  	tradeEvent.SetSequenceID(1)
    42  	sub.Push(context.Background(), tradeEvent)
    43  
    44  	tradeEvent = events.NewTradeEvent(context.Background(), newTrade())
    45  	tradeEvent.SetSequenceID(2)
    46  	sub.Push(context.Background(), tradeEvent)
    47  
    48  	sub.SetVegaTime(nowPlusOne)
    49  
    50  	tradeEvent = events.NewTradeEvent(context.Background(), newTrade())
    51  	tradeEvent.SetSequenceID(1)
    52  	sub.Push(context.Background(), tradeEvent)
    53  
    54  	tradeEvent = events.NewTradeEvent(context.Background(), newTrade())
    55  	tradeEvent.SetSequenceID(2)
    56  	sub.Push(context.Background(), tradeEvent)
    57  
    58  	assert.Equal(t, now, ts.trades[0].VegaTime)
    59  	assert.Equal(t, uint64(1), ts.trades[0].SeqNum)
    60  	assert.Equal(t, now, ts.trades[1].VegaTime)
    61  	assert.Equal(t, uint64(2), ts.trades[1].SeqNum)
    62  
    63  	assert.Equal(t, nowPlusOne, ts.trades[2].VegaTime)
    64  	assert.Equal(t, uint64(1), ts.trades[2].SeqNum)
    65  	assert.Equal(t, nowPlusOne, ts.trades[3].VegaTime)
    66  	assert.Equal(t, uint64(2), ts.trades[3].SeqNum)
    67  }
    68  
    69  type testStore struct {
    70  	trades []*entities.Trade
    71  }
    72  
    73  func (ts *testStore) Add(t *entities.Trade) error {
    74  	ts.trades = append(ts.trades, t)
    75  	return nil
    76  }
    77  
    78  func (ts *testStore) Flush(ctx context.Context) error {
    79  	return nil
    80  }
    81  
    82  func newTrade() types.Trade {
    83  	trade := types.Trade{
    84  		ID:                 "bc2001bddac588f8aaae0d9bec3d6881a447b888447e5d0a9de92d149ba4e877",
    85  		MarketID:           "8cc0e020c0bc2f9eba77749d81ecec8283283b85941722c2cb88318aaf8b8cd8",
    86  		Price:              num.NewUint(12),
    87  		Size:               16,
    88  		Buyer:              "2e4f34a38204a2a155be678e670903ed8df96e813700729deacd3daf7e55039e",
    89  		Seller:             "8b6be1a03cc4d529f682887a78b66e6879d17f81e2b37356ca0acbc5d5886eb8",
    90  		Aggressor:          types.SideBuy,
    91  		BuyOrder:           "cf951606211775c43449807fe15f908704a85c514d65d549d67bbd6b5eef66bb",
    92  		SellOrder:          "6a94947f724cdb7851bee793aca6888f68abbf8d49dfd0f778424a7ce42e7b7d",
    93  		Type:               types.TradeTypeNetworkCloseOutGood,
    94  		BuyerAuctionBatch:  3,
    95  		SellerAuctionBatch: 4,
    96  		MarketPrice:        num.NewUint(22),
    97  	}
    98  
    99  	return trade
   100  }