Files
sure/test/models/recurring_transaction/allocator_concurrency_test.rb
T
Brandon 686205c0ff feat(bills): schema and domain core for the bills subsystem (#3201)
* feat(bills): schema and domain core for the bills subsystem

First of three chunks carved out of #3083. This one carries the schema and the
domain layer: no bills pages, no calendar feed, no assistant tools. Nothing here
is reachable from the UI yet, so it changes no user-visible behavior on its own.

Schema, in a single migration with a full down:

- recurrence_rules, recurring_occurrences, recurring_allocations,
  recurring_price_changes and recurring_match_rejections
- bill columns on recurring_transactions (bill_type, payment_url, autopay,
  notes, anchor and end conditions, weekend adjustment, dedup scope)
- the four data backfills, in their original order

Domain layer:

- Schedule, the pure date PORO every cadence resolves through, and
  FrequencyPreset for the labels
- OccurrenceGenerator, Matcher, Allocator, PriceChangeDetector, Classifier,
  DeclaredBill, HistoryBackfiller and PaycheckPlanner
- Pipeline, tying detection to generation, plus the nightly job and rake task

Existing detection code changed in three places, each a bug this schema exposes:

- Cleaner used a flat two-month staleness threshold, which silently retired
  every quarterly and annual series
- SubscriptionAuditGenerator used a flat 45-day overdue threshold, meaningless
  at both ends of the frequency range
- CashFlowWarningGenerator read one projected entry per series, which only
  equalled the monthly amount because every series was monthly; weekly bills
  were under-counted fourfold in its 30-day projection

The JSON API travels with the model rather than the UI, because the status enum
widens here. The API accepts only active and inactive on write; suggested,
paused and ended are lifecycle states owned by detection, so the documented
enum stays truthful.

Uniqueness keys gain dedup_scope alongside amount, never instead of it: a
series that is not price-forked carries a blank scope, so amount is what keeps
two different prices apart.

Suite 7,550 runs, 0 failures. Rubocop and brakeman clean. Eager loading
verified, and the migration reverses and re-applies. Includes the first review round: orphan repair matches income and refuses coincidental twins, session imports persist occurrence mappings across chunks, semimonthly anchors canonicalize, classifier keywords match whole words, and the down refuses rather than failing when price-forked rows exist.

* Address second review round

Bound the cross-currency default allocation by the entry leftover and the
occurrence remainder, matching the same-currency path. Let keyword stems
carry a suffix again after the word-boundary fix silenced them. Skip an
incoherent recurrence rule row instead of rolling back the whole import.
Check rollback collisions per restored index so a refusal cannot land
after the bills tables are dropped. Replay the closed_at test through a
real second import. Preload the orphan repair associations and move the
allocator errors to locale keys.

* Match index NULL semantics in the rollback collision checks

GROUP BY treats NULLs as equal but the restored unique indexes do not:
account_id is nullable and indexed, so two accountless rows can never
collide under any of them. Excluding NULL accounts keeps the guard from
refusing a rollback PostgreSQL can perform. Verified live both ways:
accountless duplicates roll back, a real collision still refuses.

* Address maintainer review

Scope the payable debt-destination subquery to the row and its family
instead of scanning every account in the installation. Batch the cash
flow generator remaining-amount sums into one grouped query, matching
the two sibling sites. Enforce both window bounds in the after_count
branch so a future-anchored plan cannot leak past the requested end
date. Skip the explicit regeneration when the day column change will
fire the model callback anyway. Add the missing locale entry for the
allocation currency validation.
2026-08-31 23:41:38 +02:00

135 lines
5.1 KiB
Ruby

require "test_helper"
require "concurrent"
# One transaction must never be allocated beyond its own amount, no matter how
# many occurrences want it at once. The occurrence row lock cannot enforce this
# on its own: allocations against two DIFFERENT occurrences take two different
# row locks and never meet, so both read the same stale capacity.
#
# Real threads on real connections, so this needs real commits.
class RecurringTransaction::AllocatorConcurrencyTest < ActiveSupport::TestCase
self.use_transactional_tests = false
setup do
@family = Family.create!(name: "Allocator Race", currency: "USD")
@account = Account.create!(
family: @family, name: "Checking", currency: "USD",
balance: 0, accountable: Depository.new)
@entry = Entry.create!(
account: @account, name: "One payment", date: Date.current,
amount: 1000, currency: "USD", entryable: Transaction.new)
@first = occurrence_for("Rent")
@second = occurrence_for("Storage")
end
teardown do
RecurringAllocation.where(entry_id: @entry.id).delete_all
RecurringOccurrence.where(family_id: @family.id).delete_all
RecurringTransaction.where(family_id: @family.id).delete_all
Entry.where(account_id: @account.id).delete_all
@account.destroy
@family.destroy
end
test "one entry cannot be allocated past its amount by concurrent writers" do
latch = Concurrent::CountDownLatch.new(2)
outcomes = [ @first, @second ].map do |occurrence|
Thread.new do
ActiveRecord::Base.connection_pool.with_connection do
# Both threads reach the capacity read before either inserts, which
# is the interleaving the row lock cannot prevent.
latch.count_down
latch.wait(5)
RecurringTransaction::Allocator.new(occurrence).allocate!(entry: @entry, amount: 600)
:allocated
rescue RecurringTransaction::Allocator::OverAllocationError
:rejected
end
end
end.map(&:value)
allocated = RecurringAllocation.where(entry_id: @entry.id).sum(:allocated_amount)
assert_equal 600, allocated,
"two 600 allocations of a 1000 transaction must not both survive (got #{allocated})"
assert_includes outcomes, :rejected, "the second writer should have been rejected"
end
# The two sides use different write paths, so the guard has to sit on the
# entry rather than on either path.
test "a manual attach and the matcher cannot both spend the same transaction" do
latch = Concurrent::CountDownLatch.new(2)
[
Thread.new do
ActiveRecord::Base.connection_pool.with_connection do
latch.count_down
latch.wait(5)
RecurringTransaction::Allocator.new(@first).allocate!(entry: @entry, amount: 700)
rescue RecurringTransaction::Allocator::OverAllocationError
nil
end
end,
Thread.new do
ActiveRecord::Base.connection_pool.with_connection do
latch.count_down
latch.wait(5)
RecurringTransaction::Allocator.new(@second).allocate_matched!(
entry: @entry, state: "confirmed", confidence: 0.9, signals: {})
rescue RecurringTransaction::Allocator::OverAllocationError
nil
end
end
].each(&:join)
allocated = RecurringAllocation.where(entry_id: @entry.id)
.sum("COALESCE(source_amount, allocated_amount)")
assert_operator allocated, :<=, @entry.amount.abs,
"a 1000 transaction cannot fund more than 1000 of bills"
end
# Deleting a transaction must not take the payment record with it, and must
# never leave an allocation pointing at a row that is gone.
test "deleting a linked transaction leaves the payment intact and unlinked" do
RecurringTransaction::Allocator.new(@first).allocate!(entry: @entry, amount: 400)
allocation = RecurringAllocation.find_by!(entry_id: @entry.id)
@entry.destroy
allocation.reload
assert_nil allocation.entry_id
assert_equal 400, allocation.allocated_amount.to_f
end
test "an exhausted entry allocates nothing rather than the occurrence balance" do
RecurringTransaction::Allocator.new(@first).allocate!(entry: @entry, amount: 1000)
assert_nil RecurringTransaction::Allocator.new(@second).allocate_matched!(
entry: @entry, state: "confirmed", confidence: 0.9, signals: {})
assert_equal 1000, RecurringAllocation.where(entry_id: @entry.id).sum(:allocated_amount)
end
private
def occurrence_for(name)
series = RecurringTransaction.create!(
family: @family, account: @account, name: name, amount: 800,
currency: "USD", expected_day_of_month: 9, status: "active",
manual: true, bill_type: "bill",
last_occurrence_date: Date.current, next_expected_date: Date.current)
due = Date.current.beginning_of_month + 7
RecurringOccurrence.find_or_create_by!(
recurring_transaction: series, family: @family, original_due_on: due) do |row|
row.due_on = due
row.currency = "USD"
row.expected_amount = 800
row.status = "scheduled"
end
end
end