Skip to content

Commit

Permalink
Add unit test for single
Browse files Browse the repository at this point in the history
  • Loading branch information
pauluswi committed Nov 17, 2024
1 parent b4aa9da commit 32e2a64
Showing 1 changed file with 64 additions and 0 deletions.
64 changes: 64 additions & 0 deletions single/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package main

import (
"sync"
"testing"
)

func TestProcessTransaction(t *testing.T) {
// Setup accounts
accounts := map[string]*Account{
"11111": {AccountNumber: "11111", Balance: 10000},
"22222": {AccountNumber: "22222", Balance: 20000},
}

// List of transactions
transactions := []Transaction{
{"11111", -200}, // Debit
{"11111", 300}, // Credit
{"22222", -500}, // Debit
{"22222", -3000}, // Insufficient funds
{"11111", 100}, // Credit
}

// Expected final balances
expectedBalances := map[string]float64{
"11111": 10200,
"22222": 16500,
}

var wg sync.WaitGroup

// Process transactions concurrently
for _, tr := range transactions {
tr := tr // Capture range variable for goroutine
account, exists := accounts[tr.AccountNumber]
if !exists {
t.Errorf("Account %s not found", tr.AccountNumber)
continue
}

wg.Add(1)
go func(acc *Account, amount float64) {
defer wg.Done() // Ensure Done is called even if ProcessTransaction fails
acc.mu.Lock()
if amount < 0 && acc.Balance+amount < 0 {
acc.mu.Unlock()
t.Errorf("Insufficient funds for account %s", acc.AccountNumber)
return
}
acc.Balance += amount
acc.mu.Unlock()
}(account, tr.Amount)
}

wg.Wait() // Wait for all transactions to complete

// Verify final balances
for accountNumber, account := range accounts {
if account.Balance != expectedBalances[accountNumber] {
t.Errorf("Account %s: expected balance %.2f, got %.2f",
account.AccountNumber, expectedBalances[accountNumber], account.Balance)
}
}
}

0 comments on commit 32e2a64

Please sign in to comment.