cubicweb/server/test/unittest_utils.py
author Denis Laxalde <denis.laxalde@logilab.fr>
Mon, 06 Mar 2017 13:21:50 +0100
changeset 12011 d2888fee6031
parent 12007 b82cda5ba3bf
child 12567 26744ad37953
permissions -rw-r--r--
[server] introduce a scheduler class to run repository "looping tasks" We just use the sched module from the standard library and introduce a tiny Python2/3 compatibility layer (more for convenience actually). The "looping" aspect of tasks (previously in LoopTask class) is re-implemeted as a `schedule_periodic_task` function. This is a reasonably thin layer as compared to LoopTask/TasksManager classes. Only the "restart" aspect of LoopTask is no longer present as I'm not sure it's worth keeping. The advantage of using this (in addition to eventually dropping our custom code) is that this scheduler class provides a `run` method that blocks the process while running tasks in its queue. So we can rely on this to have a 'scheduler' ctl command (see forthcoming patch) that would only run "looping tasks" without having to implement the "blocking" aspect ourself. Related to #17057223.

# copyright 2012 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
#
# This file is part of CubicWeb.
#
# CubicWeb is free software: you can redistribute it and/or modify it under the
# terms of the GNU Lesser General Public License as published by the Free
# Software Foundation, either version 2.1 of the License, or (at your option)
# any later version.
#
# CubicWeb 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 Lesser General Public License for more
# details.
#
# You should have received a copy of the GNU Lesser General Public License along
# with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
"""Tests for cubicweb.server.utils module."""

from cubicweb.devtools import testlib
from cubicweb.server import utils


class UtilsTC(testlib.BaseTestCase):

    def test_crypt(self):
        for hash in (
            utils.crypt_password('xxx'),  # default sha512
            b'ab$5UsKFxRKKN.d8iBIFBnQ80',  # custom md5
            b'ab4Vlm81ZUHlg',  # DES
        ):
            self.assertEqual(utils.crypt_password('xxx', hash), hash)
            self.assertEqual(utils.crypt_password(u'xxx', hash), hash)
            self.assertEqual(utils.crypt_password(u'xxx', hash.decode('ascii')),
                             hash.decode('ascii'))
            self.assertEqual(utils.crypt_password('yyy', hash), b'')

        # accept any password for empty hashes (is it a good idea?)
        self.assertEqual(utils.crypt_password('xxx', ''), '')
        self.assertEqual(utils.crypt_password('yyy', ''), '')

    def test_schedule_periodic_task(self):
        scheduler = utils.scheduler()
        this = []

        def fill_this(x):
            this.append(x)
            if len(this) > 2:
                raise SystemExit()
            elif len(this) > 1:
                raise RuntimeError()

        event = utils.schedule_periodic_task(scheduler, 0.01, fill_this, 1)
        self.assertEqual(event.action.__name__, 'fill_this')
        self.assertEqual(len(scheduler.queue), 1)

        with self.assertLogs('cubicweb.scheduler', level='ERROR') as cm:
            scheduler.run()
        self.assertEqual(this, [1] * 3)
        self.assertEqual(len(cm.output), 2)
        self.assertIn('Unhandled exception in periodic task "fill_this"',
                      cm.output[0])
        self.assertIn('"fill_this" not re-scheduled', cm.output[1])


if __name__ == '__main__':
    import unittest
    unittest.main()