code.vegaprotocol.io/vega@v0.79.0/core/matching/cache_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 matching_test
    17  
    18  import (
    19  	"testing"
    20  
    21  	"code.vegaprotocol.io/vega/core/matching"
    22  	"code.vegaprotocol.io/vega/core/types"
    23  	"code.vegaprotocol.io/vega/libs/num"
    24  
    25  	"github.com/stretchr/testify/assert"
    26  )
    27  
    28  func TestCachingValues(t *testing.T) {
    29  	cache := matching.NewBookCache()
    30  
    31  	// by default all cache are invalid
    32  	_, priceOK := cache.GetIndicativePrice()
    33  	assert.False(t, priceOK)
    34  	_, volOK := cache.GetIndicativeVolume()
    35  	assert.False(t, volOK)
    36  	_, sideOK := cache.GetIndicativeUncrossingSide()
    37  	assert.False(t, sideOK)
    38  
    39  	// setting one value only validate one cache not the others
    40  	cache.SetIndicativeVolume(42)
    41  	_, priceOK = cache.GetIndicativePrice()
    42  	assert.False(t, priceOK)
    43  	vol, volOK := cache.GetIndicativeVolume()
    44  	assert.True(t, volOK)
    45  	assert.Equal(t, vol, uint64(42))
    46  	_, sideOK = cache.GetIndicativeUncrossingSide()
    47  	assert.False(t, sideOK)
    48  
    49  	// setting all of them make them all valid
    50  	cache.SetIndicativePrice(num.NewUint(84))
    51  	cache.SetIndicativeUncrossingSide(types.SideBuy)
    52  	price, priceOK := cache.GetIndicativePrice()
    53  	assert.True(t, priceOK)
    54  	assert.Equal(t, price.Uint64(), uint64(84))
    55  	vol, volOK = cache.GetIndicativeVolume()
    56  	assert.True(t, volOK)
    57  	assert.Equal(t, vol, uint64(42))
    58  	side, sideOK := cache.GetIndicativeUncrossingSide()
    59  	assert.True(t, sideOK)
    60  	assert.Equal(t, side, types.SideBuy)
    61  
    62  	// invalide affects all cache
    63  	cache.Invalidate()
    64  	_, priceOK = cache.GetIndicativePrice()
    65  	assert.False(t, priceOK)
    66  	_, volOK = cache.GetIndicativeVolume()
    67  	assert.False(t, volOK)
    68  	_, sideOK = cache.GetIndicativeUncrossingSide()
    69  	assert.False(t, sideOK)
    70  }