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
67import gzip
1415
1516from morango .constants import transfer_stages
1617from morango .constants import transfer_statuses
18+ from morango .constants .capabilities import ASYNC_OPERATIONS
1719from morango .models .core import Buffer
1820from morango .models .core import Store
1921from 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