diff --git a/pkg/testserver/matchers.go b/pkg/testserver/matchers.go new file mode 100644 index 000000000..c073d0dec --- /dev/null +++ b/pkg/testserver/matchers.go @@ -0,0 +1,63 @@ +package testserver + +import ( + "context" + "fmt" + "github.com/formancehq/go-libs/pointer" + "github.com/formancehq/stack/ledger/client/models/operations" + . "github.com/onsi/gomega" + "github.com/onsi/gomega/types" + "math/big" +) + +type HaveCoherentStateMatcher struct{} + +func (h HaveCoherentStateMatcher) Match(actual interface{}) (success bool, err error) { + srv, ok := actual.(*Server) + if !ok { + return false, fmt.Errorf("expect type %T", new(Server)) + } + ctx := context.Background() + + ledgers, err := ListLedgers(ctx, srv, operations.V2ListLedgersRequest{ + PageSize: pointer.For(int64(100)), + }) + if err != nil { + return false, err + } + + for _, ledger := range ledgers.Data { + aggregatedBalances, err := GetAggregatedBalances(ctx, srv, operations.V2GetBalancesAggregatedRequest{ + Ledger: ledger.Name, + UseInsertionDate: pointer.For(true), + }) + Expect(err).To(BeNil()) + if len(aggregatedBalances) == 0 { // it's random, a ledger could not have been targeted + // just in case, check if the ledger has transactions + txs, err := ListTransactions(ctx, srv, operations.V2ListTransactionsRequest{ + Ledger: ledger.Name, + }) + Expect(err).To(BeNil()) + Expect(txs.Data).To(HaveLen(0)) + } else { + Expect(aggregatedBalances).To(HaveLen(1)) + Expect(aggregatedBalances["USD"]).To(Equal(big.NewInt(0))) + } + } + + return true, nil +} + +func (h HaveCoherentStateMatcher) FailureMessage(_ interface{}) (message string) { + return "server should has coherent state" +} + +func (h HaveCoherentStateMatcher) NegatedFailureMessage(_ interface{}) (message string) { + return "server should not has coherent state but has" +} + +var _ types.GomegaMatcher = (*HaveCoherentStateMatcher)(nil) + +func HaveCoherentState() *HaveCoherentStateMatcher { + return &HaveCoherentStateMatcher{} +} diff --git a/test/e2e/stress_test.go b/test/e2e/stress_test.go index 78e0d93b6..b0f1d196b 100644 --- a/test/e2e/stress_test.go +++ b/test/e2e/stress_test.go @@ -3,7 +3,6 @@ package test_suite import ( - "context" "fmt" "math/big" "math/rand" @@ -20,7 +19,6 @@ import ( "github.com/formancehq/stack/ledger/client/models/operations" . "github.com/onsi/ginkgo/v2" . "github.com/onsi/gomega" - "github.com/onsi/gomega/types" ) var _ = Context("Ledger stress tests", func() { @@ -147,55 +145,3 @@ var _ = Context("Ledger stress tests", func() { }) }) }) - -type HaveCoherentStateMatcher struct{} - -func (h HaveCoherentStateMatcher) Match(actual interface{}) (success bool, err error) { - srv, ok := actual.(*Server) - if !ok { - return false, fmt.Errorf("expect type %T", new(Server)) - } - ctx := context.Background() - - ledgers, err := ListLedgers(ctx, srv, operations.V2ListLedgersRequest{ - PageSize: pointer.For(int64(100)), - }) - if err != nil { - return false, err - } - - for _, ledger := range ledgers.Data { - aggregatedBalances, err := GetAggregatedBalances(ctx, srv, operations.V2GetBalancesAggregatedRequest{ - Ledger: ledger.Name, - UseInsertionDate: pointer.For(true), - }) - Expect(err).To(BeNil()) - if len(aggregatedBalances) == 0 { // it's random, a ledger could not have been targeted - // just in case, check if the ledger has transactions - txs, err := ListTransactions(ctx, srv, operations.V2ListTransactionsRequest{ - Ledger: ledger.Name, - }) - Expect(err).To(BeNil()) - Expect(txs.Data).To(HaveLen(0)) - } else { - Expect(aggregatedBalances).To(HaveLen(1)) - Expect(aggregatedBalances["USD"]).To(Equal(big.NewInt(0))) - } - } - - return true, nil -} - -func (h HaveCoherentStateMatcher) FailureMessage(_ interface{}) (message string) { - return "server should has coherent state" -} - -func (h HaveCoherentStateMatcher) NegatedFailureMessage(_ interface{}) (message string) { - return "server should not has coherent state but has" -} - -var _ types.GomegaMatcher = (*HaveCoherentStateMatcher)(nil) - -func HaveCoherentState() *HaveCoherentStateMatcher { - return &HaveCoherentStateMatcher{} -}