MCPcopy Create free account
hub / github.com/devaccuracy/ledgerforge / startTestAsynqWorker

Function startTestAsynqWorker

transaction_test.go:4760–4824  ·  view source on GitHub ↗

startTestAsynqWorker sets up and starts an Asynq server for testing purposes. It takes the testing object, configuration, LedgerForge instance, and the specific transaction queue name. It returns a cleanup function that should be deferred by the caller to shut down the server.

(t *testing.T, cnf *config.Configuration, ledgerforgeInstance *LedgerForge, transactionQueueName string)

Source from the content-addressed store, hash-verified

4758// It takes the testing object, configuration, LedgerForge instance, and the specific transaction queue name.
4759// It returns a cleanup function that should be deferred by the caller to shut down the server.
4760func startTestAsynqWorker(t *testing.T, cnf *config.Configuration, ledgerforgeInstance *LedgerForge, transactionQueueName string) func() {
4761 redisOption, err := redis_db.ParseRedisURL(cnf.Redis.Dns, false)
4762 require.NoError(t, err, "Failed to parse Redis URL for Asynq")
4763
4764 queues := make(map[string]int)
4765 queues[transactionQueueName] = 1 // Concurrency for the transaction queue
4766
4767 srv := asynq.NewServer(
4768 asynq.RedisClientOpt{
4769 Addr: redisOption.Addr,
4770 Password: redisOption.Password,
4771 DB: redisOption.DB,
4772 TLSConfig: redisOption.TLSConfig,
4773 },
4774 asynq.Config{
4775 Concurrency: 1, // Overall server concurrency
4776 Queues: queues,
4777 Logger: newTestLogger(t),
4778 },
4779 )
4780
4781 mux := asynq.NewServeMux()
4782
4783 // Define transaction processing handler
4784 processTransactionHandler := func(ctx context.Context, task *asynq.Task) error {
4785 var txn model.Transaction
4786 if err := json.Unmarshal(task.Payload(), &txn); err != nil {
4787 t.Logf("TEST_WORKER: Error unmarshalling transaction: %v", err)
4788 return fmt.Errorf("failed to unmarshal transaction: %w", err)
4789 }
4790
4791 t.Logf("TEST_WORKER: Picked up transaction %s (Ref: %s) for processing.", txn.TransactionID, txn.Reference)
4792 processedTxn, err := ledgerforgeInstance.RecordTransaction(ctx, &txn) // Use ledgerforgeInstance from the outer scope
4793 if err != nil {
4794 t.Logf("TEST_WORKER: Error recording transaction %s (Ref: %s): %v", txn.TransactionID, txn.Reference, err)
4795 if strings.Contains(strings.ToLower(err.Error()), "insufficient funds") || strings.Contains(strings.ToLower(err.Error()), "transaction exceeds overdraft limit") {
4796 _, rejectErr := ledgerforgeInstance.RejectTransaction(ctx, &txn, err.Error())
4797 if rejectErr != nil {
4798 t.Logf("TEST_WORKER: Error rejecting transaction %s after processing error: %v", txn.TransactionID, rejectErr)
4799 return fmt.Errorf("processing error: %v, rejection error: %w", err, rejectErr)
4800 }
4801 t.Logf("TEST_WORKER: Rejected transaction %s (Ref: %s) due to: %v", txn.TransactionID, txn.Reference, err)
4802 return nil // Assuming rejection is a final state for this test handler.
4803 }
4804 return err // Allow Asynq to retry for other errors
4805 }
4806 t.Logf("TEST_WORKER: Successfully processed transaction %s (Ref: %s), new ID: %s, new Ref: %s, Status: %s", txn.TransactionID, txn.Reference, processedTxn.TransactionID, processedTxn.Reference, processedTxn.Status)
4807 return nil
4808 }
4809
4810 mux.HandleFunc(transactionQueueName, processTransactionHandler)
4811
4812 go func() {
4813 t.Logf("TEST_WORKER: Starting Asynq server, listening on queue: %s", transactionQueueName)
4814 if err := srv.Run(mux); err != nil {
4815 t.Errorf("TEST_WORKER: Asynq server Run() error: %v", err)
4816 }
4817 }()

Calls 4

RejectTransactionMethod · 0.80
newTestLoggerFunction · 0.70
RecordTransactionMethod · 0.65
ErrorMethod · 0.45

Tested by

no test coverage detected