code.vegaprotocol.io/vega@v0.79.0/datanode/broker/buffer_files_event_source_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 broker
    17  
    18  import (
    19  	"context"
    20  	"os"
    21  	"path/filepath"
    22  	"testing"
    23  
    24  	"code.vegaprotocol.io/vega/core/broker"
    25  	"code.vegaprotocol.io/vega/core/events"
    26  	"code.vegaprotocol.io/vega/core/types"
    27  	vgcontext "code.vegaprotocol.io/vega/libs/context"
    28  	eventsv1 "code.vegaprotocol.io/vega/protos/vega/events/v1"
    29  
    30  	"github.com/stretchr/testify/assert"
    31  )
    32  
    33  func TestReceiveEvents(t *testing.T) {
    34  	ctx, cancel := context.WithCancel(context.Background())
    35  	defer cancel()
    36  
    37  	bufferFilesDir := t.TempDir()
    38  	bufferFile, err := os.Create(filepath.Join(bufferFilesDir, "file1"))
    39  	assert.NoError(t, err)
    40  	defer bufferFile.Close()
    41  
    42  	ctxBlock1 := vgcontext.WithTraceID(ctx, "1")
    43  	a1 := events.NewAssetEvent(ctxBlock1, types.Asset{ID: "1"})
    44  
    45  	ctxBlock2 := vgcontext.WithTraceID(ctx, "2")
    46  	beginBlockEvent := events.NewBeginBlock(ctxBlock2, eventsv1.BeginBlock{
    47  		Height:    10,
    48  		Timestamp: 0,
    49  	})
    50  
    51  	a2 := events.NewAssetEvent(ctxBlock2, types.Asset{ID: "2"})
    52  	a3 := events.NewAssetEvent(ctxBlock2, types.Asset{ID: "3"})
    53  
    54  	broker.WriteToBufferFile(bufferFile, 6, a1)
    55  	broker.WriteToBufferFile(bufferFile, 7, beginBlockEvent)
    56  	broker.WriteToBufferFile(bufferFile, 8, a2)
    57  	broker.WriteToBufferFile(bufferFile, 9, a3)
    58  	bufferFile.Close()
    59  
    60  	bufferFile2, err := os.Create(filepath.Join(bufferFilesDir, "file2"))
    61  	assert.NoError(t, err)
    62  	defer bufferFile2.Close()
    63  
    64  	a4 := events.NewAssetEvent(ctxBlock2, types.Asset{ID: "4"})
    65  	a5 := events.NewAssetEvent(ctxBlock2, types.Asset{ID: "5"})
    66  
    67  	broker.WriteToBufferFile(bufferFile2, 10, a4)
    68  	broker.WriteToBufferFile(bufferFile2, 11, a5)
    69  	bufferFile2.Close()
    70  
    71  	rawEventSource, err := NewBufferFilesEventSource(bufferFilesDir, 0, 1000, "")
    72  	assert.NoError(t, err)
    73  
    74  	eventSource := NewDeserializer(rawEventSource)
    75  
    76  	err = eventSource.Listen()
    77  	assert.NoError(t, err)
    78  
    79  	evtCh, _ := eventSource.Receive(ctx)
    80  
    81  	e1 := <-evtCh
    82  	r1 := e1.(*events.BeginBlock)
    83  	e2 := <-evtCh
    84  	r2 := e2.(*events.Asset)
    85  	e3 := <-evtCh
    86  	r3 := e3.(*events.Asset)
    87  	e4 := <-evtCh
    88  	r4 := e4.(*events.Asset)
    89  	e5 := <-evtCh
    90  	r5 := e5.(*events.Asset)
    91  
    92  	assert.Equal(t, uint64(10), r1.BeginBlock().Height)
    93  	assert.Equal(t, "2", r2.Asset().Id)
    94  	assert.Equal(t, "3", r3.Asset().Id)
    95  	assert.Equal(t, "4", r4.Asset().Id)
    96  	assert.Equal(t, "5", r5.Asset().Id)
    97  }