def test_if_message_fails_error_incremented(self, mock_get_router): self.assertEqual(self.batch.errors, 0) # send the message, mocking an exception mock_get_router.return_value.send_to_backend.side_effect = MessageSendingError send_messages(self.batch) batch = Batch.objects.get(pk=self.batch.pk) self.assertEqual(batch.errors, 1) self.assertFalse(self.bulk_msg.sms)
def test_sent_messages_dont_get_resent(self): for i in range(20): BulkMessageFactory(batch=self.batch, sms=None) for i in range(10): sms = SMSFactory() BulkMessageFactory(batch=self.batch, sms=sms) BulkMessageFactory(batch=self.batch, sms=SMSFactory(), deleted=True) send_messages(self.batch) # only 21 messages get sent out (20 + 1 from setUp) self.assertEqual(len(self.outbound), 21)
def test_once_all_are_sent_next_send_completes_batch(self): for i in range(20): BulkMessageFactory(batch=self.batch, sms=None) BulkMessageFactory(batch=self.batch, sms=None, deleted=True) # send out all the messages (21) send_messages(self.batch) self.assertEqual(len(self.outbound), 21) # send again BulkMessageFactory(batch=self.batch, sms=None, deleted=True) send_messages(self.batch) # now batch should be completed batch = Batch.objects.get(pk=self.batch.pk) self.assertEqual(batch.status, Batch.COMPLETED)
def test_bulk_sms_send_multiproc(self): # close database connections manually to make sure they're not passed # to subprocesses for conn in django_db_connections.all(): conn.close() # send the messages in parallel using 10 processes. use # multiprocessing rather than threads so we don't have to clean # up database connections that the threads might leave open. cache.close() pool = multiprocessing.Pool(10) try: for i in range(30): BulkMessageFactory(batch=self.batch, sms=None) for j in range(10): BulkMessageFactory(batch=self.batch, sms=None, deleted=True) # 40 is the first multiple of 10 greater than or equal to 31 num_sent = send_messages(self.batch, num_to_send=40, map_=pool.map) batch = Batch.objects.get(pk=self.batch.pk) self.assertEqual(batch.errors, 0) self.assertEqual(batch.status, Batch.COMPLETED) # assert that we report the right number sent too self.assertEqual(num_sent, 30) self.assertEqual(len(self.outbound), 30) finally: pool.terminate() pool.join()
def test_bulk_sms_send_one_proc(self): for i in range(30): BulkMessageFactory(batch=self.batch, sms=None) for j in range(10): BulkMessageFactory(batch=self.batch, sms=None, deleted=True) num_sent = send_messages(self.batch) # 31 messages sent (30 + 1 from setUp) self.assertEqual(len(self.outbound), 31) # assert that we report the right number sent too self.assertEqual(num_sent, 31)
def test_dont_send_batch_if_batch_inactive(self): self.batch.status = Batch.PENDING self.batch.save() send_messages(self.batch) # no message sent self.assertEqual(len(self.outbound), 0)
def test_send_finite_number_each_time(self): for i in range(20): BulkMessageFactory(batch=self.batch, sms=None) send_messages(self.batch, num_to_send=10) # only 10 messages should be sent out each call self.assertEqual(len(self.outbound), 10)