Skip to content

Commit 75e1bf3

Browse files
bjesterclaude
andcommitted
Add characterization tests for legacy network sync
Pins what a full push and a full pull send and store when the server lacks ASYNC_OPERATIONS, so the refactors of the LegacyNetwork* operations can show they change neither. The tests run against a live server that emulates a morango version before 0.6.0: the MORANGO_TEST_LEGACY_SERVER variable turns off both ASYNC_OPERATIONS and FSIC_V2_FORMAT on the server, and LiveServer now takes extra environment variables for that. The client keeps its full set of capabilities, as a current Kolibri does when it syncs with an older one. Reducing only the client capabilities, as #364 first proposed, fails today on the server for both push and pull (#416). The expected lists show that the client creates the transfer session and sends no stage updates for serializing, queuing, dequeuing, or deserializing. The legacy pull moves 11 records instead of 10: the client creates the transfer session before it serializes, so it sends an empty client_fsic, and the server queues the user from the initial pull again. Closes #364 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 2b8a92b commit 75e1bf3

3 files changed

Lines changed: 126 additions & 26 deletions

File tree

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
1+
import os
2+
13
from .settings import * # noqa
24

35
DATABASES = {
46
"default": DATABASES["default2"], # noqa
57
}
8+
9+
# emulates a server on a morango version before 0.6.0, which lacks both of these capabilities
10+
if os.environ.get("MORANGO_TEST_LEGACY_SERVER"):
11+
MORANGO_DISALLOW_ASYNC_OPERATIONS = True
12+
MORANGO_DISABLE_FSIC_V2_FORMAT = True

‎tests/testapp/tests/integration/helpers.py‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,10 @@ def second_environment():
3939

4040

4141
class LiveServer:
42-
def __init__(self):
42+
def __init__(self, env=None):
4343
self.env = os.environ.copy()
4444
self.env["MORANGO_SYSTEM_ID"] = SECOND_SYSTEM_ID
45+
self.env.update(env or {})
4546
self.port = get_free_tcp_port()
4647
self.host = "127.0.0.1"
4748
self.start()
@@ -94,11 +95,13 @@ class BasePushPullClientTestCase(TransactionTestCase):
9495

9596
profile = "facilitydata"
9697
databases = ["default", SECOND_TEST_DATABASE]
98+
# extra environment variables for the live server process
99+
server_env = {}
97100

98101
@classmethod
99102
def setUpClass(cls):
100103
super(TransactionTestCase, cls).setUpClass()
101-
cls.server = LiveServer()
104+
cls.server = LiveServer(env=cls.server_env)
102105

103106
@classmethod
104107
def tearDownClass(cls):

‎tests/testapp/tests/integration/test_network_characterization.py‎

