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
@@ -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