Skip to content

Commit d4c5757

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 004444d commit d4c5757

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
@@ -43,9 +43,10 @@ def second_environment():
4343

4444

4545
class LiveServer:
46-
def __init__(self):
46+
def __init__(self, env=None):
4747
self.env = os.environ.copy()
4848
self.env["MORANGO_SYSTEM_ID"] = SECOND_SYSTEM_ID
49+
self.env.update(env or {})
4950
self.port = get_free_tcp_port()
5051
self.host = "127.0.0.1"
5152
self.start()
@@ -98,11 +99,13 @@ class BasePushPullClientTestCase(TransactionTestCase):
9899

99100
profile = "facilitydata"
100101
databases = ["default", SECOND_TEST_DATABASE]
102+
# extra environment variables for the live server process
103+
server_env = {}
101104

102105
@classmethod
103106
def setUpClass(cls):
104107
super(TransactionTestCase, cls).setUpClass()
105-
cls.server = LiveServer()
108+
cls.server = LiveServer(env=cls.server_env)
106109

107110
@classmethod
108111
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
@@ -284,9 +286,55 @@ def pull_buffers(offset):
284286
]
285287

286288

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

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

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

343-
def test_async_pull(self):
344-
with second_environment():
345-
for _ in range(5):
346-
SummaryLog.objects.create(user=self.remote_user)
347-
InteractionLog.objects.create(user=self.remote_user)
348-
349-
client = self.client.get_pull_client()
350-
requests = self._sync(client)
351-
self.assertEqual(PULL_REQUESTS, requests)
352-
383+
def _assertPullFinalState(self, client, records_total=10):
353384
transfer_session = client.context.transfer_session
354-
self.assertTransferSessionFinalState(transfer_session, records_total=10)
385+
self.assertTransferSessionFinalState(transfer_session, records_total=records_total)
355386

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

0 commit comments

Comments
 (0)