<commit_before># -*- coding: utf-8 -*-
##
##
## This file is part of CDS Indico.
## Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007 CERN.
##
## CDS Indico is free software; you can redistribute it and/or
## modify it under the terms of the GNU General Public License as
## published by the Free Software Foundation; either version 2 of the
## License, or (at your option) any later version.
##
## CDS Indico is distributed in the hope that it will be useful, but
## WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
## General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with CDS Indico; if not, write to the Free Software Foundation, Inc.,
## 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.

"""
Tests for scheduler base classes
"""
import unittest, threading, multiprocessing
import time
from datetime import timedelta
from dateutil import rrule

from MaKaC.common.db import DBMgr

from indico.util.date_time import nowutc
from indico.modules.scheduler import Scheduler, SchedulerModule, Client, base
from indico.modules.scheduler.tasks import OneShotTask, PeriodicTask

terminated = None


class TestTimeSource(base.TimeSource):
    def __init__(self, factor):
        self._startTime = nowutc()
        self._factor = factor

    def getCurrentTime(self):
        realDiff = nowutc() - self._startTime
        seconds = (realDiff.seconds + realDiff.microseconds / 1E6)

        fakeDiff = timedelta(seconds = seconds*self._factor)

        return self._startTime + fakeDiff

    def sleep(self, amount):
        time.sleep(amount/float(self._factor))


base.TimeSource.set(TestTimeSource(2))

class TestTask(OneShotTask):

    def __init__(self, myid, date_time):
        super(TestTask, self).__init__(date_time)
        self._id = myid

    def run(self):
        base.TimeSource.get().sleep(1)
        terminated[self._id] = 1


class TestFailTask(TestTask):
    def run(self):
        base.TimeSource.get().sleep(1)
        terminated[self._id] = 1
        raise Exception('I fail!')


class TestPeriodicTask(PeriodicTask):
    def __init__(self, myid, freq, **kwargs):
        super(TestPeriodicTask, self).__init__(freq, **kwargs)
        self._id = myid

    def run(self):
        terminated[self._id] += 1


class TestPeriodicFailTask(PeriodicTask):
    def __init__(self, myid, freq, **kwargs):
        super(TestPeriodicFailTask, self).__init__(freq, **kwargs)
        self._id = myid

    def run(self):

        terminated[self._id] += 1

        if terminated[self._id] % 2 == 0:
            raise Exception('I fail! %s' % terminated[self._id])


class Worker(threading.Thread):

    def __init__(self, myid, task_t, time, **kwargs):
        super(Worker, self).__init__()
        self._id = myid
        self.finished = False
        self._task_t = task_t
        self._time = time
        self._extra_args = kwargs

    def run(self):
        DBMgr.getInstance().startRequest()
        c = Client()
        c.enqueue(self._task_t(self._id, self._time, **self._extra_args))
        DBMgr.getInstance().endRequest()
        pass


class SchedulerThread(threading.Thread):

    def __init__(self, mode):
        super(SchedulerThread, self).__init__()
        self._mode = mode

    def run(self):
        DBMgr.getInstance().startRequest()
        s = Scheduler(sleep_interval = 1,
                      task_max_tries = 1,
                      multitask_mode = self._mode)
        self.result = s.run()
        DBMgr.getInstance().endRequest()


class _TestScheduler(unittest.TestCase):

    def setUp(self):

        DBMgr.getInstance()._conn = {}

        DBMgr.getInstance().startRequest()
        self._smodule = SchedulerModule.getDBInstance()
        DBMgr.getInstance().commit()

        self._sched = SchedulerThread(self._mode)
        self._sched.start()

    def _checkWorkersFinished(self, timeout, value=1):

        global terminated
<commit_msg>[FIX] scheduler - `TimeSource bug<commit_after># -*- coding: utf-8 -*-
##
##
## This file is part of CDS Indico.
## Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007 CERN.
##
## CDS Indico is free software; you can redistribute it and/or
## modify it under the terms of the GNU General Public License as
## published by the Free Software Foundation; either version 2 of the
## License, or (at your option) any later version.
##
## CDS Indico is distributed in the hope that it will be useful, but
## WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
## General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with CDS Indico; if not, write to the Free Software Foundation, Inc.,
## 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.

"""
Tests for scheduler base classes
"""
import unittest, threading, multiprocessing
import time
from datetime import timedelta
from dateutil import rrule

from MaKaC.common.db import DBMgr

from indico.util.date_time import nowutc
from indico.modules.scheduler import Scheduler, SchedulerModule, Client, base
from indico.modules.scheduler.tasks import OneShotTask, PeriodicTask

terminated = None


class TestTimeSource(base.TimeSource):
    def __init__(self, factor):
        self._startTime = nowutc()
        self._factor = factor

    def getCurrentTime(self):
        realDiff = nowutc() - self._startTime
        seconds = (realDiff.seconds + realDiff.microseconds / 1E6)

        fakeDiff = timedelta(seconds = seconds*self._factor)

        return self._startTime + fakeDiff

    def sleep(self, amount):
        time.sleep(amount/float(self._factor))


class TestTask(OneShotTask):

    def __init__(self, myid, date_time):
        super(TestTask, self).__init__(date_time)
        self._id = myid

    def run(self):
        base.TimeSource.get().sleep(1)
        terminated[self._id] = 1


class TestFailTask(TestTask):
    def run(self):
        base.TimeSource.get().sleep(1)
        terminated[self._id] = 1
        raise Exception('I fail!')


class TestPeriodicTask(PeriodicTask):
    def __init__(self, myid, freq, **kwargs):
        super(TestPeriodicTask, self).__init__(freq, **kwargs)
        self._id = myid

    def run(self):
        terminated[self._id] += 1


class TestPeriodicFailTask(PeriodicTask):
    def __init__(self, myid, freq, **kwargs):
        super(TestPeriodicFailTask, self).__init__(freq, **kwargs)
        self._id = myid

    def run(self):

        terminated[self._id] += 1

        if terminated[self._id] % 2 == 0:
            raise Exception('I fail! %s' % terminated[self._id])


class Worker(threading.Thread):

    def __init__(self, myid, task_t, time, **kwargs):
        super(Worker, self).__init__()
        self._id = myid
        self.finished = False
        self._task_t = task_t
        self._time = time
        self._extra_args = kwargs

    def run(self):
        DBMgr.getInstance().startRequest()
        c = Client()
        c.enqueue(self._task_t(self._id, self._time, **self._extra_args))
        DBMgr.getInstance().endRequest()
        pass


class SchedulerThread(threading.Thread):

    def __init__(self, mode):
        super(SchedulerThread, self).__init__()
        self._mode = mode

    def run(self):
        DBMgr.getInstance().startRequest()
        s = Scheduler(sleep_interval = 1,
                      task_max_tries = 1,
                      multitask_mode = self._mode)
        self.result = s.run()
        DBMgr.getInstance().endRequest()


class _TestScheduler(unittest.TestCase):

    def setUp(self):

        base.TimeSource.set(TestTimeSource(2))

        DBMgr.getInstance()._conn = {}

        DBMgr.getInstance().startRequest()
        self._smodule = SchedulerModule.getDBInstance()
        DBMgr.getInstance().commit()

        self._sched = SchedulerThread(self._mode)
        self._sched.start()

    def _checkWorkersFinished(self, timeout, value=1):

        global terminated

{'fraction_non_alphanumeric': 0.10787459886447791, 'fraction_numerical': 0.013330041964946927, 'mean_word_length': 2.968168462291871, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5015735', 'n_tokens_mistral': 2525, 'n_tokens_neox': 2376, 'n_words': 679}