def test_bandwidth_exceeded(self):

        RESTRICTIVE_DATA_RULES = HydrusNetworking.BandwidthRules()

        RESTRICTIVE_DATA_RULES.AddRule(HC.BANDWIDTH_TYPE_DATA, None, 10)

        HYDRUS_NETWORK_CONTEXT = ClientNetworkingContexts.NetworkContext(
            CC.NETWORK_CONTEXT_HYDRUS, MOCK_HYDRUS_SERVICE_KEY)

        #

        job = self._GetJob()

        self.assertEqual(job.TryToStartBandwidth(), True)

        job.engine.bandwidth_manager.ReportDataUsed([HYDRUS_NETWORK_CONTEXT],
                                                    50)

        job.engine.bandwidth_manager.SetRules(HYDRUS_NETWORK_CONTEXT,
                                              RESTRICTIVE_DATA_RULES)

        self.assertEqual(job.TryToStartBandwidth(), False)

        #

        job = self._GetJob(for_login=True)

        self.assertEqual(job.TryToStartBandwidth(), True)

        job.engine.bandwidth_manager.ReportDataUsed([HYDRUS_NETWORK_CONTEXT],
                                                    50)

        job.engine.bandwidth_manager.SetRules(HYDRUS_NETWORK_CONTEXT,
                                              RESTRICTIVE_DATA_RULES)

        self.assertEqual(job.TryToStartBandwidth(), True)
Beispiel #2
0
    def test_bandwidth_ok(self):

        PERMISSIVE_DATA_RULES = HydrusNetworking.BandwidthRules()

        PERMISSIVE_DATA_RULES.AddRule(HC.BANDWIDTH_TYPE_DATA, None, 1048576)

        DOMAIN_NETWORK_CONTEXT = ClientNetworkingContexts.NetworkContext(
            CC.NETWORK_CONTEXT_DOMAIN, MOCK_DOMAIN)

        #

        job = self._GetJob()

        job.engine.bandwidth_manager.ReportDataUsed([DOMAIN_NETWORK_CONTEXT],
                                                    50)

        self.assertEqual(job.TryToStartBandwidth(), True)

        job.engine.bandwidth_manager.SetRules(DOMAIN_NETWORK_CONTEXT,
                                              PERMISSIVE_DATA_RULES)

        self.assertEqual(job.TryToStartBandwidth(), True)

        #

        job = self._GetJob(for_login=True)

        job.engine.bandwidth_manager.ReportDataUsed([DOMAIN_NETWORK_CONTEXT],
                                                    50)

        self.assertEqual(job.TryToStartBandwidth(), True)

        job.engine.bandwidth_manager.SetRules(DOMAIN_NETWORK_CONTEXT,
                                              PERMISSIVE_DATA_RULES)

        self.assertEqual(job.TryToStartBandwidth(), True)
