pollForTransactionStatus polls for a transaction until it reaches the expected status or a timeout occurs.
(ctx context.Context, ds database.IDataSource, transactionRef string, expectedStatus string, pollInterval time.Duration, timeoutDuration time.Duration)
| 92 | |
| 93 | // pollForTransactionStatus polls for a transaction until it reaches the expected status or a timeout occurs. |
| 94 | func pollForTransactionStatus(ctx context.Context, ds database.IDataSource, transactionRef string, expectedStatus string, pollInterval time.Duration, timeoutDuration time.Duration) (*model.Transaction, error) { |
| 95 | timeoutCtx, cancel := context.WithTimeout(ctx, timeoutDuration) |
| 96 | defer cancel() |
| 97 | |
| 98 | ticker := time.NewTicker(pollInterval) |
| 99 | defer ticker.Stop() |
| 100 | |
| 101 | for { |
| 102 | select { |
| 103 | case <-timeoutCtx.Done(): |
| 104 | return nil, fmt.Errorf("timed out waiting for transaction ref %s to reach status %s: %w", transactionRef, expectedStatus, timeoutCtx.Err()) |
| 105 | case <-ticker.C: |
| 106 | txn, err := ds.GetTransactionByRef(timeoutCtx, transactionRef) // Use timeoutCtx for the DB call as well |
| 107 | if err != nil { |
| 108 | // If the error is that the transaction is not found, we should continue polling. |
| 109 | // For other errors, the outer timeout will eventually catch persistent issues. |
| 110 | continue |
| 111 | } |
| 112 | // If err is nil, txn is a valid model.Transaction struct. |
| 113 | if txn.Status == expectedStatus { |
| 114 | return &txn, nil // Return a pointer to the transaction |
| 115 | } |
| 116 | } |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | func TestRecordTransaction(t *testing.T) { |
| 121 | cnf := &config.Configuration{ |
no test coverage detected