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

Function StartTestAsynqWorker

api/transaction_api_test.go:102–166  ·  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.LedgerForge, transactionQueueName string)

Source from the content-addressed store, hash-verified

100// It takes the testing object, configuration, LedgerForge instance, and the specific transaction queue name.
101// It returns a cleanup function that should be deferred by the caller to shut down the server.
102func StartTestAsynqWorker(t *testing.T, cnf *config.Configuration, ledgerforgeInstance *ledgerforge.LedgerForge, transactionQueueName string) func() {
103 redisOption, err := redis_db.ParseRedisURL(cnf.Redis.Dns, cnf.Redis.SkipTLSVerify)
104 require.NoError(t, err, "Failed to parse Redis URL for Asynq")
105
106 queues := make(map[string]int)
107 queues[transactionQueueName] = 1 // Concurrency for the transaction queue
108
109 srv := asynq.NewServer(
110 asynq.RedisClientOpt{
111 Addr: redisOption.Addr,
112 Password: redisOption.Password,
113 DB: redisOption.DB,
114 TLSConfig: redisOption.TLSConfig,
115 },
116 asynq.Config{
117 Concurrency: 1, // Overall server concurrency
118 Queues: queues,
119 Logger: newTestLogger(t),
120 },
121 )
122
123 mux := asynq.NewServeMux()
124
125 // Define transaction processing handler
126 processTransactionHandler := func(ctx context.Context, task *asynq.Task) error {
127 var txn model.Transaction
128 if err := json.Unmarshal(task.Payload(), &txn); err != nil {
129 t.Logf("TEST_WORKER: Error unmarshalling transaction: %v", err)
130 return fmt.Errorf("failed to unmarshal transaction: %w", err)
131 }
132
133 t.Logf("TEST_WORKER: Picked up transaction %s (Ref: %s) for processing.", txn.TransactionID, txn.Reference)
134 processedTxn, err := ledgerforgeInstance.RecordTransaction(ctx, &txn) // Use ledgerforgeInstance from the outer scope
135 if err != nil {
136 t.Logf("TEST_WORKER: Error recording transaction %s (Ref: %s): %v", txn.TransactionID, txn.Reference, err)
137 if strings.Contains(strings.ToLower(err.Error()), "insufficient funds") || strings.Contains(strings.ToLower(err.Error()), "transaction exceeds overdraft limit") {
138 _, rejectErr := ledgerforgeInstance.RejectTransaction(ctx, &txn, err.Error())
139 if rejectErr != nil {
140 t.Logf("TEST_WORKER: Error rejecting transaction %s after processing error: %v", txn.TransactionID, rejectErr)
141 return fmt.Errorf("processing error: %v, rejection error: %w", err, rejectErr)
142 }
143 t.Logf("TEST_WORKER: Rejected transaction %s (Ref: %s) due to: %v", txn.TransactionID, txn.Reference, err)
144 return nil // Assuming rejection is a final state for this test handler.
145 }
146 return err // Allow Asynq to retry for other errors
147 }
148 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)
149 return nil
150 }
151
152 mux.HandleFunc(transactionQueueName, processTransactionHandler)
153
154 go func() {
155 t.Logf("TEST_WORKER: Starting Asynq server, listening on queue: %s", transactionQueueName)
156 if err := srv.Run(mux); err != nil {
157 t.Errorf("TEST_WORKER: Asynq server Run() error: %v", err)
158 }
159 }()

Calls 4

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

Tested by

no test coverage detected