def test_add_task(self): mock_timer1 = self.ws._timer_run_next_task mock_timer2 = self.ws._timer_count_pending_requests host = "abc.xyz" port = 80 request = WSRequest("", host, port, "", None) key = request.get_host_key() self.ws.add_task(0, request) request.priority = True self.ws.add_task(0, request) request.important = True self.ws.add_task(1, request) # Test if timer start was called in case it was inactive mock_timer1.isActive.return_value = False mock_timer2.isActive.return_value = False request.priority = False self.ws.add_task(1, request) self.assertIn('start', repr(mock_timer1.method_calls)) # Test if key was added to prio queue self.assertEqual(len(self.ws._queues[1]), 1) self.assertIn(key, self.ws._queues[1]) # Test if 2 requests were added in prio queue self.assertEqual(len(self.ws._queues[1][key]), 2) # Test if important request was added ahead in the queue self.assertEqual(self.ws._queues[0][key][0], 1)
def test_run_task(self): host = "abc.xyz" port = 80 request = WSRequest("", host, port, "", None) key = request.get_host_key() mock_task = MagicMock() mock_task2 = MagicMock() delay_func = ratecontrol.get_delay_to_next_request = MagicMock() # Patching the get delay function to delay the 2nd task on queue to the next call delay_func.side_effect = [(False, 0), (True, 0), (False, 0), (False, 0), (False, 0), (False, 0)] self.ws.add_task(mock_task, request) request.priority = True self.ws.add_task(mock_task2, request) request.priority = False self.ws.add_task(mock_task, request) self.ws.add_task(mock_task, request) # Ensure no tasks are run before run_next_task is called self.assertEqual(mock_task.call_count, 0) self.ws._run_next_task() # Ensure priority task is run first self.assertEqual(mock_task2.call_count, 1) self.assertEqual(mock_task.call_count, 0) self.assertIn(key, self.ws._queues[1]) # Ensure that the calls are run as expected self.ws._run_next_task() self.assertEqual(mock_task.call_count, 1) # Checking if the cleanup occured on the prio queue self.assertNotIn(key, self.ws._queues[1]) # Check the call counts on proper execution of tasks self.ws._run_next_task() self.assertEqual(mock_task.call_count, 2) self.ws._run_next_task() self.assertEqual(mock_task.call_count, 3) # Ensure that the clean up happened on the normal queue self.ws._run_next_task() self.assertEqual(mock_task.call_count, 3) self.assertNotIn(key, self.ws._queues[0])
def test_run_task(self): host = "abc.xyz" port = 80 request = WSRequest("", host, port, "", None) key = request.get_host_key() mock_task = MagicMock() mock_task2 = MagicMock() delay_func = self.ws._get_delay_to_next_request = MagicMock() # Patching the get delay function to delay the 2nd task on queue to the next call delay_func.side_effect = [(False, 0), (True, 0), (False, 0), (False, 0), (False, 0), (False, 0)] self.ws.add_task(mock_task, request) request.priority = True self.ws.add_task(mock_task2, request) request.priority = False self.ws.add_task(mock_task, request) self.ws.add_task(mock_task, request) # Ensure no tasks are run before run_next_task is called self.assertEqual(mock_task.call_count, 0) self.ws._run_next_task() # Ensure priority task is run first self.assertEqual(mock_task2.call_count, 1) self.assertEqual(mock_task.call_count, 0) self.assertIn(key, self.ws._queues[1]) # Ensure that the calls are run as expected self.ws._run_next_task() self.assertEqual(mock_task.call_count, 1) # Checking if the cleanup occured on the prio queue self.assertNotIn(key, self.ws._queues[1]) # Check the call counts on proper execution of tasks self.ws._run_next_task() self.assertEqual(mock_task.call_count, 2) self.ws._run_next_task() self.assertEqual(mock_task.call_count, 3) # Ensure that the clean up happened on the normal queue self.ws._run_next_task() self.assertEqual(mock_task.call_count, 3) self.assertNotIn(key, self.ws._queues[0])