Lines changed: 114 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""
2-
Characterization tests that pin the HTTP requests and the final state of a network push and pull
3-
that use `ASYNC_OPERATIONS`. Refactors of the sync layer must pass these tests without edits.
2+
Characterization tests that pin the HTTP requests and the final state of a network push and pull,
3+
both with `ASYNC_OPERATIONS` and with the legacy flow of a server that lacks it. Refactors of the
4+
sync layer must pass these tests without edits.
45
"""
56

67
import gzip
@@ -14,6 +15,7 @@
1415

1516
from morango.constants import transfer_stages
1617
from morango.constants import transfer_statuses
18+
from morango.constants.capabilities import ASYNC_OPERATIONS
1719
from morango.models.core import Buffer
1820
from morango.models.core import Store
1921
from morango.models.core import TransferSession
@@ -283,9 +285,55 @@ def pull_buffers(offset):
283285
]
284286

285287

286-
class NetworkCharacterizationTestCase(BasePushPullClientTestCase):
288+
# The legacy server serializes when the client creates the transfer session, and dequeues and
289+
# deserializes when the client closes it, so the client sends no stage updates for those stages.
290+
# The client tells the server how many records it queued, since the server did not queue them.
291+
LEGACY_PUSH_REQUESTS = [
292+
# initializing
293+
create_transfer_session(push=True),
294+
# queuing
295+
update_transfer_session(("records_total",), records_total=10),
296+
# transferring, where the server advances its transfer session as it receives each chunk
297+
push_buffers(3),
298+
push_buffers(3),
299+
push_buffers(3),
300+
push_buffers(1),
301+
# cleanup
302+
close_transfer_session(),
303+
]
304+
305+
# The legacy server serializes and queues when the client creates the transfer session, and returns
306+
# the number of records it queued in that response. The client creates the transfer session before
307+
# it serializes, so it sends an empty `client_fsic` and the server queues the user from the initial
308+
# pull in `setUp` again, for 11 records in chunks of 3.
309+
LEGACY_PULL_REQUESTS = [
310+
# initializing
311+
create_transfer_session(push=False),
312+
# transferring, where the client reports its progress after each chunk
313+
pull_buffers(offset=0),
314+
update_transfer_session(
315+
PULL_TRANSFER_KEYS, records_transferred=3, transfer_stage=transfer_stages.TRANSFERRING
316+
),
317+
pull_buffers(offset=3),
318+
update_transfer_session(
319+
PULL_TRANSFER_KEYS, records_transferred=6, transfer_stage=transfer_stages.TRANSFERRING
320+
),
321+
pull_buffers(offset=6),
322+
update_transfer_session(
323+
PULL_TRANSFER_KEYS, records_transferred=9, transfer_stage=transfer_stages.TRANSFERRING
324+
),
325+
pull_buffers(offset=9),
326+
update_transfer_session(
327+
PULL_TRANSFER_KEYS, records_transferred=11, transfer_stage=transfer_stages.TRANSFERRING
328+
),
329+
# cleanup
330+
close_transfer_session(),
331+
]
332+
333+
334+
class BaseNetworkCharacterizationTestCase(BasePushPullClientTestCase):
287335
def setUp(self):
288-
super(NetworkCharacterizationTestCase, self).setUp()
336+
super(BaseNetworkCharacterizationTestCase, self).setUp()
289337
# the expected requests depend on the chunk size, so pin it here rather than rely on
290338
# the harness
291339
self.conn.chunk_size = CHUNK_SIZE
@@ -316,15 +364,7 @@ def assertTransferSessionFinalState(self, transfer_session, records_total):
316364
self.assertFalse(transfer_session.active)
317365
self.assertEqual(0, Buffer.objects.filter(transfer_session=transfer_session).count())
318366

319-
def test_async_push(self):
320-
for _ in range(5):
321-
SummaryLog.objects.create(user=self.local_user)
322-
InteractionLog.objects.create(user=self.local_user)
323-
324-
client = self.client.get_push_client()
325-
requests = self._sync(client)
326-
self.assertEqual(PUSH_REQUESTS, requests)
327-
367+
def _assertPushFinalState(self, client):
328368
transfer_session = client.context.transfer_session
329369
self.assertTransferSessionFinalState(transfer_session, records_total=10)
330370

@@ -339,18 +379,9 @@ def test_async_push(self):
339379
self.assertEqual(5, SummaryLog.objects.filter(user=self.remote_user).count())
340380
self.assertEqual(5, InteractionLog.objects.filter(user=self.remote_user).count())
341381

342-
def test_async_pull(self):
343-
with second_environment():
344-
for _ in range(5):
345-
SummaryLog.objects.create(user=self.remote_user)
346-
InteractionLog.objects.create(user=self.remote_user)
347-
348-
client = self.client.get_pull_client()
349-
requests = self._sync(client)
350-
self.assertEqual(PULL_REQUESTS, requests)
351-
382+
def _assertPullFinalState(self, client, records_total=10):
352383
transfer_session = client.context.transfer_session
353-
self.assertTransferSessionFinalState(transfer_session, records_total=10)
384+
self.assertTransferSessionFinalState(transfer_session, records_total=records_total)
354385

355386
# the client reports its byte counts to the server, flipped to the server's point of view
356387
remote_transfer_session = self._remote_transfer_session(transfer_session)
@@ -361,3 +392,62 @@ def test_async_pull(self):
361392
self.assertEqual(11, self._store_count())
362393
self.assertEqual(5, SummaryLog.objects.filter(user=self.local_user).count())
363394
self.assertEqual(5, InteractionLog.objects.filter(user=self.local_user).count())
395+
396+
def _create_local_logs(self):
397+
for _ in range(5):
398+
SummaryLog.objects.create(user=self.local_user)
399+
InteractionLog.objects.create(user=self.local_user)
400+
401+
def _create_remote_logs(self):
402+
with second_environment():
403+
for _ in range(5):
404+
SummaryLog.objects.create(user=self.remote_user)
405+
InteractionLog.objects.create(user=self.remote_user)
406+
407+
408+
class NetworkCharacterizationTestCase(BaseNetworkCharacterizationTestCase):
409+
def test_async_push(self):
410+
self._create_local_logs()
411+
412+
client = self.client.get_push_client()
413+
self.assertIn(ASYNC_OPERATIONS, client.context.capabilities)
414+
requests = self._sync(client)
415+
self.assertEqual(PUSH_REQUESTS, requests)
416+
self._assertPushFinalState(client)
417+
418+
def test_async_pull(self):
419+
self._create_remote_logs()
420+
421+
client = self.client.get_pull_client()
422+
self.assertIn(ASYNC_OPERATIONS, client.context.capabilities)
423+
requests = self._sync(client)
424+
self.assertEqual(PULL_REQUESTS, requests)
425+
self._assertPullFinalState(client)
426+
427+
428+
class LegacyNetworkCharacterizationTestCase(BaseNetworkCharacterizationTestCase):
429+
"""
430+
Syncs with a server that emulates a morango version before 0.6.0, so the server lacks
431+
`ASYNC_OPERATIONS` and `FSIC_V2_FORMAT`. The client keeps its full set of capabilities.
432+
"""
433+
434+
server_env = {"MORANGO_TEST_LEGACY_SERVER": "1"}
435+
436+
def test_legacy_push(self):
437+
self._create_local_logs()
438+
439+
client = self.client.get_push_client()
440+
self.assertNotIn(ASYNC_OPERATIONS, client.context.capabilities)
441+
requests = self._sync(client)
442+
self.assertEqual(LEGACY_PUSH_REQUESTS, requests)
443+
self._assertPushFinalState(client)
444+
445+
def test_legacy_pull(self):
446+
self._create_remote_logs()
447+
448+
client = self.client.get_pull_client()
449+
self.assertNotIn(ASYNC_OPERATIONS, client.context.capabilities)
450+
requests = self._sync(client)
451+
self.assertEqual(LEGACY_PULL_REQUESTS, requests)
452+
# the server sent the user from the initial pull again, which the client already had
453+
self._assertPullFinalState(client, records_total=11)

0 commit comments

Comments
 (0)