code.vegaprotocol.io/vega@v0.79.0/datanode/candlesv2/service_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 candlesv2_test
    17  
    18  import (
    19  	"context"
    20  	"testing"
    21  	"time"
    22  
    23  	"code.vegaprotocol.io/vega/datanode/candlesv2"
    24  	"code.vegaprotocol.io/vega/datanode/candlesv2/mocks"
    25  	"code.vegaprotocol.io/vega/datanode/entities"
    26  	"code.vegaprotocol.io/vega/logging"
    27  
    28  	"github.com/golang/mock/gomock"
    29  	"github.com/stretchr/testify/assert"
    30  )
    31  
    32  func TestCandleSubscribe(t *testing.T) {
    33  	ctrl := gomock.NewController(t)
    34  
    35  	store := mocks.NewMockCandleStore(ctrl)
    36  	store.EXPECT().CandleExists(
    37  		gomock.Any(),
    38  		gomock.Any()).Return(true, nil)
    39  
    40  	expectedCandle := createCandle(time.Now(), time.Now(), 1, 2, 2, 1, 10, 100)
    41  
    42  	store.EXPECT().GetCandleDataForTimeSpan(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).
    43  		Return([]entities.Candle{expectedCandle}, entities.PageInfo{}, nil).AnyTimes()
    44  
    45  	svc := candlesv2.NewService(context.Background(), logging.NewTestLogger(), candlesv2.NewDefaultConfig(), store)
    46  
    47  	candleID := "candle1"
    48  	_, out1, err := svc.Subscribe(context.Background(), candleID)
    49  	if err != nil {
    50  		t.Fatalf("failed to Subscribe: %s", err)
    51  	}
    52  
    53  	candle1 := <-out1
    54  	assert.Equal(t, expectedCandle, candle1)
    55  }
    56  
    57  func TestCandleUnsubscribe(t *testing.T) {
    58  	ctrl := gomock.NewController(t)
    59  
    60  	mockStore := mocks.NewMockCandleStore(ctrl)
    61  	mockStore.EXPECT().CandleExists(
    62  		gomock.Any(),
    63  		gomock.Any()).Return(true, nil)
    64  
    65  	testStore := &testStore{
    66  		CandleStore: mockStore,
    67  		candles:     make(chan []entities.Candle),
    68  	}
    69  
    70  	svc := candlesv2.NewService(context.Background(), logging.NewTestLogger(), candlesv2.NewDefaultConfig(), testStore)
    71  
    72  	candleID := "candle1"
    73  	subscriptionID, out1, err := svc.Subscribe(context.Background(), candleID)
    74  	if err != nil {
    75  		t.Fatalf("failed to Subscribe: %s", err)
    76  	}
    77  
    78  	expectedCandle := createCandle(time.Now(), time.Now(), 1, 2, 2, 1, 10, 100)
    79  	testStore.candles <- []entities.Candle{expectedCandle}
    80  
    81  	candle1 := <-out1
    82  	assert.Equal(t, expectedCandle, candle1)
    83  
    84  	svc.Unsubscribe(subscriptionID)
    85  
    86  	_, ok := <-out1
    87  	assert.False(t, ok, "channel should be closed")
    88  }
    89  
    90  type testStore struct {
    91  	candlesv2.CandleStore
    92  	candles chan []entities.Candle
    93  }
    94  
    95  func (t *testStore) GetCandleDataForTimeSpan(ctx context.Context, candleID string, from *time.Time, to *time.Time, p entities.CursorPagination) ([]entities.Candle, entities.PageInfo, error) {
    96  	return <-t.candles, entities.PageInfo{}, nil
    97  }