Beispiel #3
0
    def _test_restricted(self, service):

        # access_key

        registration_key = HydrusData.GenerateKey()

        HG.test_controller.SetRead('access_key', self._access_key)

        response = service.Request(HC.GET, 'access_key',
                                   {'registration_key': registration_key})

        self.assertEqual(response['access_key'], self._access_key)

        # set up session

        last_error = 0

        account = self._account

        HG.test_controller.SetRead('service', service)

        HG.test_controller.SetRead('account_key_from_access_key',
                                   HydrusData.GenerateKey())
        HG.test_controller.SetRead('account', self._account)

        # account

        response = service.Request(HC.GET, 'account')

        self.assertEqual(repr(response['account']), repr(self._account))

        # account from access key

        HG.test_controller.SetRead('account', self._account)

        subject_account_identifier = HydrusNetwork.AccountIdentifier(
            account_key=self._account.GetAccountKey())

        response = service.Request(
            HC.GET, 'other_account',
            {'subject_identifier': subject_account_identifier})

        self.assertEqual(repr(response['account']), repr(self._account))

        # account from file

        HG.test_controller.SetRead('account_from_content', self._account)

        content = HydrusNetwork.Content(
            content_type=HC.CONTENT_TYPE_FILES,
            content_data=(HydrusData.GenerateKey(), ))

        subject_account_identifier = HydrusNetwork.AccountIdentifier(
            content=content)

        response = service.Request(
            HC.GET, 'other_account',
            {'subject_identifier': subject_account_identifier})

        self.assertEqual(repr(response['account']), repr(self._account))

        # account from mapping

        HG.test_controller.SetRead('account_from_content', self._account)

        content = HydrusNetwork.Content(
            content_type=HC.CONTENT_TYPE_MAPPING,
            content_data=('hello', HydrusData.GenerateKey()))

        subject_account_identifier = HydrusNetwork.AccountIdentifier(
            content=content)

        response = service.Request(
            HC.GET, 'other_account',
            {'subject_identifier': subject_account_identifier})

        self.assertEqual(repr(response['account']), repr(self._account))

        # account_info

        account_info = {'message': 'hello'}

        HG.test_controller.SetRead('account_info', account_info)

        subject_account_identifier = HydrusNetwork.AccountIdentifier(
            account_key=HydrusData.GenerateKey())

        response = service.Request(
            HC.GET, 'account_info',
            {'subject_identifier': subject_account_identifier})

        self.assertEqual(response['account_info'], account_info)

        #

        content = HydrusNetwork.Content(
            content_type=HC.CONTENT_TYPE_FILES,
            content_data=(HydrusData.GenerateKey(), ))

        subject_account_identifier = HydrusNetwork.AccountIdentifier(
            content=content)

        with self.assertRaises(HydrusExceptions.BadRequestException):

            # can only do it with an account id
            response = service.Request(
                HC.GET, 'account_info',
                {'subject_identifier': subject_account_identifier})

        content = HydrusNetwork.Content(
            content_type=HC.CONTENT_TYPE_MAPPING,
            content_data=('hello', HydrusData.GenerateKey()))

        subject_account_identifier = HydrusNetwork.AccountIdentifier(
            content=content)

        with self.assertRaises(HydrusExceptions.BadRequestException):

            # can only do it with an account id
            response = service.Request(
                HC.GET, 'account_info',
                {'subject_identifier': subject_account_identifier})

        # account_types

        account_types = [
            HydrusNetwork.AccountType.GenerateAdminAccountType(
                service.GetServiceType())
        ]

        HG.test_controller.SetRead('account_types', account_types)

        HG.test_controller.ClearWrites('account_types')

        response = service.Request(HC.GET, 'account_types')

        self.assertEqual(response['account_types'][0].GetAccountTypeKey(),
                         account_types[0].GetAccountTypeKey())

        empty_account_type = HydrusNetwork.AccountType.GenerateNewAccountType(
            'empty account', {}, HydrusNetworking.BandwidthRules())

        account_types.append(empty_account_type)

        service.Request(
            HC.POST, 'account_types', {
                'account_types': account_types,
                'deletee_account_type_keys_to_new_account_type_keys': {}
            })

        written = HG.test_controller.GetWrite('account_types')

        [(args, kwargs)] = written

        (written_service_key, written_account, written_account_types,
         written_deletee_account_type_keys_to_new_account_type_keys) = args

        self.assertEqual(
            {wat.GetAccountTypeKey()
             for wat in written_account_types},
            {at.GetAccountTypeKey()
             for at in account_types})
        self.assertEqual(
            written_deletee_account_type_keys_to_new_account_type_keys, {})

        # registration_keys

        registration_key = HydrusData.GenerateKey()

        HG.test_controller.SetRead('registration_keys', [registration_key])

        response = service.Request(
            HC.GET, 'registration_keys', {
                'num': 1,
                'account_type_key': os.urandom(32),
                'expires': HydrusData.GetNow() + 1200
            })

        self.assertEqual(response['registration_keys'], [registration_key])
