def test_next_task_removed_file(self):
        # cleaning tasks_dirs
        tasks_dir = os.path.join(self.c_dir, 'test_next_task_removed_file')
        to_proc_dir = os.path.join(tasks_dir, 'to_proc')
        ready_dir = os.path.join(tasks_dir, 'ready')
        shutil.rmtree(tasks_dir, ignore_errors=True)

        # creating tasks
        os.makedirs(to_proc_dir)
        os.makedirs(ready_dir)
        task_name = '101'
        with open(os.path.join(to_proc_dir, task_name), 'w') as f:
            f.write('one\n')
            f.write('two\n')
        tm = TaskManager(to_proc_dir, ready_dir)
        self.assertTrue(task_name in os.listdir(to_proc_dir))
        self.assertTrue(task_name not in os.listdir(ready_dir))
        tm.next_task()

        # cleaning tasks_dirs
        shutil.rmtree(tasks_dir, ignore_errors=True)

        # checking task removed from tasks sets
        self.assertRaises(TaskFromFileLoadingError, tm.next_task)
        self.assertTrue(task_name not in tm.tasks)
        self.assertTrue(task_name not in tm.tasks_in_process)
    def test_next_task_name(self):
        tm = TaskManager(self.tasks_dir, self.r_t_dir)
        tasks_num = len(tm.tasks)
        self.assertTrue(tasks_num>0)
        self.assertTrue(len(tm.tasks_in_process)==0)
        for _ in range(tasks_num):
            t_name = tm.next_task_name()
            self.assertTrue(t_name in tm.tasks_in_process)
            self.assertTrue(t_name not in tm.tasks)

        # checking tasks_in_process switching with tasks
        self.assertEquals(0, len(tm.tasks))
        self.assertEquals(tasks_num, len(tm.tasks_in_process))
        t_name = tm.next_task_name()
        self.assertEquals(tasks_num - 1, len(tm.tasks))
        self.assertEquals(1, len(tm.tasks_in_process))
        self.assertTrue(t_name in tm.tasks_in_process)
        self.assertTrue(t_name not in tm.tasks)
 def test_reload_tasks(self):
     tm = TaskManager(self.tasks_dir, self.r_t_dir)
     tm.tasks = set()
     tm.tasks_in_process = set()
     tm.reload_tasks()
     exp_tasks = set(['1', 'one'])
     act_tasks = tm._tasks_to_proc()
     self.assertEquals(exp_tasks, act_tasks)
    def test_receive_task(self):
        # cleaning tasks_dirs
        tasks_dir = os.path.join(self.c_dir, 'test_receive_task')
        to_proc_dir = os.path.join(tasks_dir, 'to_proc')
        ready_dir = os.path.join(tasks_dir, 'ready')
        shutil.rmtree(tasks_dir, ignore_errors=True)
        # creating tasks
        os.makedirs(to_proc_dir)
        os.makedirs(ready_dir)
        task_name = '100'
        with open(os.path.join(to_proc_dir, task_name), 'w') as f:
            f.write('one\n')
            f.write('two\n')
        # testing task receive
        tm = TaskManager(to_proc_dir, ready_dir)
        self.assertTrue(task_name in os.listdir(to_proc_dir))
        self.assertTrue(task_name not in os.listdir(ready_dir))
        task = tm.next_task()
        self.assertEquals(1, len(tm.tasks_in_process))
        task.result = 'ready'
        tm.receive_task(task)
        self.assertTrue(task_name in os.listdir(to_proc_dir))
        self.assertTrue(task_name in os.listdir(ready_dir))
        self.assertEquals(0, len(tm.tasks_in_process))
        with open(os.path.join(ready_dir, task_name)) as f:
            self.assertEquals(task.result, f.read())

        # testing duplicate receive_task call
        task.result = 'new_%s' % task.result
        tm.receive_task(task)
        self.assertTrue(task_name in os.listdir(to_proc_dir))
        self.assertTrue(task_name in os.listdir(ready_dir))
        self.assertEquals(0, len(tm.tasks_in_process))
        with open(os.path.join(ready_dir, task_name)) as f:
            self.assertEquals(task.result, f.read())

        # cleaning tasks_dirs
        shutil.rmtree(tasks_dir, ignore_errors=True)
 def test_tasks_to_proc(self):
     tm = TaskManager(self.tasks_dir, self.r_t_dir)
     exp_tasks = set(['1', 'one'])
     act_tasks = tm._tasks_to_proc()
     self.assertEquals(exp_tasks, act_tasks)