From 2ee3e5ddade23ceb0d338065d7dcb2ab16ad0ff7 Mon Sep 17 00:00:00 2001 From: catborise Date: Sun, 8 Nov 2026 20:51:50 +0300 Subject: [PATCH] test(quota): the concurrent resize test checks what happened to each request The test asserted only that one resize ran: a request that failed for any other reason in its thread, unseen, passed it as well. It now collects each request's messages, or its exception, and asserts that one resized and the other was refused by the user's quota lock or by the quota. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Kk4h2TcUiaWmU5MUszYSea --- instances/tests/test_quota.py | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/instances/tests/test_quota.py b/instances/tests/test_quota.py index 2fa10fea..bcf1ae76 100644 --- a/instances/tests/test_quota.py +++ b/instances/tests/test_quota.py @@ -330,11 +330,12 @@ class QuotaRaceTestCase(TransactionTestCase): for vm in self.vms: UserInstance.objects.create(user=self.user, instance=vm, is_change=True) self.allocated = 2 # two VMs with one vCPU each + self.outcomes = [] # the messages of each resize def fake_quota(self, user, instance, cpu, memory, disk): seen = self.allocated time.sleep(0.3) # widen the window between the check and the change - return "cpu" if seen + cpu > QUOTA_CPUS else "" + return QuotaExceeded("cpu", seen + cpu, QUOTA_CPUS) if seen + cpu > QUOTA_CPUS else None def fake_resize(self, cur_vcpu, vcpu): self.allocated += int(vcpu) - 1 @@ -342,8 +343,13 @@ class QuotaRaceTestCase(TransactionTestCase): def resize(self, vm): client = Client() client.force_login(self.user) - client.post(reverse("instances:resizevm_cpu", args=[vm.id]), {"vcpu": 3, "cur_vcpu": 3}) - connection.close() + try: + response = client.post(reverse("instances:resizevm_cpu", args=[vm.id]), {"vcpu": 3, "cur_vcpu": 3}) + self.outcomes.append(" ".join(str(m) for m in get_messages(response.wsgi_request))) + except Exception as e: # a thread's exception would be lost + self.outcomes.append(repr(e)) + finally: + connection.close() def test_second_concurrent_resize_is_refused(self): with patch("instances.views.utils.check_user_quota", self.fake_quota), patch( @@ -359,3 +365,8 @@ class QuotaRaceTestCase(TransactionTestCase): t.join() self.assertEqual(wvm.return_value.resize_cpu.call_count, 1) self.assertLessEqual(self.allocated, QUOTA_CPUS) + # one resized; the other was refused, by the user's quota lock or (once + # the first resize is done) by the quota, and did not fail otherwise + resized, refused = sorted(self.outcomes, key=lambda outcome: not outcome.startswith("vCPUs set")) + self.assertEqual(resized, "vCPUs set to 3 (maximum 3)") + self.assertRegex(refused, r"^(A short operation is in progress|The vCPU quota is reached)")