code.vegaprotocol.io/vega@v0.79.0/core/execution/common/expiring_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 "testing" 20 21 "code.vegaprotocol.io/vega/core/execution/common" 22 23 "github.com/stretchr/testify/assert" 24 "github.com/stretchr/testify/require" 25 ) 26 27 func TestExpiringOrders(t *testing.T) { 28 t.Run("expire orders ", testExpireOrders) 29 t.Run("snapshot ", testExpireOrdersSnapshot) 30 } 31 32 func testExpireOrders(t *testing.T) { 33 eo := common.NewExpiringOrders() 34 eo.Insert("1", 100) 35 eo.Insert("2", 110) 36 eo.Insert("3", 140) 37 eo.Insert("4", 140) 38 eo.Insert("5", 160) 39 eo.Insert("6", 170) 40 41 // remove them once 42 orders := eo.Expire(140) 43 assert.Equal(t, 4, len(orders)) 44 assert.Equal(t, "1", orders[0]) 45 assert.Equal(t, "2", orders[1]) 46 assert.Equal(t, "3", orders[2]) 47 assert.Equal(t, "4", orders[3]) 48 49 // try again to remove to check if they are still there. 50 orders = eo.Expire(140) 51 assert.Equal(t, 0, len(orders)) 52 53 // now try to remove one more 54 orders = eo.Expire(160) 55 assert.Equal(t, 1, len(orders)) 56 assert.Equal(t, "5", orders[0]) 57 } 58 59 func testExpireOrdersSnapshot(t *testing.T) { 60 a := assert.New(t) 61 eo := common.NewExpiringOrders() 62 a.True(eo.Changed()) 63 64 testOrders := getTestOrders()[:6] 65 66 // Test empty 67 a.Len(eo.GetState(), 0) 68 69 eo.Insert(testOrders[0].ID, 100) 70 eo.Insert(testOrders[1].ID, 110) 71 eo.Insert(testOrders[2].ID, 140) 72 eo.Insert(testOrders[3].ID, 140) 73 eo.Insert(testOrders[4].ID, 160) 74 eo.Insert(testOrders[5].ID, 170) 75 a.True(eo.Changed()) 76 77 testIDs := map[string]struct{}{} 78 for _, to := range testOrders { 79 testIDs[to.ID] = struct{}{} 80 } 81 82 s := eo.GetState() 83 84 newEo := common.NewExpiringOrdersFromState(s) 85 a.True(newEo.Changed()) 86 state := newEo.GetState() 87 a.Equal(len(testIDs), len(state)) 88 for _, o := range state { 89 require.NotNil(t, testIDs[o.ID]) 90 } 91 }