Skip to content

Commit

Permalink
Added unit tests for when the queue has items and the queue size is c…
Browse files Browse the repository at this point in the history
…hanging vs not changing.
  • Loading branch information
Medhat Gayed committed Jan 23, 2018
1 parent edef370 commit 867c2e4
Showing 1 changed file with 21 additions and 9 deletions.
30 changes: 21 additions & 9 deletions tests/test_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
@mock.patch.object(settings, 'DEFAULT_FROM_EMAIL', 'sample_from@email.com')
@mock.patch.object(settings, 'ADMINS', ['sample_admin@email.com'])
class RQPulseCheckTestCase(TestCase):
def test_number_of_workers_is_not_expected(self, mock_queue, mock_worker, mock_send_mail,
mock_log, mock_time):
def test_number_of_workers_is_not_expected(
self, mock_queue, mock_worker, mock_send_mail, mock_log, mock_time):
mock_worker.all.return_value = []
call_command('rq_pulse_check')
mock_send_mail.assert_called_with(
Expand All @@ -32,22 +32,34 @@ def test_number_of_workers_is_not_expected(self, mock_queue, mock_worker, mock_s
'The number of workers 1 does not equal the expected number 2. Workers maybe down.',
'sample_from@email.com', ['sample_admin@email.com'])

def test_number_of_workers_is_expected(self, mock_queue, mock_worker, mock_send_mail,
mock_log, mock_time):
def test_number_of_workers_is_expected(
self, mock_queue, mock_worker, mock_send_mail, mock_log, mock_time):
mock_worker.all.return_value = [mock.MagicMock(), mock.MagicMock()]
call_command('rq_pulse_check')
self.assertFalse(mock_send_mail.called)

def test_queue_has_no_items(self, mock_queue, mock_worker, mock_send_mail,
mock_log, mock_time):
def test_queue_has_no_items(
self, mock_queue, mock_worker, mock_send_mail, mock_log, mock_time):
mock_queue.return_value.__len__.return_value = 0
call_command('rq_pulse_check')
self.assertFalse(mock_time.sleep.called)

def test_queue_has_items(self, mock_queue, mock_worker, mock_send_mail,
mock_log, mock_time):
def test_queue_has_items_and_queue_size_not_changing(
self, mock_queue, mock_worker, mock_send_mail, mock_log, mock_time):
mock_worker.all.return_value = [mock.MagicMock(), mock.MagicMock()]
mock_queue.return_value.__len__.return_value = 1
call_command('rq_pulse_check')
self.assertListEqual(mock_time.sleep.call_args_list,
[mock.call(5), mock.call(5), mock.call(5), mock.call(5), mock.call(5)])

mock_send_mail.assert_called_with(
'WARNING: RQ Workers maybe down!',
'The Q size is not changing, this is bad. Workers maybe down.',
'sample_from@email.com', ['sample_admin@email.com'])

def test_queue_has_items_and_queue_size_is_changing(
self, mock_queue, mock_worker, mock_send_mail, mock_log, mock_time):
mock_worker.all.return_value = [mock.MagicMock(), mock.MagicMock()]
mock_queue.return_value.__len__.side_effect = [10, 9]
call_command('rq_pulse_check')
self.assertListEqual(mock_time.sleep.call_args_list, [mock.call(5)])
self.assertFalse(mock_send_mail.called)

0 comments on commit 867c2e4

Please sign in to comment.