code.vegaprotocol.io/vega@v0.79.0/datanode/api/event_observer_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 api 17 18 import ( 19 "context" 20 "fmt" 21 "testing" 22 "time" 23 24 protoapi "code.vegaprotocol.io/vega/protos/vega/api/v1" 25 eventspb "code.vegaprotocol.io/vega/protos/vega/events/v1" 26 27 "github.com/stretchr/testify/assert" 28 "google.golang.org/grpc/codes" 29 "google.golang.org/grpc/status" 30 ) 31 32 func Test_recvEventRequest(t *testing.T) { 33 type args struct { 34 ctx context.Context 35 timeout time.Duration 36 stream eventBusServer 37 } 38 tests := []struct { 39 name string 40 args args 41 want *protoapi.ObserveEventBusRequest 42 wantErr assert.ErrorAssertionFunc 43 }{ 44 { 45 name: "happy path", 46 args: args{ 47 ctx: context.Background(), 48 stream: &mockEventBusServer{ 49 msg: exampleObserveEventBusRequest(), 50 sleep: 10 * time.Millisecond, 51 }, 52 timeout: 100 * time.Millisecond, 53 }, 54 want: exampleObserveEventBusRequest(), 55 wantErr: assert.NoError, 56 }, { 57 name: "error on stream", 58 args: args{ 59 ctx: context.Background(), 60 stream: &mockEventBusServer{ 61 err: status.Error(codes.Internal, "error"), 62 sleep: 10 * time.Millisecond, 63 }, 64 timeout: 100 * time.Millisecond, 65 }, 66 wantErr: assert.Error, 67 }, { 68 name: "timeout", 69 args: args{ 70 ctx: context.Background(), 71 stream: &mockEventBusServer{ 72 sleep: 100 * time.Millisecond, 73 }, 74 timeout: 10 * time.Millisecond, 75 }, 76 wantErr: assert.Error, 77 }, 78 } 79 for _, tt := range tests { 80 t.Run(tt.name, func(t *testing.T) { 81 got, err := recvEventRequest(tt.args.ctx, tt.args.timeout, tt.args.stream) 82 if !tt.wantErr(t, err, fmt.Sprintf("recvEventRequest(%v, %v, %v)", tt.args.ctx, tt.args.timeout, tt.args.stream)) { 83 return 84 } 85 if err != nil { 86 return 87 } 88 assert.Equalf(t, tt.want, got, "recvEventRequest(%v, %v, %v)", tt.args.ctx, tt.args.timeout, tt.args.stream) 89 }) 90 } 91 } 92 93 func exampleObserveEventBusRequest() *protoapi.ObserveEventBusRequest { 94 return &protoapi.ObserveEventBusRequest{ 95 Type: []eventspb.BusEventType{eventspb.BusEventType_BUS_EVENT_TYPE_ALL}, 96 MarketId: "123", 97 PartyId: "asdf", 98 BatchSize: 100, 99 } 100 } 101 102 type mockEventBusServer struct { 103 msg *protoapi.ObserveEventBusRequest 104 err error 105 sleep time.Duration 106 } 107 108 func (m *mockEventBusServer) RecvMsg(i interface{}) error { 109 time.Sleep(m.sleep) 110 if m.err == nil && m.msg != nil { 111 *i.(*protoapi.ObserveEventBusRequest) = *m.msg 112 } 113 return m.err 114 } 115 116 func (m *mockEventBusServer) Context() context.Context { 117 return context.Background() 118 } 119 120 func (m *mockEventBusServer) Send([]*eventspb.BusEvent) error { 121 return nil 122 }