code.vegaprotocol.io/vega@v0.79.0/core/execution/common/pegged_orders_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 common_test 17 18 import ( 19 "fmt" 20 "testing" 21 22 "code.vegaprotocol.io/vega/core/execution/common" 23 "code.vegaprotocol.io/vega/core/execution/common/mocks" 24 "code.vegaprotocol.io/vega/core/types" 25 "code.vegaprotocol.io/vega/libs/crypto" 26 "code.vegaprotocol.io/vega/libs/num" 27 "code.vegaprotocol.io/vega/logging" 28 29 "github.com/golang/mock/gomock" 30 "github.com/stretchr/testify/assert" 31 ) 32 33 func getTestOrders() []*types.Order { 34 o := []*types.Order{} 35 36 for i := 0; i < 10; i++ { 37 p, _ := num.UintFromString(fmt.Sprintf("%d", i*10), 10) 38 39 o = append(o, &types.Order{ 40 ID: crypto.RandomHash(), 41 MarketID: "market-1", 42 Party: "party-1", 43 Side: types.SideBuy, 44 Price: p, 45 Size: uint64(i), 46 Remaining: uint64(i), 47 TimeInForce: types.OrderTimeInForceFOK, 48 Type: types.OrderTypeMarket, 49 Status: types.OrderStatusActive, 50 Reference: "ref-1", 51 Version: uint64(2), 52 BatchID: uint64(4), 53 }) 54 } 55 56 return o 57 } 58 59 func TestPeggedOrders(t *testing.T) { 60 t.Run("snapshot ", testPeggedOrdersSnapshot) 61 } 62 63 func testPeggedOrdersSnapshot(t *testing.T) { 64 a := assert.New(t) 65 66 t.Helper() 67 ctrl := gomock.NewController(t) 68 tm := mocks.NewMockTimeService(ctrl) 69 p := common.NewPeggedOrders(logging.NewTestLogger(), tm) 70 tm.EXPECT().GetTimeNow().AnyTimes() 71 72 // Test empty 73 s := p.GetState() 74 a.Equal( 75 &types.PeggedOrdersState{ 76 Parked: []*types.Order{}, 77 }, 78 s, 79 ) 80 81 testOrders := getTestOrders()[:4] 82 83 // Test after adding orders 84 p.Park(testOrders[0]) 85 p.Park(testOrders[1]) 86 p.Park(testOrders[2]) 87 p.Park(testOrders[3]) 88 a.Equal(testOrders[0].ID, p.GetState().Parked[0].ID) 89 a.Equal(testOrders[1].ID, p.GetState().Parked[1].ID) 90 a.Equal(testOrders[2].ID, p.GetState().Parked[2].ID) 91 a.Equal(testOrders[3].ID, p.GetState().Parked[3].ID) 92 93 // Test amend 94 p.AmendParked(testOrders[0]) 95 a.True(p.Changed()) 96 a.Equal(testOrders[0], p.GetState().Parked[0]) 97 98 // Test unpark 99 p.Unpark(testOrders[3].ID) 100 a.Equal(3, len(p.GetState().Parked)) 101 a.Equal(testOrders[0].ID, p.GetState().Parked[0].ID) 102 a.Equal(testOrders[1].ID, p.GetState().Parked[1].ID) 103 a.Equal(testOrders[2].ID, p.GetState().Parked[2].ID) 104 105 // Test get functions won't change state 106 p.GetAllParkedForParty("party-1") 107 p.GetParkedIDs() 108 p.GetParkedByID("id-2") 109 p.GetParkedOrdersCount() 110 111 // Test restore state 112 s = p.GetState() 113 114 newP := common.NewPeggedOrdersFromSnapshot(logging.NewTestLogger(), tm, s) 115 a.Equal(s, newP.GetState()) 116 a.Equal(len(p.GetParkedIDs()), len(newP.GetParkedIDs())) 117 }