Publish reviewed history-free open-source snapshot (2026-10-12)

This commit is contained in:
Zonghengjia Open Source
2026-10-11 23:23:20 +00:00
commit 901f27baf5
301 changed files with 32378 additions and 0 deletions
@@ -0,0 +1,51 @@
"""Sequential invariants only; SQLite cannot certify concurrent row locking."""
from unittest.mock import patch
from django.contrib.auth import get_user_model
from django.db import connection, transaction
from django.db.models.query import QuerySet
from django.test import TestCase
from control.models import Plan, Subscription, UsageRecord
from control.signals import recompute_subscription_usage
from control.usage_accounting import lock_usage_subscriptions, non_key_lock_options
class UsageLockOrderTests(TestCase):
def setUp(self):
self.owner = get_user_model().objects.create_user(username="lock-fixture")
self.plan = Plan.objects.create(name="Fixture", slug="lock-fixture", audience=Plan.Audience.ALL)
self.subscription = Subscription.objects.create(user=self.owner, plan=self.plan)
def test_parent_lock_is_requested_before_aggregate_and_stale_total_is_repaired(self):
Subscription.objects.filter(pk=self.subscription.pk).update(upload_bytes=999, download_bytes=999)
sequence = []
original_lock, original_aggregate = QuerySet.select_for_update, QuerySet.aggregate
def locked(queryset, *args, **kwargs):
if queryset.model is Subscription:
self.assertTrue(connection.in_atomic_block)
sequence.append("parent-lock")
return original_lock(queryset, *args, **kwargs)
def aggregated(queryset, *args, **kwargs):
if queryset.model is UsageRecord:
self.assertEqual(sequence, ["parent-lock"])
self.assertTrue(connection.in_atomic_block)
sequence.append("aggregate")
return original_aggregate(queryset, *args, **kwargs)
with patch.object(QuerySet, "select_for_update", new=locked), patch.object(QuerySet, "aggregate", new=aggregated):
recompute_subscription_usage(self.subscription.pk)
self.assertEqual(sequence, ["parent-lock", "aggregate"])
self.subscription.refresh_from_db()
self.assertEqual(self.subscription.used_bytes, 0)
def test_multi_subscription_lock_order_is_sorted_and_deduplicated(self):
another = Subscription.objects.create(user=self.owner, plan=self.plan)
with transaction.atomic():
locked = lock_usage_subscriptions([another.pk, self.subscription.pk, another.pk])
self.assertEqual([item.pk for item in locked], sorted([self.subscription.pk, another.pk]))
def test_no_key_lock_only_requested_on_supported_databases(self):
with patch.object(connection.features, "has_select_for_no_key_update", True):
self.assertEqual(non_key_lock_options(), {"no_key": True})
with patch.object(connection.features, "has_select_for_no_key_update", False):
self.assertEqual(non_key_lock_options(), {})