summaryrefslogtreecommitdiff
path: root/kombu/tests/utils/test_scheduling.py
blob: ed668714b7118738f9c9cc71067b39d6733bfbd1 (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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
from __future__ import absolute_import, unicode_literals

from kombu.utils.scheduling import FairCycle, cycle_by_name

from kombu.tests.case import Case


class MyEmpty(Exception):
    pass


def consume(fun, n):
    r = []
    for i in range(n):
        r.append(fun())
    return r


class test_FairCycle(Case):

    def test_cycle(self):
        resources = ['a', 'b', 'c', 'd', 'e']

        def echo(r, timeout=None):
            return r

        # cycle should be ['a', 'b', 'c', 'd', 'e', ... repeat]
        cycle = FairCycle(echo, resources, MyEmpty)
        for i in range(len(resources)):
            self.assertEqual(cycle.get(), (resources[i],
                                           resources[i]))
        for i in range(len(resources)):
            self.assertEqual(cycle.get(), (resources[i],
                                           resources[i]))

    def test_cycle_breaks(self):
        resources = ['a', 'b', 'c', 'd', 'e']

        def echo(r):
            if r == 'c':
                raise MyEmpty(r)
            return r

        cycle = FairCycle(echo, resources, MyEmpty)
        self.assertEqual(
            consume(cycle.get, len(resources)),
            [('a', 'a'), ('b', 'b'), ('d', 'd'),
             ('e', 'e'), ('a', 'a')],
        )
        self.assertEqual(
            consume(cycle.get, len(resources)),
            [('b', 'b'), ('d', 'd'), ('e', 'e'),
             ('a', 'a'), ('b', 'b')],
        )
        cycle2 = FairCycle(echo, ['c', 'c'], MyEmpty)
        with self.assertRaises(MyEmpty):
            consume(cycle2.get, 3)

    def test_cycle_no_resources(self):
        cycle = FairCycle(None, [], MyEmpty)
        cycle.pos = 10

        with self.assertRaises(MyEmpty):
            cycle._next()

    def test__repr__(self):
        self.assertTrue(repr(FairCycle(lambda x: x, [1, 2, 3], MyEmpty)))


class test_round_robin_cycle(Case):

    def test_round_robin_cycle(self):
        it = cycle_by_name('round_robin')(['A', 'B', 'C'])
        self.assertListEqual(it.consume(3), ['A', 'B', 'C'])
        it.rotate('B')
        self.assertListEqual(it.consume(3), ['A', 'C', 'B'])
        it.rotate('A')
        self.assertListEqual(it.consume(3), ['C', 'B', 'A'])
        it.rotate('A')
        self.assertListEqual(it.consume(3), ['C', 'B', 'A'])
        it.rotate('C')
        self.assertListEqual(it.consume(3), ['B', 'A', 'C'])


class test_priority_cycle(Case):

    def test_priority_cycle(self):
        it = cycle_by_name('priority')(['A', 'B', 'C'])
        self.assertListEqual(it.consume(3), ['A', 'B', 'C'])
        it.rotate('B')
        self.assertListEqual(it.consume(3), ['A', 'B', 'C'])
        it.rotate('A')
        self.assertListEqual(it.consume(3), ['A', 'B', 'C'])
        it.rotate('A')
        self.assertListEqual(it.consume(3), ['A', 'B', 'C'])
        it.rotate('C')
        self.assertListEqual(it.consume(3), ['A', 'B', 'C'])


class test_sorted_cycle(Case):

    def test_sorted_cycle(self):
        it = cycle_by_name('sorted')(['B', 'C', 'A'])
        self.assertListEqual(it.consume(3), ['A', 'B', 'C'])
        it.rotate('B')
        self.assertListEqual(it.consume(3), ['A', 'B', 'C'])
        it.rotate('A')
        self.assertListEqual(it.consume(3), ['A', 'B', 'C'])
        it.rotate('A')
        self.assertListEqual(it.consume(3), ['A', 'B', 'C'])
        it.rotate('C')
        self.assertListEqual(it.consume(3), ['A', 'B', 'C'])