def test_send_dm(self):
   user = '******'
   message = 'unittest message. '
   # Adding some random characters so twitter doesn't complain about
   # duplicate messages
   for _ in range(5):
     message += random.choice(string.ascii_lowercase + string.digits)
   self.assertTrue(twitter.send_dm(user, message))
def do_deferred_job():
  '''Attempts to run a job in the queue. Returns True if a job is found.'''
  message_to_send = database.pop_message_to_send()
  if not message_to_send:
    return False
  
  # If it's been more then 5 minutes, throw away the job
  if time() > message_to_send.time + (5 * 60):
    logging.error(
      "Timing out DM to %s. dm.time: %s, current time: %s. Message: %s" \
        % (message_to_send.user, message_to_send.time, time(),
           message_to_send.message))
    return True
      
  # Job is still valid. Run it!
  if twitter.send_dm(message_to_send.user, message_to_send.message):
    logging.info('sent DM to user: %s' % message_to_send.user)
  else:
    logging.warn('DM to user %s failed' % message_to_send.user)
    # We couldn't finish the job - push it back into the queue
    database.push_message_to_send(message_to_send)