@@ -880,7 +880,7 @@ def test_first_time(_global_cache, _global_get, global_set_if_not_exists, uuid):
880880
881881 assert _cache .global_lock_for_write (b"key" ).result () == b".arandomuuid"
882882 _global_get .assert_called_once_with (b"key" )
883- global_set_if_not_exists .assert_called_once_with (b"key" , lock_value , expires = 32 )
883+ global_set_if_not_exists .assert_called_once_with (b"key" , lock_value , expires = 64 )
884884
885885 @staticmethod
886886 @mock .patch ("google.cloud.ndb._cache.uuid" )
@@ -922,8 +922,8 @@ def test_not_first_time_fail_once(
922922 )
923923 _global_compare_and_swap .assert_has_calls (
924924 [
925- mock .call (b"key" , new_lock_value , expires = 32 ),
926- mock .call (b"key" , new_lock_value , expires = 32 ),
925+ mock .call (b"key" , new_lock_value , expires = 64 ),
926+ mock .call (b"key" , new_lock_value , expires = 64 ),
927927 ]
928928 )
929929
@@ -955,7 +955,102 @@ def test_last_time(
955955 assert _cache .global_unlock_for_write (b"key" , lock ).result () is None
956956 _global_get .assert_called_once_with (b"key" )
957957 _global_watch .assert_called_once_with (b"key" , lock_value )
958- _global_compare_and_swap .assert_called_once_with (b"key" , b"" , expires = 32 )
958+ _global_compare_and_swap .assert_called_once_with (b"key" , b"" , expires = 64 )
959+
960+ @staticmethod
961+ @mock .patch ("google.cloud.ndb._cache.uuid" )
962+ @mock .patch ("google.cloud.ndb._cache._global_compare_and_swap" )
963+ @mock .patch ("google.cloud.ndb._cache._global_watch" )
964+ @mock .patch ("google.cloud.ndb._cache._global_get" )
965+ @mock .patch ("google.cloud.ndb._cache._global_cache" )
966+ def test_lock_missing (
967+ _global_cache , _global_get , _global_watch , _global_compare_and_swap , uuid
968+ ):
969+ lock = b".arandomuuid"
970+
971+ _global_cache .return_value = mock .Mock (
972+ transient_errors = (),
973+ strict_write = False ,
974+ spec = ("transient_errors" , "strict_write" ),
975+ )
976+
977+ lock_value = _cache ._LOCKED_FOR_WRITE + b".adifferentlock"
978+ _global_get .return_value = _future_result (lock_value )
979+ _global_watch .return_value = _future_result (None )
980+ _global_compare_and_swap .return_value = _future_result (True )
981+
982+ with warnings .catch_warnings (record = True ) as logged :
983+ assert _cache .global_unlock_for_write (b"key" , lock ).result () is None
984+ logged = [
985+ warning for warning in logged if warning .category is RuntimeWarning
986+ ]
987+ assert len (logged ) == 1
988+
989+ _global_get .assert_called_once_with (b"key" )
990+ _global_watch .assert_not_called ()
991+ _global_compare_and_swap .assert_not_called ()
992+
993+ @staticmethod
994+ @mock .patch ("google.cloud.ndb._cache.uuid" )
995+ @mock .patch ("google.cloud.ndb._cache.global_set_if_not_exists" )
996+ @mock .patch ("google.cloud.ndb._cache._global_get" )
997+ @mock .patch ("google.cloud.ndb._cache._global_cache" )
998+ def test_no_value_in_cache (
999+ _global_cache , _global_get , global_set_if_not_exists , uuid
1000+ ):
1001+ lock = b".arandomuuid"
1002+
1003+ _global_cache .return_value = mock .Mock (
1004+ transient_errors = (),
1005+ strict_write = False ,
1006+ spec = ("transient_errors" , "strict_write" ),
1007+ )
1008+
1009+ _global_get .return_value = _future_result (None )
1010+ global_set_if_not_exists .return_value = _future_result (True )
1011+
1012+ with warnings .catch_warnings (record = True ) as logged :
1013+ assert _cache .global_unlock_for_write (b"key" , lock ).result () is None
1014+ logged = [
1015+ warning for warning in logged if warning .category is RuntimeWarning
1016+ ]
1017+ assert len (logged ) == 1
1018+
1019+ _global_get .assert_called_once_with (b"key" )
1020+ global_set_if_not_exists .assert_not_called ()
1021+
1022+ @staticmethod
1023+ @mock .patch ("google.cloud.ndb._cache.uuid" )
1024+ @mock .patch ("google.cloud.ndb._cache._global_compare_and_swap" )
1025+ @mock .patch ("google.cloud.ndb._cache._global_watch" )
1026+ @mock .patch ("google.cloud.ndb._cache._global_get" )
1027+ @mock .patch ("google.cloud.ndb._cache._global_cache" )
1028+ def test_lock_overwritten (
1029+ _global_cache , _global_get , _global_watch , _global_compare_and_swap , uuid
1030+ ):
1031+ lock = b".arandomuuid"
1032+
1033+ _global_cache .return_value = mock .Mock (
1034+ transient_errors = (),
1035+ strict_write = False ,
1036+ spec = ("transient_errors" , "strict_write" ),
1037+ )
1038+
1039+ lock_value = b"SOMERANDOMVALUE"
1040+ _global_get .return_value = _future_result (lock_value )
1041+ _global_watch .return_value = _future_result (None )
1042+ _global_compare_and_swap .return_value = _future_result (True )
1043+
1044+ with warnings .catch_warnings (record = True ) as logged :
1045+ assert _cache .global_unlock_for_write (b"key" , lock ).result () is None
1046+ logged = [
1047+ warning for warning in logged if warning .category is RuntimeWarning
1048+ ]
1049+ assert len (logged ) == 1
1050+
1051+ _global_get .assert_called_once_with (b"key" )
1052+ _global_watch .assert_called_once_with (b"key" , lock_value )
1053+ _global_compare_and_swap .assert_called_once_with (b"key" , b"" , expires = 64 )
9591054
9601055 @staticmethod
9611056 @mock .patch ("google.cloud.ndb._cache.uuid" )
@@ -1023,8 +1118,8 @@ def test_not_last_time_fail_once(
10231118 )
10241119 _global_compare_and_swap .assert_has_calls (
10251120 [
1026- mock .call (b"key" , new_lock_value , expires = 32 ),
1027- mock .call (b"key" , new_lock_value , expires = 32 ),
1121+ mock .call (b"key" , new_lock_value , expires = 64 ),
1122+ mock .call (b"key" , new_lock_value , expires = 64 ),
10281123 ]
10291124 )
10301125
0 commit comments