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._network_contexts_to_bandwidth_trackers = collections.defaultdict(
            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()
    def InitView(self):

        HydrusController.HydrusController.InitView(self)

        port = self._admin_service.GetPort()

        if HydrusNetworking.LocalPortInUse(port):

            HydrusData.Print(
                'Something is already bound to port ' + str(port) +
                ', so your administration service cannot be started. Please quit the server and retry once the port is clear.'
            )

        else:

            self.SetRunningTwistedServices(self._services)

        #

        job = self.CallRepeating(5.0, 600.0, self.SyncRepositories)

        self._daemon_jobs['sync_repositories'] = job

        job = self.CallRepeating(0.0, 30.0, self.SaveDirtyObjects)

        self._daemon_jobs['save_dirty_objects'] = job

        job = self.CallRepeating(0.0, 86400.0, self.DeleteOrphans)

        self._daemon_jobs['delete_orphans'] = job
Ejemplo n.º 3
0
 def _callbackParsePOSTArgs( self, request ):
     
     request.content.seek( 0 )
     
     if not request.requestHeaders.hasHeader( 'Content-Type' ):
         
         parsed_request_args = HydrusNetworking.ParsedRequestArguments()
         
     else:
         
         content_types = request.requestHeaders.getRawHeaders( 'Content-Type' )
         
         content_type = content_types[0]
         
         try:
             
             mime = HC.mime_enum_lookup[ content_type ]
             
         except:
             
             raise HydrusExceptions.BadRequestException( 'Did not recognise Content-Type header!' )
             
         
         total_bytes_read = 0
         
         if mime == HC.APPLICATION_JSON:
             
             json_string = request.content.read()
             
             total_bytes_read += len( json_string )
             
             parsed_request_args = HydrusNetwork.ParseNetworkBytesToParsedHydrusArgs( json_string )
             
         else:
             
             ( os_file_handle, temp_path ) = HydrusPaths.GetTempPath()
             
             request.temp_file_info = ( os_file_handle, temp_path )
             
             with open( temp_path, 'wb' ) as f:
                 
                 for block in HydrusPaths.ReadFileLikeAsBlocks( request.content ): 
                     
                     f.write( block )
                     
                     total_bytes_read += len( block )
                     
                 
             
             decompression_bombs_ok = self._DecompressionBombsOK( request )
             
             parsed_request_args = HydrusServerResources.ParseFileArguments( temp_path, decompression_bombs_ok )
             
         
         self._reportDataUsed( request, total_bytes_read )
         
     
     request.parsed_request_args = parsed_request_args
     
     return request
    def SetServices(self, services):

        # doesn't need the dirty_object_lock because the caller takes it

        # first test available ports

        my_ports = {s.GetPort() for s in self._services}

        for service in services:

            port = service.GetPort()

            if port not in my_ports and HydrusNetworking.LocalPortInUse(port):

                raise HydrusExceptions.ServerException(
                    'Something was already bound to port ' + str(port))

        #

        self._services = services

        self.CallToThread(self.services_upnp_manager.SetServices,
                          self._services)

        [self._admin_service] = [
            service for service in self._services
            if service.GetServiceType() == HC.SERVER_ADMIN
        ]

        self.SetRunningTwistedServices(self._services)
Ejemplo n.º 5
0
    def GetTracker(self, network_context):

        with self._lock:

            if network_context in self._network_contexts_to_tracker_containers:

                return self._GetTracker(network_context)

            else:

                return HydrusNetworking.BandwidthTracker()
Ejemplo n.º 6
0
    def GetValue(self):

        bandwidth_rules = HydrusNetworking.BandwidthRules()

        for rule in self._listctrl.GetData():

            (bandwidth_type, time_delta, max_allowed) = rule

            bandwidth_rules.AddRule(bandwidth_type, time_delta, max_allowed)

        return bandwidth_rules
Ejemplo n.º 7
0
 def test_no_rules( self ):
     
     rules = HydrusNetworking.BandwidthRules()
     
     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.assertTrue( rules.CanContinueDownload( HIGH_USAGE ) )
Ejemplo n.º 8
0
    def __init__(self, name, network_context=None, bandwidth_tracker=None):

        if network_context is None:

            network_context = ClientNetworkingContexts.GLOBAL_NETWORK_CONTEXT

        if bandwidth_tracker is None:

            bandwidth_tracker = HydrusNetworking.BandwidthTracker()

        HydrusSerialisable.SerialisableBaseNamed.__init__(self, name)

        self.network_context = network_context
        self.bandwidth_tracker = bandwidth_tracker
    def DeleteHistory(self, network_contexts):

        with self._lock:

            for network_context in network_contexts:

                if network_context in self._network_contexts_to_bandwidth_trackers:

                    del self._network_contexts_to_bandwidth_trackers[
                        network_context]

                    if network_context == ClientNetworkingContexts.GLOBAL_NETWORK_CONTEXT:

                        # just to reset it, so we have a 0 global context at all times
                        self._network_contexts_to_bandwidth_trackers[
                            ClientNetworkingContexts.
                            GLOBAL_NETWORK_CONTEXT] = HydrusNetworking.BandwidthTracker(
                            )

            self._SetDirty()
Ejemplo n.º 10
0
    def _GetTracker(self,
                    network_context: ClientNetworkingContexts.NetworkContext,
                    making_it_dirty=False):

        if network_context not in self._network_contexts_to_tracker_containers:

            bandwidth_tracker = HydrusNetworking.BandwidthTracker()

            tracker_container_name = HydrusData.GenerateKey().hex()

            tracker_container = NetworkBandwidthManagerTrackerContainer(
                tracker_container_name,
                network_context=network_context,
                bandwidth_tracker=bandwidth_tracker)

            self._tracker_container_names_to_tracker_containers[
                tracker_container_name] = tracker_container
            self._network_contexts_to_tracker_containers[
                network_context] = tracker_container

            # note this discards ephemeral network contexts, which have temporary identifiers that are generally invisible to the user

            if not network_context.IsEphemeral():

                self._tracker_container_names.add(tracker_container_name)
                self._dirty_tracker_container_names.add(tracker_container_name)

            self._SetDirty()

        tracker_container = self._network_contexts_to_tracker_containers[
            network_context]

        if making_it_dirty and not network_context.IsEphemeral():

            self._dirty_tracker_container_names.add(
                tracker_container.GetName())

        return tracker_container.bandwidth_tracker
Ejemplo n.º 11
0
    def test_bandwidth_ok(self):

        PERMISSIVE_DATA_RULES = HydrusNetworking.BandwidthRules()

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

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

        #

        job = self._GetJob()

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

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

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

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

        #

        job = self._GetJob(for_login=True)

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

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

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

        self.assertEqual(job.BandwidthOK(), True)
Ejemplo n.º 12
0
    def test_bandwidth_exceeded(self):

        RESTRICTIVE_DATA_RULES = HydrusNetworking.BandwidthRules()

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

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

        #

        job = self._GetJob()

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

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

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

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

        #

        job = self._GetJob(for_login=True)

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

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

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

        self.assertEqual(job.BandwidthOK(), True)
    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)
