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