Files
payments/pkg/testserver/outbox_helpers.go
fgueryandQuentin David a77f1fabef fix(EN-549): remove config and connector dependencies (#622)
* chore: rename RunNextTasks workflow

* feat: added a trimmed down type for connector only containing the id

* feat: make runNextTask use connectorIdOnly

* feat: added isScheduledForDeletion to plugin interface

* feat: add scheduledForDeletion at plugin level

* feat: refactor manager.Load to take a connector instead of parts of it

* feat: saves the connector's scheduledFOrDeletion into the plugin

* feat: use plugin.IsScheduledForDeletion instead of loading connector

* fix: make e2e tests less flaky

* fix: remove config from most request types

* feat: (WIP) remove config from runNextTask

* fix: remove unneeded method definitions in each plugin

* fix: simplify RunNextFlow as anyway we need to change the parameters

* fix: add a workflow compatible with previous version for runNextTasks

* fix(EN-577): added back RunSendEvents as a deprecated workflow

* fix(EN-577): added back RunUpdatePaymentInitiationFromPayment as a deprecated workflow

* fix: fix linting directives

* fix: replace old usage with refactored one

* fix: added back missing deprecated activities

* fix: ensure fetch_next_balance works with v3.0 code

* fix: simplify a bit the versionning etc

* fix: quick refactor before applying solution elsewhere

* fix: add back RunSendEvents and RunNextTask child workflows where necessary for backward compatibility

* fix: add back missing activity, seems like it's mandatory?

* fix: run payment initiation update as child workflow when necessary

* fix: missing activy call in test, refine some todos

* fix: lint

* fix: ensure we always reload plugins into the manager -- so exposing isScheduledForDeletion

* fix: ensure we stop storing clear config in temporal

* fix: make store config support plain text config

* fix: some comments

* fix: change runSendEvent workflow to be match what we had in 3.0

* feat: enable worker versionning to avoid workflow from new version to get executed on old code

* fix: lint

* fix: revert worker versionning

* fix: minor code review comments

* fix: add context to Encrypt & Decrypt methods

* fix: improve lint, add couple tests

* fix: use atomic to handle isScheduledForDeletion

* fix: make create_events work on migration

* fix: avoid returning channel; just send what is needed in runSendEventAsChildWorkflow and runNextTaskAsChildWorkflow

* fix: remove extra call to installWorkflow on start plugin

* fix: typo in error log line

* fix: simplify errChan initialization

* fix: clarify feature flag names
2026-01-19 14:35:36 +01:00

148 lines
4.1 KiB
Go

package testserver
import (
"context"
"encoding/json"
"errors"
"time"
"github.com/onsi/gomega"
"github.com/uptrace/bun"
)
// AwaitOutboxEvent polls the database outbox_events table until it finds
// an event with the given outbox event type matching all provided payload matchers,
// or the timeout expires. It checks only pending outbox events.
func AwaitOutboxEvent(ctx context.Context, s *Server, outboxEventType string, timeout, interval time.Duration, matchers ...PayloadMatcher) error {
deadline := time.Now().Add(timeout)
for {
if time.Now().After(deadline) {
return errors.New("timeout waiting for outbox event of type " + outboxEventType)
}
found, err := findMatchingOutboxEvent(ctx, s, outboxEventType, matchers...)
if err != nil {
return err
}
if found {
return nil
}
select {
case <-ctx.Done():
return ctx.Err()
case <-time.After(interval):
}
}
}
// EventuallyOutbox is a convenience wrapper that uses default polling
// timings aligned with the outbox publisher period.
func EventuallyOutbox(ctx context.Context, s *Server, outboxEventType string, matchers ...PayloadMatcher) error {
return AwaitOutboxEvent(ctx, s, outboxEventType, 5*time.Second, 50*time.Millisecond, matchers...)
}
// findMatchingOutboxEvent queries the DB for pending outbox events of the given type
// and applies matchers to the payload.
func findMatchingOutboxEvent(ctx context.Context, s *Server, outboxEventType string, matchers ...PayloadMatcher) (bool, error) {
db, err := s.Database()
if err != nil {
return false, err
}
defer func(db *bun.DB) { _ = db.Close() }(db)
type outboxRow struct {
EventType string `bun:"event_type"`
Payload json.RawMessage `bun:"payload"`
Status string `bun:"status"`
CreatedAt time.Time `bun:"created_at"`
}
var rows []outboxRow
err = db.NewSelect().
TableExpr("outbox_events").
Where("event_type = ?", outboxEventType).
Where("status = ?", "pending").
Order("created_at DESC").
Limit(50).
Scan(ctx, &rows)
if err != nil {
return false, err
}
for _, row := range rows {
matched := true
for _, m := range matchers {
if err := m.Match(row.Payload); err != nil {
matched = false
break
}
}
if matched {
return true, nil
}
}
return false, nil
}
// MustEventuallyOutbox is a gomega-friendly helper for tests using Gomega directly.
func MustEventuallyOutbox(ctx context.Context, s *Server, outboxEventType string, matchers ...PayloadMatcher) {
gomega.Expect(EventuallyOutbox(ctx, s, outboxEventType, matchers...)).To(gomega.Succeed())
}
func MustOutbox(ctx context.Context, s *Server, outboxEventType string, matchers ...PayloadMatcher) {
success, err := findMatchingOutboxEvent(ctx, s, outboxEventType, matchers...)
gomega.Expect(err).To(gomega.BeNil())
gomega.Expect(success).To(gomega.BeTrue())
}
// CountOutboxEventsByType counts events with the given type in outbox_events table.
// Test helper to standardize counting across E2E tests.
func CountOutboxEventsByType(ctx context.Context, s *Server, eventType string) (int, error) {
db, err := s.Database()
if err != nil {
return 0, err
}
defer func(db *bun.DB) { _ = db.Close() }(db)
type row struct {
Count int `bun:"count"`
}
var r row
err = db.NewSelect().
TableExpr("outbox_events").
ColumnExpr("count(*) as count").
Where("event_type = ?", eventType).
Scan(ctx, &r)
return r.Count, err
}
// LoadOutboxPayloadsByType returns payloads for events of the given type ordered by creation time.
func LoadOutboxPayloadsByType(ctx context.Context, s *Server, eventType string) ([]json.RawMessage, error) {
db, err := s.Database()
if err != nil {
return nil, err
}
defer func(db *bun.DB) { _ = db.Close() }(db)
type row struct {
Payload json.RawMessage `bun:"payload"`
}
var rows []row
err = db.NewSelect().
TableExpr("outbox_events").
Column("payload").
Where("event_type = ?", eventType).
Order("created_at ASC").
Scan(ctx, &rows)
if err != nil {
return nil, err
}
out := make([]json.RawMessage, 0, len(rows))
for _, r := range rows {
out = append(out, r.Payload)
}
return out, nil
}