def test_delete_outdated_users(gatekeeper): # This tests the deletion of users whose subscription has outdated (subscription expires now) # Create some users with associated data and add them to the gatekeeper users = {} current_height = gatekeeper.block_processor.get_block_count() for _ in range(10): appointments = { get_random_value_hex(32): Appointment(get_random_value_hex(32), None, None) } user_id = get_random_value_hex(16) user_info = UserInfo(available_slots=100, subscription_expiry=current_height, appointments=appointments) users[user_id] = user_info gatekeeper.registered_users[user_id] = user_info # Get a list of the users that should be deleted at this block height (must match the newly generated ones) users_to_be_deleted = gatekeeper.get_outdated_user_ids( current_height + gatekeeper.expiry_delta) assert users_to_be_deleted == list(users.keys()) # Delete the users Cleaner.delete_outdated_users(users_to_be_deleted, gatekeeper.registered_users, gatekeeper.user_db) # Check that the users are not in the gatekeeper anymore for user_id in users_to_be_deleted: assert user_id not in gatekeeper.registered_users assert not gatekeeper.user_db.load_user(user_id)
def test_delete_outdated_users(users_db_manager): # Tests the deletion of users whose subscription has outdated (subscription expires now) # Let's mock adding some users and appointments to the Gatekeeper (memory and db) registered_users = setup_users(users_db_manager, MAX_ITEMS) # Delete the users to_be_deleted = list(registered_users.keys()) Cleaner.delete_outdated_users(to_be_deleted, registered_users, users_db_manager) # Check that the users are not in the gatekeeper anymore for user_id in to_be_deleted: assert user_id not in registered_users assert not users_db_manager.load_user(user_id)
def manage_subscription_expiry(self): """ Manages the subscription expiry of the registered users. Subscriptions are not deleted straightaway for two purposes: - First, it gives time to the ``Watcher`` and the ``Responder`` to query the necessary data for housekeeping, and gives some reorg protection. - Second, it gives a grace time to the user to renew their subscription before it is irrevocably deleted. """ while True: block_hash = self.block_queue.get() # When the ChainMonitor is stopped, a final ChainMonitor.END_MESSAGE message is sent if block_hash == ChainMonitor.END_MESSAGE: break # Expired user deletion is delayed. Users are deleted when their subscription is outdated, not expired. block_height = self.block_processor.get_block(block_hash, blocking=True).get("height") self.update_outdated_users_cache(block_height) Cleaner.delete_outdated_users(self.get_outdated_user_ids(block_height), self.registered_users, self.user_db)