Beispiel #4
0
    def test_can_start(self):

        EMPTY_RULES = HydrusNetworking.BandwidthRules()

        PERMISSIVE_DATA_RULES = HydrusNetworking.BandwidthRules()

        PERMISSIVE_DATA_RULES.AddRule(HC.BANDWIDTH_TYPE_DATA, None, 1048576)

        PERMISSIVE_REQUEST_RULES = HydrusNetworking.BandwidthRules()

        PERMISSIVE_REQUEST_RULES.AddRule(HC.BANDWIDTH_TYPE_REQUESTS, None,
                                         10000)

        RESTRICTIVE_DATA_RULES = HydrusNetworking.BandwidthRules()

        RESTRICTIVE_DATA_RULES.AddRule(HC.BANDWIDTH_TYPE_DATA, None, 10)

        RESTRICTIVE_REQUEST_RULES = HydrusNetworking.BandwidthRules()

        RESTRICTIVE_REQUEST_RULES.AddRule(HC.BANDWIDTH_TYPE_REQUESTS, None, 1)

        DOMAIN_NETWORK_CONTEXT = ClientNetworkingContexts.NetworkContext(
            CC.NETWORK_CONTEXT_DOMAIN, MOCK_DOMAIN)
        SUBDOMAIN_NETWORK_CONTEXT = ClientNetworkingContexts.NetworkContext(
            CC.NETWORK_CONTEXT_DOMAIN, MOCK_SUBDOMAIN)

        GLOBAL_NETWORK_CONTEXTS = [
            ClientNetworkingContexts.GLOBAL_NETWORK_CONTEXT
        ]
        DOMAIN_NETWORK_CONTEXTS = [
            ClientNetworkingContexts.GLOBAL_NETWORK_CONTEXT,
            DOMAIN_NETWORK_CONTEXT
        ]
        SUBDOMAIN_NETWORK_CONTEXTS = [
            ClientNetworkingContexts.GLOBAL_NETWORK_CONTEXT,
            DOMAIN_NETWORK_CONTEXT, SUBDOMAIN_NETWORK_CONTEXT
        ]

        #

        fast_forward = HydrusData.GetNow() + 3600

        with patch.object(HydrusData, 'GetNow', return_value=fast_forward):

            bm = ClientNetworkingBandwidth.NetworkBandwidthManager()

            self.assertTrue(bm.CanStartRequest(GLOBAL_NETWORK_CONTEXTS))
            self.assertTrue(bm.CanStartRequest(DOMAIN_NETWORK_CONTEXTS))
            self.assertTrue(bm.CanStartRequest(SUBDOMAIN_NETWORK_CONTEXTS))

            #

            bm.ReportRequestUsed(DOMAIN_NETWORK_CONTEXTS)
            bm.ReportDataUsed(DOMAIN_NETWORK_CONTEXTS, 50)
            bm.ReportRequestUsed(SUBDOMAIN_NETWORK_CONTEXTS)
            bm.ReportDataUsed(SUBDOMAIN_NETWORK_CONTEXTS, 25)

            self.assertTrue(bm.CanStartRequest(GLOBAL_NETWORK_CONTEXTS))
            self.assertTrue(bm.CanStartRequest(DOMAIN_NETWORK_CONTEXTS))
            self.assertTrue(bm.CanStartRequest(SUBDOMAIN_NETWORK_CONTEXTS))

            #

            bm.SetRules(ClientNetworkingContexts.GLOBAL_NETWORK_CONTEXT,
                        EMPTY_RULES)
            bm.SetRules(DOMAIN_NETWORK_CONTEXT, EMPTY_RULES)
            bm.SetRules(SUBDOMAIN_NETWORK_CONTEXT, EMPTY_RULES)

            self.assertTrue(bm.CanStartRequest(GLOBAL_NETWORK_CONTEXTS))
            self.assertTrue(bm.CanStartRequest(DOMAIN_NETWORK_CONTEXTS))
            self.assertTrue(bm.CanStartRequest(SUBDOMAIN_NETWORK_CONTEXTS))

            bm.SetRules(ClientNetworkingContexts.GLOBAL_NETWORK_CONTEXT,
                        PERMISSIVE_DATA_RULES)
            bm.SetRules(DOMAIN_NETWORK_CONTEXT, PERMISSIVE_DATA_RULES)
            bm.SetRules(SUBDOMAIN_NETWORK_CONTEXT, PERMISSIVE_DATA_RULES)

            self.assertTrue(bm.CanStartRequest(GLOBAL_NETWORK_CONTEXTS))
            self.assertTrue(bm.CanStartRequest(DOMAIN_NETWORK_CONTEXTS))
            self.assertTrue(bm.CanStartRequest(SUBDOMAIN_NETWORK_CONTEXTS))

            bm.SetRules(ClientNetworkingContexts.GLOBAL_NETWORK_CONTEXT,
                        PERMISSIVE_REQUEST_RULES)
            bm.SetRules(DOMAIN_NETWORK_CONTEXT, PERMISSIVE_REQUEST_RULES)
            bm.SetRules(SUBDOMAIN_NETWORK_CONTEXT, PERMISSIVE_REQUEST_RULES)

            self.assertTrue(bm.CanStartRequest(GLOBAL_NETWORK_CONTEXTS))
            self.assertTrue(bm.CanStartRequest(DOMAIN_NETWORK_CONTEXTS))
            self.assertTrue(bm.CanStartRequest(SUBDOMAIN_NETWORK_CONTEXTS))

            #

            bm.SetRules(SUBDOMAIN_NETWORK_CONTEXT, RESTRICTIVE_DATA_RULES)

            self.assertTrue(bm.CanStartRequest(GLOBAL_NETWORK_CONTEXTS))
            self.assertTrue(bm.CanStartRequest(DOMAIN_NETWORK_CONTEXTS))
            self.assertFalse(bm.CanStartRequest(SUBDOMAIN_NETWORK_CONTEXTS))

            bm.SetRules(SUBDOMAIN_NETWORK_CONTEXT, RESTRICTIVE_REQUEST_RULES)

            self.assertTrue(bm.CanStartRequest(GLOBAL_NETWORK_CONTEXTS))
            self.assertTrue(bm.CanStartRequest(DOMAIN_NETWORK_CONTEXTS))
            self.assertFalse(bm.CanStartRequest(SUBDOMAIN_NETWORK_CONTEXTS))

            bm.SetRules(SUBDOMAIN_NETWORK_CONTEXT, PERMISSIVE_REQUEST_RULES)

            self.assertTrue(bm.CanStartRequest(GLOBAL_NETWORK_CONTEXTS))
            self.assertTrue(bm.CanStartRequest(DOMAIN_NETWORK_CONTEXTS))
            self.assertTrue(bm.CanStartRequest(SUBDOMAIN_NETWORK_CONTEXTS))

            #

            bm.SetRules(DOMAIN_NETWORK_CONTEXT, RESTRICTIVE_DATA_RULES)

            self.assertTrue(bm.CanStartRequest(GLOBAL_NETWORK_CONTEXTS))
            self.assertFalse(bm.CanStartRequest(DOMAIN_NETWORK_CONTEXTS))
            self.assertFalse(bm.CanStartRequest(SUBDOMAIN_NETWORK_CONTEXTS))

            bm.SetRules(DOMAIN_NETWORK_CONTEXT, RESTRICTIVE_REQUEST_RULES)

            self.assertTrue(bm.CanStartRequest(GLOBAL_NETWORK_CONTEXTS))
            self.assertFalse(bm.CanStartRequest(DOMAIN_NETWORK_CONTEXTS))
            self.assertFalse(bm.CanStartRequest(SUBDOMAIN_NETWORK_CONTEXTS))

            bm.SetRules(DOMAIN_NETWORK_CONTEXT, PERMISSIVE_REQUEST_RULES)

            self.assertTrue(bm.CanStartRequest(GLOBAL_NETWORK_CONTEXTS))
            self.assertTrue(bm.CanStartRequest(DOMAIN_NETWORK_CONTEXTS))
            self.assertTrue(bm.CanStartRequest(SUBDOMAIN_NETWORK_CONTEXTS))

            #

            bm.SetRules(ClientNetworkingContexts.GLOBAL_NETWORK_CONTEXT,
                        RESTRICTIVE_DATA_RULES)

            self.assertFalse(bm.CanStartRequest(GLOBAL_NETWORK_CONTEXTS))
            self.assertFalse(bm.CanStartRequest(DOMAIN_NETWORK_CONTEXTS))
            self.assertFalse(bm.CanStartRequest(SUBDOMAIN_NETWORK_CONTEXTS))

            bm.SetRules(ClientNetworkingContexts.GLOBAL_NETWORK_CONTEXT,
                        RESTRICTIVE_REQUEST_RULES)

            self.assertFalse(bm.CanStartRequest(GLOBAL_NETWORK_CONTEXTS))
            self.assertFalse(bm.CanStartRequest(DOMAIN_NETWORK_CONTEXTS))
            self.assertFalse(bm.CanStartRequest(SUBDOMAIN_NETWORK_CONTEXTS))

            bm.SetRules(ClientNetworkingContexts.GLOBAL_NETWORK_CONTEXT,
                        PERMISSIVE_REQUEST_RULES)

            self.assertTrue(bm.CanStartRequest(GLOBAL_NETWORK_CONTEXTS))
            self.assertTrue(bm.CanStartRequest(DOMAIN_NETWORK_CONTEXTS))
            self.assertTrue(bm.CanStartRequest(SUBDOMAIN_NETWORK_CONTEXTS))

            #

            bm.SetRules(ClientNetworkingContexts.GLOBAL_NETWORK_CONTEXT,
                        RESTRICTIVE_DATA_RULES)
            bm.SetRules(DOMAIN_NETWORK_CONTEXT, RESTRICTIVE_REQUEST_RULES)
            bm.SetRules(DOMAIN_NETWORK_CONTEXT, EMPTY_RULES)

            self.assertFalse(bm.CanStartRequest(GLOBAL_NETWORK_CONTEXTS))
            self.assertFalse(bm.CanStartRequest(DOMAIN_NETWORK_CONTEXTS))
            self.assertFalse(bm.CanStartRequest(SUBDOMAIN_NETWORK_CONTEXTS))