Ejemplo n.º 14
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))
Ejemplo n.º 15
0
from mock import patch

from hydrus.core import HydrusConstants as HC
from hydrus.core import HydrusData
from hydrus.core import HydrusNetworking

now = HydrusData.GetNow()

now_10 = now + 10

now_20 = now + 20

with patch.object( HydrusData, 'GetNow', return_value = now ):
    
    HIGH_USAGE = HydrusNetworking.BandwidthTracker()
    
    for i in range( 100 ):
        
        HIGH_USAGE.ReportRequestUsed()
        HIGH_USAGE.ReportDataUsed( random.randint( 512, 1024 ) )
        
    
    LOW_USAGE = HydrusNetworking.BandwidthTracker()
    
    LOW_USAGE.ReportRequestUsed()
    LOW_USAGE.ReportDataUsed( 1024 )
    
    ZERO_USAGE = HydrusNetworking.BandwidthTracker()
    
class TestBandwidthRules( unittest.TestCase ):
Ejemplo n.º 16
0
            def StartServices(*args, **kwargs):

                HydrusData.Print('Starting services\u2026')

                for service in services:

                    service_key = service.GetServiceKey()
                    service_type = service.GetServiceType()

                    try:

                        port = service.GetPort()

                        if service_type == HC.SERVER_ADMIN:

                            http_factory = ServerServer.HydrusServiceAdmin(
                                service)

                        elif service_type == HC.FILE_REPOSITORY:

                            http_factory = ServerServer.HydrusServiceRepositoryFile(
                                service)

                        elif service_type == HC.TAG_REPOSITORY:

                            http_factory = ServerServer.HydrusServiceRepositoryTag(
                                service)

                        else:

                            return

                        (ssl_cert_path, ssl_key_path) = self.db.GetSSLPaths()

                        sslmethod = twisted.internet.ssl.SSL.TLSv1_2_METHOD

                        context_factory = twisted.internet.ssl.DefaultOpenSSLContextFactory(
                            ssl_key_path, ssl_cert_path, sslmethod)

                        ipv6_port = None

                        try:

                            ipv6_port = reactor.listenSSL(port,
                                                          http_factory,
                                                          context_factory,
                                                          interface='::')

                        except Exception as e:

                            HydrusData.Print('Could not bind to IPv6:')

                            HydrusData.Print(str(e))

                        ipv4_port = None

                        try:

                            ipv4_port = reactor.listenSSL(
                                port, http_factory, context_factory)

                        except:

                            if ipv6_port is None:

                                raise

                        self._service_keys_to_connected_ports[service_key] = (
                            ipv4_port, ipv6_port)

                        if not HydrusNetworking.LocalPortInUse(port):

                            raise Exception(
                                'Tried to bind port {} for "{}" but it failed.'
                                .format(port, service.GetName()))

                    except Exception as e:

                        HydrusData.Print(traceback.format_exc())

                HydrusData.Print('Services started')
