116 lines
4.2 KiB
Python
116 lines
4.2 KiB
Python
"""Run against settings pointing to an isolated PostgreSQL test database."""
|
|
from concurrent.futures import ThreadPoolExecutor
|
|
from threading import Barrier
|
|
from uuid import uuid4
|
|
|
|
import pytest
|
|
from django.db import close_old_connections, connection
|
|
from django.test import override_settings
|
|
from organizations.models import (
|
|
GovernmentProcurementExtension,
|
|
Organization,
|
|
OrganizationSourceRecord,
|
|
)
|
|
from organizations.source_export_catalog import build_source, issue_ticket
|
|
from rest_framework.test import APIClient
|
|
|
|
from tests.apps.user.factories import UserFactory
|
|
|
|
|
|
@pytest.mark.django_db(transaction=True)
|
|
@pytest.mark.skipif(
|
|
connection.vendor != "postgresql",
|
|
reason="Requires PostgreSQL row locks and independent connections",
|
|
)
|
|
def test_two_simultaneous_native_posts_one_ticket(tmp_path):
|
|
with override_settings(SOURCE_EXPORT_DIRECTORY=str(tmp_path)):
|
|
user = UserFactory.create_superuser()
|
|
organization = Organization.objects.create(
|
|
name="Synthetic race fixture", inn="0012345678", ogrn="1027700132195"
|
|
)
|
|
extension = GovernmentProcurementExtension.objects.create(
|
|
organization=organization
|
|
)
|
|
OrganizationSourceRecord.objects.create(
|
|
extension=extension,
|
|
source="procurements_44fz",
|
|
record_type="procurement",
|
|
external_id=str(uuid4()),
|
|
)
|
|
generation = build_source("government_procurements")
|
|
ticket = issue_ticket(
|
|
user,
|
|
{
|
|
"selection_kind": "file",
|
|
"source_group": "government_procurements",
|
|
"generation_id": str(generation.pk),
|
|
"part_id": generation.manifest["parts"][0]["part_id"],
|
|
"format": "json",
|
|
},
|
|
)
|
|
barrier = Barrier(2)
|
|
|
|
def post():
|
|
close_old_connections()
|
|
try:
|
|
barrier.wait(timeout=10)
|
|
response = APIClient().post(
|
|
"/api/v2/organization-source-records/export-download/",
|
|
"ticket=" + ticket["ticket"],
|
|
content_type="application/x-www-form-urlencoded",
|
|
)
|
|
if response.streaming:
|
|
assert sum(len(chunk) for chunk in response.streaming_content) > 0
|
|
code = response.status_code
|
|
response.close()
|
|
return code
|
|
finally:
|
|
connection.close()
|
|
|
|
with ThreadPoolExecutor(max_workers=2) as workers:
|
|
futures = [workers.submit(post) for _ in range(2)]
|
|
assert sorted(f.result(timeout=30) for f in futures) == [200, 410]
|
|
|
|
|
|
@pytest.mark.django_db(transaction=True)
|
|
@pytest.mark.skipif(
|
|
connection.vendor != "postgresql",
|
|
reason="Requires independent PostgreSQL connections",
|
|
)
|
|
def test_stream_lease_renewal_and_loss_use_independent_database_connection():
|
|
import time
|
|
from datetime import timedelta
|
|
from unittest.mock import patch
|
|
|
|
from django.utils import timezone
|
|
from organizations.models import SourceExportGeneration, SourceExportLease
|
|
from organizations.source_export_catalog import _heartbeat
|
|
|
|
generation = SourceExportGeneration.objects.create(
|
|
source_group="arbitration", data_revision="test"
|
|
)
|
|
expires = timezone.now() + timedelta(minutes=2)
|
|
lease = SourceExportLease.objects.create(
|
|
generation=generation, issuer_id="test", kind="stream", expires_at=expires
|
|
)
|
|
|
|
def renew():
|
|
return SourceExportLease.objects.filter(
|
|
pk=lease.pk, expires_at__gt=timezone.now()
|
|
).update(expires_at=timezone.now() + timedelta(minutes=10))
|
|
|
|
# Accelerate only the timer; connection, SQL and expiry/ownership checks are real.
|
|
with patch(
|
|
"organizations.source_export_catalog.HEARTBEAT_RENEW_SECONDS", 0.05
|
|
), _heartbeat(renew) as lost:
|
|
deadline = time.monotonic() + 5
|
|
while time.monotonic() < deadline:
|
|
lease.refresh_from_db()
|
|
if lease.expires_at > expires:
|
|
break
|
|
time.sleep(0.02)
|
|
assert lease.expires_at > expires
|
|
assert not lost.is_set()
|
|
lease.delete()
|
|
assert lost.wait(timeout=5)
|