Beispiel #5
0
    def _test_account_creation(self):

        result = self._read('account_types', self._tag_service_key,
                            self._tag_service_account)

        (self._tag_service_admin_account_type, ) = result

        self.assertEqual(self._tag_service_admin_account_type.GetTitle(),
                         'administrator')

        #

        self._regular_user_account_type = HydrusNetwork.AccountType.GenerateNewAccountType(
            'regular user',
            {HC.CONTENT_TYPE_MAPPINGS: HC.PERMISSION_ACTION_CREATE},
            HydrusNetworking.BandwidthRules())
        self._deletee_user_account_type = HydrusNetwork.AccountType.GenerateNewAccountType(
            'deletee user', {}, HydrusNetworking.BandwidthRules())

        new_account_types = [
            self._tag_service_admin_account_type,
            self._regular_user_account_type, self._deletee_user_account_type
        ]

        #

        self._write('account_types', self._tag_service_key,
                    self._tag_service_account, new_account_types, {})

        edited_account_types = self._read('account_types',
                                          self._tag_service_key,
                                          self._tag_service_account)

        self.assertEqual(
            {at.GetAccountTypeKey()
             for at in edited_account_types}, {
                 at.GetAccountTypeKey()
                 for at in (self._tag_service_admin_account_type,
                            self._regular_user_account_type,
                            self._deletee_user_account_type)
             })

        #

        r_keys = self._read(
            'registration_keys', self._tag_service_key,
            self._tag_service_account, 5,
            self._deletee_user_account_type.GetAccountTypeKey(), 86400 * 365)

        access_keys = [
            self._read('access_key', self._tag_service_key, r_key)
            for r_key in r_keys
        ]

        account_keys = [
            self._read('account_key_from_access_key', self._tag_service_key,
                       access_key) for access_key in access_keys
        ]

        accounts = [
            self._read('account', self._tag_service_key, account_key)
            for account_key in account_keys
        ]

        for account in accounts:

            self.assertEqual(
                account.GetAccountType().GetAccountTypeKey(),
                self._deletee_user_account_type.GetAccountTypeKey())

        #

        deletee_account_type_keys_to_replacement_account_type_keys = {
            self._deletee_user_account_type.GetAccountTypeKey():
            self._regular_user_account_type.GetAccountTypeKey()
        }

        new_account_types = [
            self._tag_service_admin_account_type,
            self._regular_user_account_type
        ]

        self._write(
            'account_types', self._tag_service_key, self._tag_service_account,
            new_account_types,
            deletee_account_type_keys_to_replacement_account_type_keys)

        accounts = [
            self._read('account', self._tag_service_key, account_key)
            for account_key in account_keys
        ]

        self._tag_service_regular_account = accounts[0]

        for account in accounts:

            self.assertEqual(
                account.GetAccountType().GetAccountTypeKey(),
                self._regular_user_account_type.GetAccountTypeKey())

        #

        r_keys = self._read(
            'registration_keys', self._tag_service_key,
            self._tag_service_account, 5,
            self._regular_user_account_type.GetAccountTypeKey(), 86400 * 365)

        self.assertEqual(len(r_keys), 5)

        for r_key in r_keys:
            self.assertEqual(len(r_key), 32)

        r_key = r_keys[0]

        access_key = self._read('access_key', self._tag_service_key, r_key)
        access_key_2 = self._read('access_key', self._tag_service_key, r_key)

        self.assertNotEqual(access_key, access_key_2)

        with self.assertRaises(
                HydrusExceptions.InsufficientCredentialsException):

            # this access key has been replaced
            self._read('account_key_from_access_key', self._tag_service_key,
                       access_key)

        account_key = self._read('account_key_from_access_key',
                                 self._tag_service_key, access_key_2)

        with self.assertRaises(
                HydrusExceptions.InsufficientCredentialsException):

            # this registration key has been deleted
            self._read('access_key', self._tag_service_key, r_key)
 def test_per_sec( self ):
     
     # at short time deltas, we can always start based on data alone
     
     rules = HydrusNetworking.BandwidthRules()
     
     rules.AddRule( HC.BANDWIDTH_TYPE_DATA, 1, 10240 )
     
     with patch.object( HydrusData, 'GetNow', return_value = now ):
         
         self.assertTrue( rules.CanStartRequest( ZERO_USAGE ) )
         self.assertTrue( rules.CanStartRequest( LOW_USAGE ) )
         self.assertTrue( rules.CanStartRequest( HIGH_USAGE ) )
         
         self.assertTrue( rules.CanContinueDownload( ZERO_USAGE ) )
         self.assertTrue( rules.CanContinueDownload( LOW_USAGE ) )
         self.assertFalse( rules.CanContinueDownload( HIGH_USAGE ) )
         
     
     with patch.object( HydrusData, 'GetNow', return_value = now_10 ):
         
         self.assertTrue( rules.CanStartRequest( ZERO_USAGE ) )
         self.assertTrue( rules.CanStartRequest( LOW_USAGE ) )
         self.assertTrue( rules.CanStartRequest( HIGH_USAGE ) )
         
         self.assertTrue( rules.CanContinueDownload( ZERO_USAGE ) )
         self.assertTrue( rules.CanContinueDownload( LOW_USAGE ) )
         self.assertTrue( rules.CanContinueDownload( HIGH_USAGE ) )
         
     
     with patch.object( HydrusData, 'GetNow', return_value = now_20 ):
         
         self.assertTrue( rules.CanStartRequest( ZERO_USAGE ) )
         self.assertTrue( rules.CanStartRequest( LOW_USAGE ) )
         self.assertTrue( rules.CanStartRequest( HIGH_USAGE ) )
         
         self.assertTrue( rules.CanContinueDownload( ZERO_USAGE ) )
         self.assertTrue( rules.CanContinueDownload( LOW_USAGE ) )
         self.assertTrue( rules.CanContinueDownload( HIGH_USAGE ) )
         
     
     #
     
     rules = HydrusNetworking.BandwidthRules()
     
     rules.AddRule( HC.BANDWIDTH_TYPE_REQUESTS, 1, 1 )
     
     with patch.object( HydrusData, 'GetNow', return_value = now ):
         
         self.assertTrue( rules.CanStartRequest( ZERO_USAGE ) )
         self.assertFalse( rules.CanStartRequest( LOW_USAGE ) )
         self.assertFalse( rules.CanStartRequest( HIGH_USAGE ) )
         
         self.assertTrue( rules.CanContinueDownload( ZERO_USAGE ) )
         self.assertTrue( rules.CanContinueDownload( LOW_USAGE ) )
         self.assertTrue( rules.CanContinueDownload( HIGH_USAGE ) )
         
     
     with patch.object( HydrusData, 'GetNow', return_value = now_10 ):
         
         self.assertTrue( rules.CanStartRequest( ZERO_USAGE ) )
         self.assertTrue( rules.CanStartRequest( LOW_USAGE ) )
         self.assertTrue( rules.CanStartRequest( HIGH_USAGE ) )
         
         self.assertTrue( rules.CanContinueDownload( ZERO_USAGE ) )
         self.assertTrue( rules.CanContinueDownload( LOW_USAGE ) )
         self.assertTrue( rules.CanContinueDownload( HIGH_USAGE ) )
         
     
     with patch.object( HydrusData, 'GetNow', return_value = now_20 ):
         
         self.assertTrue( rules.CanStartRequest( ZERO_USAGE ) )
         self.assertTrue( rules.CanStartRequest( LOW_USAGE ) )
         self.assertTrue( rules.CanStartRequest( HIGH_USAGE ) )
         
         self.assertTrue( rules.CanContinueDownload( ZERO_USAGE ) )
         self.assertTrue( rules.CanContinueDownload( LOW_USAGE ) )
         self.assertTrue( rules.CanContinueDownload( HIGH_USAGE ) )
         
     
     #
     
     rules = HydrusNetworking.BandwidthRules()
     
     rules.AddRule( HC.BANDWIDTH_TYPE_DATA, 1, 10240 )
     rules.AddRule( HC.BANDWIDTH_TYPE_REQUESTS, 1, 1 )
     
     with patch.object( HydrusData, 'GetNow', return_value = now ):
         
         self.assertTrue( rules.CanStartRequest( ZERO_USAGE ) )
         self.assertFalse( rules.CanStartRequest( LOW_USAGE ) )
         self.assertFalse( rules.CanStartRequest( HIGH_USAGE ) )
         
         self.assertTrue( rules.CanContinueDownload( ZERO_USAGE ) )
         self.assertTrue( rules.CanContinueDownload( LOW_USAGE ) )
         self.assertFalse( rules.CanContinueDownload( HIGH_USAGE ) )
         
     
     with patch.object( HydrusData, 'GetNow', return_value = now_10 ):
         
         self.assertTrue( rules.CanStartRequest( ZERO_USAGE ) )
         self.assertTrue( rules.CanStartRequest( LOW_USAGE ) )
         self.assertTrue( rules.CanStartRequest( HIGH_USAGE ) )
         
         self.assertTrue( rules.CanContinueDownload( ZERO_USAGE ) )
         self.assertTrue( rules.CanContinueDownload( LOW_USAGE ) )
         self.assertTrue( rules.CanContinueDownload( HIGH_USAGE ) )
         
     
     with patch.object( HydrusData, 'GetNow', return_value = now_20 ):
         
         self.assertTrue( rules.CanStartRequest( ZERO_USAGE ) )
         self.assertTrue( rules.CanStartRequest( LOW_USAGE ) )
         self.assertTrue( rules.CanStartRequest( HIGH_USAGE ) )
         
         self.assertTrue( rules.CanContinueDownload( ZERO_USAGE ) )
         self.assertTrue( rules.CanContinueDownload( LOW_USAGE ) )
         self.assertTrue( rules.CanContinueDownload( HIGH_USAGE ) )
 def test_per_month( self ):
     
     rules = HydrusNetworking.BandwidthRules()
     
     rules.AddRule( HC.BANDWIDTH_TYPE_DATA, None, 10240 )
     
     with patch.object( HydrusData, 'GetNow', return_value = now ):
         
         self.assertTrue( rules.CanStartRequest( ZERO_USAGE ) )
         self.assertTrue( rules.CanStartRequest( LOW_USAGE ) )
         self.assertFalse( rules.CanStartRequest( HIGH_USAGE ) )
         
         self.assertTrue( rules.CanContinueDownload( ZERO_USAGE ) )
         self.assertTrue( rules.CanContinueDownload( LOW_USAGE ) )
         self.assertTrue( rules.CanContinueDownload( HIGH_USAGE ) )
         
     
     with patch.object( HydrusData, 'GetNow', return_value = now_10 ):
         
         self.assertTrue( rules.CanStartRequest( ZERO_USAGE ) )
         self.assertTrue( rules.CanStartRequest( LOW_USAGE ) )
         self.assertFalse( rules.CanStartRequest( HIGH_USAGE ) )
         
         self.assertTrue( rules.CanContinueDownload( ZERO_USAGE ) )
         self.assertTrue( rules.CanContinueDownload( LOW_USAGE ) )
         self.assertTrue( rules.CanContinueDownload( HIGH_USAGE ) )
         
     
     with patch.object( HydrusData, 'GetNow', return_value = now_20 ):
         
         self.assertTrue( rules.CanStartRequest( ZERO_USAGE ) )
         self.assertTrue( rules.CanStartRequest( LOW_USAGE ) )
         self.assertFalse( rules.CanStartRequest( HIGH_USAGE ) )
         
         self.assertTrue( rules.CanContinueDownload( ZERO_USAGE ) )
         self.assertTrue( rules.CanContinueDownload( LOW_USAGE ) )
         self.assertTrue( rules.CanContinueDownload( HIGH_USAGE ) )
         
     
     #
     
     rules = HydrusNetworking.BandwidthRules()
     
     rules.AddRule( HC.BANDWIDTH_TYPE_REQUESTS, None, 10 )
     
     with patch.object( HydrusData, 'GetNow', return_value = now ):
         
         self.assertTrue( rules.CanStartRequest( ZERO_USAGE ) )
         self.assertTrue( rules.CanStartRequest( LOW_USAGE ) )
         self.assertFalse( rules.CanStartRequest( HIGH_USAGE ) )
         
         self.assertTrue( rules.CanContinueDownload( ZERO_USAGE ) )
         self.assertTrue( rules.CanContinueDownload( LOW_USAGE ) )
         self.assertTrue( rules.CanContinueDownload( HIGH_USAGE ) )
         
     
     with patch.object( HydrusData, 'GetNow', return_value = now_10 ):
         
         self.assertTrue( rules.CanStartRequest( ZERO_USAGE ) )
         self.assertTrue( rules.CanStartRequest( LOW_USAGE ) )
         self.assertFalse( rules.CanStartRequest( HIGH_USAGE ) )
         
         self.assertTrue( rules.CanContinueDownload( ZERO_USAGE ) )
         self.assertTrue( rules.CanContinueDownload( LOW_USAGE ) )
         self.assertTrue( rules.CanContinueDownload( HIGH_USAGE ) )
         
     
     with patch.object( HydrusData, 'GetNow', return_value = now_20 ):
         
         self.assertTrue( rules.CanStartRequest( ZERO_USAGE ) )
         self.assertTrue( rules.CanStartRequest( LOW_USAGE ) )
         self.assertFalse( rules.CanStartRequest( HIGH_USAGE ) )
         
         self.assertTrue( rules.CanContinueDownload( ZERO_USAGE ) )
         self.assertTrue( rules.CanContinueDownload( LOW_USAGE ) )
         self.assertTrue( rules.CanContinueDownload( HIGH_USAGE ) )
         
     
     #
     
     rules = HydrusNetworking.BandwidthRules()
     
     rules.AddRule( HC.BANDWIDTH_TYPE_DATA, None, 10240 )
     rules.AddRule( HC.BANDWIDTH_TYPE_REQUESTS, None, 10 )
     
     with patch.object( HydrusData, 'GetNow', return_value = now ):
         
         self.assertTrue( rules.CanStartRequest( ZERO_USAGE ) )
         self.assertTrue( rules.CanStartRequest( LOW_USAGE ) )
         self.assertFalse( rules.CanStartRequest( HIGH_USAGE ) )
         
         self.assertTrue( rules.CanContinueDownload( ZERO_USAGE ) )
         self.assertTrue( rules.CanContinueDownload( LOW_USAGE ) )
         self.assertTrue( rules.CanContinueDownload( HIGH_USAGE ) )
         
     
     with patch.object( HydrusData, 'GetNow', return_value = now_10 ):
         
         self.assertTrue( rules.CanStartRequest( ZERO_USAGE ) )
         self.assertTrue( rules.CanStartRequest( LOW_USAGE ) )
         self.assertFalse( rules.CanStartRequest( HIGH_USAGE ) )
         
         self.assertTrue( rules.CanContinueDownload( ZERO_USAGE ) )
         self.assertTrue( rules.CanContinueDownload( LOW_USAGE ) )
         self.assertTrue( rules.CanContinueDownload( HIGH_USAGE ) )
         
     
     with patch.object( HydrusData, 'GetNow', return_value = now_20 ):
         
         self.assertTrue( rules.CanStartRequest( ZERO_USAGE ) )
         self.assertTrue( rules.CanStartRequest( LOW_USAGE ) )
         self.assertFalse( rules.CanStartRequest( HIGH_USAGE ) )
         
         self.assertTrue( rules.CanContinueDownload( ZERO_USAGE ) )
         self.assertTrue( rules.CanContinueDownload( LOW_USAGE ) )
         self.assertTrue( rules.CanContinueDownload( HIGH_USAGE ) )
 def __init__( self ):
     
     HydrusSerialisable.SerialisableBase.__init__( self )
     
     self.engine = None
     
     self._dirty = False
     
     self._lock = threading.Lock()
     
     self._last_pages_gallery_query_timestamps = collections.defaultdict( lambda: 0 )
     self._last_subscriptions_gallery_query_timestamps = collections.defaultdict( lambda: 0 )
     self._last_watchers_query_timestamps = collections.defaultdict( lambda: 0 )
     
     self._tracker_container_names_to_tracker_containers = {}
     self._network_contexts_to_tracker_containers = {}
     
     self._tracker_container_names = set()
     self._dirty_tracker_container_names = set()
     self._deletee_tracker_container_names = set()
     
     self._my_bandwidth_tracker = HydrusNetworking.BandwidthTracker()
     
     self._network_contexts_to_bandwidth_rules = collections.defaultdict( HydrusNetworking.BandwidthRules )
     
     for context_type in [ CC.NETWORK_CONTEXT_GLOBAL, CC.NETWORK_CONTEXT_HYDRUS, CC.NETWORK_CONTEXT_DOMAIN, CC.NETWORK_CONTEXT_DOWNLOADER_PAGE, CC.NETWORK_CONTEXT_SUBSCRIPTION, CC.NETWORK_CONTEXT_WATCHER_PAGE ]:
         
         self._network_contexts_to_bandwidth_rules[ ClientNetworkingContexts.NetworkContext( context_type ) ] = HydrusNetworking.BandwidthRules()