summaryrefslogtreecommitdiff
path: root/tests/glibext/workqueue.py
blob: 203970b0ffc26e28b9a85d79b497b94ef9a7bcb9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49

from chrysacase import ChrysalideTestCase
from pychrysalide.glibext import GenericWork, WorkQueue
from threading import Lock


class TestWorks(ChrysalideTestCase):
    """TestCase for glibext.*Work*"""

    def testBasicWorkQueueBehaviour(self):
        """Check the default basic behaviour of a work queue."""

        queue = WorkQueue()

        ret = queue.is_empty(123)
        self.assertTrue(ret)


    def testWorkScheduling(self):
        """Check scheduled works results."""

        class SchedulableWork(GenericWork):
            def __init__(self, lck, val):
                super(SchedulableWork, self).__init__()
                self._lck = lck
                self._val = val
            def _run(self):
                self._lck.acquire()
                self._val['integer'] += 1
                self._lck.release()

        lock = Lock()
        value = { 'integer': 0 }

        queue = WorkQueue()

        gid = queue.define_group(4)

        count = 31

        for i in range(count):

            work = SchedulableWork(lock, value)
            queue.schedule(work, gid)

        while not(queue.wait_for_completion(gid)):
            pass

        self.assertEqual(value['integer'], count)