示例#1
0
    def test_deletes_workers(self, mock_filter, mock_delete_worker):
        mock_filter.return_value = [
            resources.Worker('name1', datetime.utcnow()),
            resources.Worker('name2', datetime.utcnow()),
        ]

        scheduler.WorkerTimeoutMonitor().check_workers()

        # make sure _delete_worker is only called for the two expected calls
        mock_delete_worker.assert_has_calls(
            [mock.call('name1'), mock.call('name2')])
示例#2
0
    def test_deletes_workers(self, mock_filter, mock_delete):
        mock_filter.return_value = [
            resources.Worker('name1', datetime.utcnow()),
            resources.Worker('name2', datetime.utcnow()),
        ]

        scheduler.WorkerTimeoutMonitor().check_workers()

        # make sure only these two queues were deleted
        mock_delete.assert_any_call(args=('name1', ),
                                    queue=RESOURCE_MANAGER_QUEUE)
        mock_delete.assert_any_call(args=('name2', ),
                                    queue=RESOURCE_MANAGER_QUEUE)
        self.assertEqual(mock_delete.call_count, 2)
示例#3
0
    def test_delete_with_reserved_resources(self):
        """
        Test delete() for a Worker with a ReservedResource referencing its queue_name.
        """
        now = datetime.utcnow()
        worker = resources.Worker('worker_with_a_reserved_resource', now)
        worker.save()
        workers_collection = resources.Worker.get_collection()
        self.assertEqual(workers_collection.find({'_id': worker.name}).count(), 1)

        # Create 3 resources, 2 referencing the worker to be deleted and 1 with no worker references
        rr1 = resources.ReservedResource('reserved_resource1', assigned_queue=worker.queue_name,
                                         num_reservations=1)
        rr2 = resources.ReservedResource('reserved_resource2', assigned_queue=worker.queue_name,
                                         num_reservations=1)
        rr = resources.ReservedResource('reserved_resource_no_queue', num_reservations=0)
        for r in (rr1, rr2, rr):
            r.save()
        rrc = resources.ReservedResource.get_collection()
        self.assertEqual(rrc.count(), 3)
        self.assertEqual(rrc.find({'assigned_queue': worker.queue_name}).count(), 2)

        worker.delete()

        # Make sure that only the resource with reference to the deleted Worker's queue_name is
        # deleted
        self.assertEqual(workers_collection.count(), 0)
        self.assertEqual(rrc.count(), 1)
        self.assertFalse(rrc.find_one(
            {'_id': 'reserved_resource_no_queue', 'num_reservations': 0}) is None)
示例#4
0
    def test_delete(self):
        """
        Test delete().
        """
        now = datetime.utcnow()
        worker = resources.Worker('wont_exist_for_long', now)
        worker.save()
        workers_collection = resources.Worker.get_collection()
        self.assertEqual(workers_collection.find({'_id': 'wont_exist_for_long'}).count(), 1)

        worker.delete()

        self.assertEqual(workers_collection.count(), 0)
示例#5
0
    def test___init___defaults(self, super_init):
        """
        Test __init__() with default values.
        """
        now = datetime.utcnow()
        worker = resources.Worker('some_name', now)

        # The superclass __init__ should have been called
        super_init.assert_called_once_with(worker)
        # Make sure the attributes are correct
        self.assertEqual(worker.name, 'some_name')
        self.assertEqual(worker.last_heartbeat, now)
        self.assertEqual('_id' in worker, False)
        self.assertEqual('id' in worker, False)
示例#6
0
    def test_save(self):
        """
        Test the save() method.
        """
        last_heartbeat = datetime(2013, 12, 16)

        worker = resources.Worker('a_worker', last_heartbeat)

        worker.save()

        # Make sure the DB has the correct data
        workers_collection = resources.Worker.get_collection()
        self.assertEqual(workers_collection.count(), 1)
        saved_worker = workers_collection.find_one({'_id': 'a_worker'})
        self.assertEqual(saved_worker['last_heartbeat'], last_heartbeat)
示例#7
0
    def test_from_bson(self):
        """
        Test from_bson().
        """
        last_heartbeat = datetime(2013, 12, 16)
        worker = resources.Worker('a_worker', last_heartbeat)
        worker.save()
        workers_collection = resources.Worker.get_collection()
        worker_bson = workers_collection.find_one({'_id': 'a_worker'})

        # Replace the worker reference with a newly instantiated Worker from our bson
        worker = resources.Worker.from_bson(worker_bson)

        self.assertEqual(worker.name, 'a_worker')
        self.assertEqual(worker.last_heartbeat, last_heartbeat)