コード例 #1
0
    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)
コード例 #2
0
ファイル: test_webservice.py プロジェクト: rdswift/picard
 def test_from_request(self):
     request = WSRequest('', 'example.com', 443, '', None, priority=True)
     func = 1
     task = RequestTask.from_request(request, func)
     self.assertEqual(request.get_host_key(), task.hostkey)
     self.assertEqual(func, task.func)
     self.assertEqual(1, task.priority)
     self.assertEqual((request.get_host_key(), func, 1), task)
コード例 #3
0
ファイル: test_webservice.py プロジェクト: rdswift/picard
 def test_add_task(self):
     request = WSRequest("", "abc.xyz", 80, "", None)
     func = 1
     task = self.ws.add_task(func, request)
     self.assertEqual((request.get_host_key(), func, 0), task)
     self.ws._queue.add_task.assert_called_with(task, False)
     request.important = True
     task = self.ws.add_task(func, request)
     self.ws._queue.add_task.assert_called_with(task, True)
コード例 #4
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 = 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])
コード例 #5
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])
コード例 #6
0
    def test_remove_task(self):
        host = "abc.xyz"
        port = 80
        request = WSRequest("", host, port, "", None)
        key = request.get_host_key()

        # Add a task and check for its existance
        task = self.ws.add_task(0, request)
        self.assertIn(key, self.ws._queues[0])
        self.assertEqual(len(self.ws._queues[0][key]), 1)

        # Remove the task and check
        self.ws.remove_task(task)
        self.assertIn(key, self.ws._queues[0])
        self.assertEqual(len(self.ws._queues[0][key]), 0)

        # Try to remove a non existing task and check for errors
        non_existing_task = (1, "a", "b")
        self.ws.remove_task(non_existing_task)
コード例 #7
0
    def test_remove_task(self):
        host = "abc.xyz"
        port = 80
        request = WSRequest("", host, port, "", None)
        key = request.get_host_key()

        # Add a task and check for its existance
        task = self.ws.add_task(0, request)
        self.assertIn(key, self.ws._queues[0])
        self.assertEqual(len(self.ws._queues[0][key]), 1)

        # Remove the task and check
        self.ws.remove_task(task)
        self.assertIn(key, self.ws._queues[0])
        self.assertEqual(len(self.ws._queues[0][key]), 0)

        # Try to remove a non existing task and check for errors
        non_existing_task = (1, "a", "b")
        self.ws.remove_task(non_existing_task)
コード例 #8
0
    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)
コード例 #9
0
ファイル: test_webservice.py プロジェクト: rdswift/picard
    def test_add_task_calls_timers(self):
        mock_timer1 = self.ws._timer_run_next_task
        mock_timer2 = self.ws._timer_count_pending_requests
        request = WSRequest("", "abc.xyz", 80, "", None)

        self.ws.add_task(0, request)
        mock_timer1.start.assert_not_called()
        mock_timer2.start.assert_not_called()

        # Test if timer start was called in case it was inactive
        mock_timer1.isActive.return_value = False
        mock_timer2.isActive.return_value = False
        self.ws.add_task(0, request)
        mock_timer1.start.assert_called_with(0)
        mock_timer2.start.assert_called_with(0)
コード例 #10
0
    def add_task(self, handler):
        """
        Use the webservice queue to add a task -- a simple function.
        """
        # count requests
        self.album._requests += 1  # noqa
        self.requests += 1

        # queue function call
        return ws.add_task(
            # wrap the handler in the finished decorator
            func=self.finished(handler),
            request=WSRequest(
                method='GET',
                host=settings.LASTFM_HOST,
                port=settings.LASTFM_PORT,
                path=settings.LASTFM_PATH,
                handler=self.finished(handler),
            ),
        )