code.vegaprotocol.io/vega@v0.79.0/datanode/sqlsubscribers/transaction_results_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 sqlsubscribers_test
    17  
    18  import (
    19  	"context"
    20  	"fmt"
    21  	"sync"
    22  	"testing"
    23  
    24  	"code.vegaprotocol.io/vega/core/events"
    25  	"code.vegaprotocol.io/vega/datanode/sqlsubscribers"
    26  	"code.vegaprotocol.io/vega/logging"
    27  	commandspb "code.vegaprotocol.io/vega/protos/vega/commands/v1"
    28  
    29  	"github.com/stretchr/testify/assert"
    30  )
    31  
    32  func TestTransactionResults(t *testing.T) {
    33  	logger := logging.NewTestLogger()
    34  
    35  	ctx := context.Background()
    36  
    37  	subscriber := sqlsubscribers.NewTransactionResults(logger)
    38  
    39  	expectedEvents := generateTestTransactionResultEvents(5, 5)
    40  
    41  	var wg sync.WaitGroup
    42  	// expect all events + success events + failed events + 2 per party events + 1 specific hash event
    43  	wg.Add(len(expectedEvents)*2 + 3)
    44  
    45  	// all events
    46  	subChan, ref := subscriber.Observe(ctx, 2, []string{""}, []string{""}, nil)
    47  	assert.Equal(t, uint64(1), ref)
    48  	go func() {
    49  		for events := range subChan {
    50  			for range events {
    51  				wg.Done()
    52  			}
    53  		}
    54  	}()
    55  
    56  	// success events
    57  	success := true
    58  	successSubChan, ref := subscriber.Observe(ctx, 2, []string{""}, []string{""}, &success)
    59  	assert.Equal(t, uint64(2), ref)
    60  	go func() {
    61  		for events := range successSubChan {
    62  			for range events {
    63  				wg.Done()
    64  			}
    65  		}
    66  	}()
    67  
    68  	// failed events
    69  	failure := false
    70  	failureSubChan, ref := subscriber.Observe(ctx, 2, []string{""}, []string{""}, &failure)
    71  	assert.Equal(t, uint64(3), ref)
    72  	go func() {
    73  		for events := range failureSubChan {
    74  			for range events {
    75  				wg.Done()
    76  			}
    77  		}
    78  	}()
    79  
    80  	// 2 per party events
    81  	partySubChan, ref := subscriber.Observe(ctx, 2, []string{"party-2"}, []string{""}, nil)
    82  	assert.Equal(t, uint64(4), ref)
    83  	go func() {
    84  		for events := range partySubChan {
    85  			for range events {
    86  				wg.Done()
    87  			}
    88  		}
    89  	}()
    90  
    91  	// 1 specific hash event
    92  	hashSubChan, ref := subscriber.Observe(ctx, 2, []string{""}, []string{"hash-6"}, nil)
    93  	assert.Equal(t, uint64(5), ref)
    94  	go func() {
    95  		for events := range hashSubChan {
    96  			for range events {
    97  				wg.Done()
    98  			}
    99  		}
   100  	}()
   101  
   102  	for _, event := range expectedEvents {
   103  		subscriber.Push(context.Background(), event)
   104  	}
   105  
   106  	wg.Done()
   107  }
   108  
   109  func generateTestTransactionResultEvents(successCount, failureCount int) []*events.TransactionResult {
   110  	out := make([]*events.TransactionResult, 0, successCount+failureCount)
   111  
   112  	for i := 0; i < successCount; i++ {
   113  		out = append(out,
   114  			events.NewTransactionResultEventSuccess(
   115  				context.Background(),
   116  				fmt.Sprintf("hash-%d", i),
   117  				fmt.Sprintf("party-%d", i),
   118  				&commandspb.LiquidityProvisionSubmission{
   119  					MarketId:         fmt.Sprintf("market-%d", i),
   120  					CommitmentAmount: "100",
   121  					Fee:              "1",
   122  					Reference:        fmt.Sprintf("lp-%d", i),
   123  				},
   124  			),
   125  		)
   126  	}
   127  
   128  	for i := 0; i < failureCount; i++ {
   129  		nth := i + successCount
   130  		out = append(out,
   131  			events.NewTransactionResultEventFailure(
   132  				context.Background(),
   133  				fmt.Sprintf("hash-%d", nth),
   134  				fmt.Sprintf("party-%d", i),
   135  				fmt.Errorf("error-%d", i),
   136  				&commandspb.LiquidityProvisionSubmission{
   137  					MarketId:         fmt.Sprintf("market-%d", i),
   138  					CommitmentAmount: "100",
   139  					Fee:              "1",
   140  					Reference:        fmt.Sprintf("lp-%d", i),
   141  				},
   142  			),
   143  		)
   144  	}
   145  
   146  	return out
   147  }