def setUp(self):
     self.queue = MailQueue()
     self.watcher = MailQueueWatcher(self.queue)
     self.mkdir(self.queue._get_maildir())
class MailQueueWatcherTest(TestCase, QueueHelperMixin):
    def setUp(self):
        self.queue = MailQueue()
        self.watcher = MailQueueWatcher(self.queue)
        self.mkdir(self.queue._get_maildir())

    def test_process_events(self):
        self.watcher.start()
        self.assertQueueIsEmpty()

        self.create_mail('a')
        self.watcher.process_events()

        self.assertEqual(len(self.queue.queue), 1)
        self.assertEqual(self.queue.queue[0].identifier, 'a')

    def test_process_events_when_hardlinked_into_maildir(self):
        self.watcher.start()
        self.assertQueueIsEmpty()

        self.create_mail('a', hardlink_from_tmp=True)
        self.watcher.process_events()

        self.assertEqual(len(self.queue.queue), 1)
        self.assertEqual(self.queue.queue[0].identifier, 'a')

    def test_process_events_does_not_block(self):
        self.watcher.start()

        before = datetime.now()
        self.watcher.process_events()
        after = datetime.now()

        delta = after - before
        self.assertLess(delta.total_seconds(), 1)

    def test_start_fails_when_dir_does_not_exist(self):
        os.rmdir(self.queue._get_maildir())

        with self.assertRaises(pyinotify.WatchManagerError):
            self.watcher.start()