Ejemplo n.º 17
0
 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 ) )
Ejemplo n.º 18
0
 def test_bandwidth_tracker( self ):
     
     bandwidth_tracker = HydrusNetworking.BandwidthTracker()
     
     self.assertEqual( bandwidth_tracker.GetCurrentMonthSummary(), 'used 0B in 0 requests this month' )
     
     now = HydrusData.GetNow()
     
     with patch.object( HydrusData, 'GetNow', return_value = now ):
         
         self.assertEqual( bandwidth_tracker.GetUsage( HC.BANDWIDTH_TYPE_DATA, 0 ), 0 )
         self.assertEqual( bandwidth_tracker.GetUsage( HC.BANDWIDTH_TYPE_REQUESTS, 0 ), 0 )
         
         self.assertEqual( bandwidth_tracker.GetUsage( HC.BANDWIDTH_TYPE_DATA, 1 ), 0 )
         self.assertEqual( bandwidth_tracker.GetUsage( HC.BANDWIDTH_TYPE_REQUESTS, 1 ), 0 )
         
         self.assertEqual( bandwidth_tracker.GetUsage( HC.BANDWIDTH_TYPE_DATA, 2 ), 0 )
         self.assertEqual( bandwidth_tracker.GetUsage( HC.BANDWIDTH_TYPE_REQUESTS, 2 ), 0 )
         
         self.assertEqual( bandwidth_tracker.GetUsage( HC.BANDWIDTH_TYPE_DATA, 6 ), 0 )
         self.assertEqual( bandwidth_tracker.GetUsage( HC.BANDWIDTH_TYPE_REQUESTS, 6 ), 0 )
         
         self.assertEqual( bandwidth_tracker.GetUsage( HC.BANDWIDTH_TYPE_DATA, 3600 ), 0 )
         self.assertEqual( bandwidth_tracker.GetUsage( HC.BANDWIDTH_TYPE_REQUESTS, 3600 ), 0 )
         
         self.assertEqual( bandwidth_tracker.GetUsage( HC.BANDWIDTH_TYPE_DATA, None ), 0 )
         self.assertEqual( bandwidth_tracker.GetUsage( HC.BANDWIDTH_TYPE_REQUESTS, None ), 0 )
         
         #
         
         bandwidth_tracker.ReportDataUsed( 1024 )
         bandwidth_tracker.ReportRequestUsed()
         
         self.assertEqual( bandwidth_tracker.GetCurrentMonthSummary(), 'used 1.0KB in 1 requests this month' )
         
         self.assertEqual( bandwidth_tracker.GetUsage( HC.BANDWIDTH_TYPE_DATA, 0 ), 0 )
         self.assertEqual( bandwidth_tracker.GetUsage( HC.BANDWIDTH_TYPE_REQUESTS, 0 ), 0 )
         
         self.assertEqual( bandwidth_tracker.GetUsage( HC.BANDWIDTH_TYPE_DATA, 1 ), 1024 )
         self.assertEqual( bandwidth_tracker.GetUsage( HC.BANDWIDTH_TYPE_REQUESTS, 1 ), 1 )
         
         self.assertEqual( bandwidth_tracker.GetUsage( HC.BANDWIDTH_TYPE_DATA, 2 ), 1024 )
         self.assertEqual( bandwidth_tracker.GetUsage( HC.BANDWIDTH_TYPE_REQUESTS, 2 ), 1 )
         
         self.assertEqual( bandwidth_tracker.GetUsage( HC.BANDWIDTH_TYPE_DATA, 6 ), 1024 )
         self.assertEqual( bandwidth_tracker.GetUsage( HC.BANDWIDTH_TYPE_REQUESTS, 6 ), 1 )
         
         self.assertEqual( bandwidth_tracker.GetUsage( HC.BANDWIDTH_TYPE_DATA, 3600 ), 1024 )
         self.assertEqual( bandwidth_tracker.GetUsage( HC.BANDWIDTH_TYPE_REQUESTS, 3600 ), 1 )
         
         self.assertEqual( bandwidth_tracker.GetUsage( HC.BANDWIDTH_TYPE_DATA, None ), 1024 )
         self.assertEqual( bandwidth_tracker.GetUsage( HC.BANDWIDTH_TYPE_REQUESTS, None ), 1 )
         
     
     #
     
     five_secs_from_now = now + 5
     
     with patch.object( HydrusData, 'GetNow', return_value = five_secs_from_now ):
         
         self.assertEqual( bandwidth_tracker.GetUsage( HC.BANDWIDTH_TYPE_DATA, 0 ), 0 )
         self.assertEqual( bandwidth_tracker.GetUsage( HC.BANDWIDTH_TYPE_REQUESTS, 0 ), 0 )
         
         self.assertEqual( bandwidth_tracker.GetUsage( HC.BANDWIDTH_TYPE_DATA, 1 ), 0 )
         self.assertEqual( bandwidth_tracker.GetUsage( HC.BANDWIDTH_TYPE_REQUESTS, 1 ), 0 )
         
         self.assertEqual( bandwidth_tracker.GetUsage( HC.BANDWIDTH_TYPE_DATA, 2 ), 0 )
         self.assertEqual( bandwidth_tracker.GetUsage( HC.BANDWIDTH_TYPE_REQUESTS, 2 ), 0 )
         
         self.assertEqual( bandwidth_tracker.GetUsage( HC.BANDWIDTH_TYPE_DATA, 6 ), 1024 )
         self.assertEqual( bandwidth_tracker.GetUsage( HC.BANDWIDTH_TYPE_REQUESTS, 6 ), 1 )
         
         self.assertEqual( bandwidth_tracker.GetUsage( HC.BANDWIDTH_TYPE_DATA, 3600 ), 1024 )
         self.assertEqual( bandwidth_tracker.GetUsage( HC.BANDWIDTH_TYPE_REQUESTS, 3600 ), 1 )
         
         self.assertEqual( bandwidth_tracker.GetUsage( HC.BANDWIDTH_TYPE_DATA, None ), 1024 )
         self.assertEqual( bandwidth_tracker.GetUsage( HC.BANDWIDTH_TYPE_REQUESTS, None ), 1 )
         
         #
         
         bandwidth_tracker.ReportDataUsed( 32 )
         bandwidth_tracker.ReportRequestUsed()
         
         bandwidth_tracker.ReportDataUsed( 32 )
         bandwidth_tracker.ReportRequestUsed()
         
         self.assertEqual( bandwidth_tracker.GetCurrentMonthSummary(), 'used 1.1KB in 3 requests this month' )
         
         self.assertEqual( bandwidth_tracker.GetUsage( HC.BANDWIDTH_TYPE_DATA, 0 ), 0 )
         self.assertEqual( bandwidth_tracker.GetUsage( HC.BANDWIDTH_TYPE_REQUESTS, 0 ), 0 )
         
         self.assertEqual( bandwidth_tracker.GetUsage( HC.BANDWIDTH_TYPE_DATA, 1 ), 64 )
         self.assertEqual( bandwidth_tracker.GetUsage( HC.BANDWIDTH_TYPE_REQUESTS, 1 ), 2 )
         
         self.assertEqual( bandwidth_tracker.GetUsage( HC.BANDWIDTH_TYPE_DATA, 2 ), 64 )
         self.assertEqual( bandwidth_tracker.GetUsage( HC.BANDWIDTH_TYPE_REQUESTS, 2 ), 2 )
         
         self.assertEqual( bandwidth_tracker.GetUsage( HC.BANDWIDTH_TYPE_DATA, 6 ), 1088 )
         self.assertEqual( bandwidth_tracker.GetUsage( HC.BANDWIDTH_TYPE_REQUESTS, 6 ), 3 )
         
         self.assertEqual( bandwidth_tracker.GetUsage( HC.BANDWIDTH_TYPE_DATA, 3600 ), 1088 )
         self.assertEqual( bandwidth_tracker.GetUsage( HC.BANDWIDTH_TYPE_REQUESTS, 3600 ), 3 )
         
         self.assertEqual( bandwidth_tracker.GetUsage( HC.BANDWIDTH_TYPE_DATA, None ), 1088 )
         self.assertEqual( bandwidth_tracker.GetUsage( HC.BANDWIDTH_TYPE_REQUESTS, None ), 3 )
Ejemplo n.º 19
0
 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 ) )