code.vegaprotocol.io/vega@v0.79.0/datanode/service/market_data_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 service_test
    17  
    18  import (
    19  	"context"
    20  	"testing"
    21  
    22  	"code.vegaprotocol.io/vega/datanode/entities"
    23  	"code.vegaprotocol.io/vega/datanode/service"
    24  	"code.vegaprotocol.io/vega/datanode/service/mocks"
    25  	"code.vegaprotocol.io/vega/logging"
    26  
    27  	"github.com/golang/mock/gomock"
    28  	"github.com/google/go-cmp/cmp"
    29  	"github.com/google/go-cmp/cmp/cmpopts"
    30  	"github.com/stretchr/testify/assert"
    31  )
    32  
    33  var (
    34  	testData1      = entities.MarketData{Market: entities.MarketID("aa"), SeqNum: 1}
    35  	testData2      = entities.MarketData{Market: entities.MarketID("bb"), SeqNum: 2}
    36  	testData3      = entities.MarketData{Market: entities.MarketID("aa"), SeqNum: 3}
    37  	testData4      = entities.MarketData{Market: entities.MarketID("cc"), SeqNum: 4}
    38  	sortMarketData = cmpopts.SortSlices(func(a, b entities.MarketData) bool { return a.SeqNum < b.SeqNum })
    39  )
    40  
    41  func TestInitialise(t *testing.T) {
    42  	ctx := context.Background()
    43  	ctrl := gomock.NewController(t)
    44  
    45  	// Set up mock store to have some initial data in it when we initialise
    46  	store := mocks.NewMockMarketDataStore(ctrl)
    47  	store.EXPECT().GetMarketsData(gomock.Any()).Return([]entities.MarketData{
    48  		testData1,
    49  		testData2,
    50  	}, nil)
    51  
    52  	// Initialise and check that we get that data out of the cache (e.g. no other calls to store)
    53  	svc := service.NewMarketData(store, logging.NewTestLogger())
    54  	svc.Initialise(ctx)
    55  
    56  	allData, err := svc.GetMarketsData(ctx)
    57  	assert.NoError(t, err)
    58  	assert.Empty(t, cmp.Diff(allData, []entities.MarketData{testData1, testData2}, sortMarketData))
    59  }
    60  
    61  func TestAdd(t *testing.T) {
    62  	ctx := context.Background()
    63  	ctrl := gomock.NewController(t)
    64  
    65  	// Set up mock store to have some initial data in it when we initialise
    66  	store := mocks.NewMockMarketDataStore(ctrl)
    67  	store.EXPECT().GetMarketsData(gomock.Any()).Return([]entities.MarketData{
    68  		testData1,
    69  		testData2,
    70  	}, nil)
    71  
    72  	// Expect a couple of calls to Add, we don't need to do anything with them as service should cache
    73  	store.EXPECT().Add(gomock.Any()).Return(nil).Times(2)
    74  
    75  	// Make service, initialise (mock store has 2 records in it), and add two more bits of data.
    76  	svc := service.NewMarketData(store, logging.NewTestLogger())
    77  	svc.Initialise(ctx)
    78  	svc.Add(&testData3)
    79  	svc.Add(&testData4)
    80  
    81  	// testData3 has the same market as testData1 so check we replaced it. Expect no calls to
    82  	// the store as this should be in the service cache.
    83  	allData, err := svc.GetMarketsData(ctx)
    84  	assert.NoError(t, err)
    85  	assert.Empty(t, cmp.Diff(allData, []entities.MarketData{testData2, testData3, testData4}, sortMarketData))
    86  
    87  	// Then try getting for just one market. Should be cached.
    88  	oneData, err := svc.GetMarketDataByID(ctx, "aa")
    89  	assert.NoError(t, err)
    90  	assert.Equal(t, testData3, oneData)
    91  }