code.vegaprotocol.io/vega@v0.79.0/libs/subscribers/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 subscribers
    17  
    18  import (
    19  	"context"
    20  	"testing"
    21  
    22  	"code.vegaprotocol.io/vega/core/events"
    23  	dtypes "code.vegaprotocol.io/vega/core/types"
    24  	"code.vegaprotocol.io/vega/libs/subscribers/mocks"
    25  	"code.vegaprotocol.io/vega/logging"
    26  	eventspb "code.vegaprotocol.io/vega/protos/vega/events/v1"
    27  
    28  	"github.com/golang/mock/gomock"
    29  	"github.com/stretchr/testify/assert"
    30  )
    31  
    32  func TestSlowConsumerIsDisconnected(t *testing.T) {
    33  	ctx, cancelFn := context.WithCancel(context.Background())
    34  	defer cancelFn()
    35  
    36  	ctrl := gomock.NewController(t)
    37  
    38  	broker := mocks.NewMockBroker(ctrl)
    39  	broker.EXPECT().Subscribe(gomock.Any())
    40  	broker.EXPECT().Unsubscribe(gomock.Any())
    41  
    42  	maxBufferSize := 3
    43  	testStreamSubscription := TestStreamSubscription{events: make(chan []*eventspb.BusEvent)}
    44  
    45  	s := NewService(logging.NewTestLogger(), broker, maxBufferSize)
    46  	out, _ := s.ObserveEventsOnStream(ctx, 2, testStreamSubscription)
    47  
    48  	testStreamSubscription.events <- []*eventspb.BusEvent{
    49  		events.NewAccountEvent(ctx, dtypes.Account{
    50  			ID: "acc-1",
    51  		}).StreamMessage(),
    52  		events.NewAccountEvent(ctx, dtypes.Account{
    53  			ID: "acc-2",
    54  		}).StreamMessage(),
    55  	}
    56  
    57  	events1 := <-out
    58  	assert.Equal(t,
    59  		[]*eventspb.BusEvent{
    60  			events.NewAccountEvent(ctx, dtypes.Account{ID: "acc-1"}).StreamMessage(),
    61  			events.NewAccountEvent(ctx, dtypes.Account{ID: "acc-2"}).StreamMessage(),
    62  		},
    63  		events1)
    64  
    65  	testStreamSubscription.events <- []*eventspb.BusEvent{
    66  		events.NewAccountEvent(ctx, dtypes.Account{
    67  			ID: "acc-3",
    68  		}).StreamMessage(),
    69  		events.NewAccountEvent(ctx, dtypes.Account{
    70  			ID: "acc-4",
    71  		}).StreamMessage(),
    72  		events.NewAccountEvent(ctx, dtypes.Account{
    73  			ID: "acc-5",
    74  		}).StreamMessage(),
    75  		events.NewAccountEvent(ctx, dtypes.Account{
    76  			ID: "acc-6",
    77  		}).StreamMessage(),
    78  	}
    79  
    80  	// We expect this channel to close
    81  	for range out {
    82  	}
    83  }
    84  
    85  type TestStreamSubscription struct {
    86  	events chan []*eventspb.BusEvent
    87  }
    88  
    89  func (t TestStreamSubscription) Halt() {
    90  	// TODO implement me
    91  	panic("implement me")
    92  }
    93  
    94  func (t TestStreamSubscription) Push(evts ...events.Event) {
    95  	// TODO implement me
    96  	panic("implement me")
    97  }
    98  
    99  func (t TestStreamSubscription) UpdateBatchSize(ctx context.Context, size int) []*eventspb.BusEvent {
   100  	// TODO implement me
   101  	panic("implement me")
   102  }
   103  
   104  func (t TestStreamSubscription) Types() []events.Type {
   105  	// TODO implement me
   106  	panic("implement me")
   107  }
   108  
   109  func (t TestStreamSubscription) GetData(ctx context.Context) []*eventspb.BusEvent {
   110  	return <-t.events
   111  }
   112  
   113  func (t TestStreamSubscription) C() chan<- []events.Event {
   114  	// TODO implement me
   115  	panic("implement me")
   116  }
   117  
   118  func (t TestStreamSubscription) Closed() <-chan struct{} {
   119  	// TODO implement me
   120  	panic("implement me")
   121  }
   122  
   123  func (t TestStreamSubscription) Skip() <-chan struct{} {
   124  	// TODO implement me
   125  	panic("implement me")
   126  }
   127  
   128  func (t TestStreamSubscription) SetID(id int) {
   129  	// TODO implement me
   130  	panic("implement me")
   131  }
   132  
   133  func (t TestStreamSubscription) ID() int {
   134  	// TODO implement me
   135  	panic("implement me")
   136  }
   137  
   138  func (t TestStreamSubscription) Ack() bool {
   139  	// TODO implement me
   140  	panic("implement me")
   141  }