File: id_allocation_test.py

package info (click to toggle)
python-mitogen 0.3.0~rc1-4
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 3,240 kB
  • sloc: python: 19,899; sh: 91; perl: 19; ansic: 18; makefile: 13
file content (36 lines) | stat: -rw-r--r-- 922 bytes parent folder | download
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

import unittest2

import testlib

import mitogen.core
import mitogen.parent


@mitogen.core.takes_econtext
def allocate_an_id(econtext):
    mitogen.parent.upgrade_router(econtext)
    return econtext.router.allocate_id()


class SlaveTest(testlib.RouterMixin, testlib.TestCase):
    def test_slave_allocates_id(self):
        context = self.router.local()
        # Master's allocator named the context 1.
        self.assertEquals(1, context.context_id)

        # First call from slave allocates a block (2..1001)
        id_ = context.call(allocate_an_id)
        self.assertEqual(id_, 2)

        # Second call from slave allocates from block (3..1001)
        id_ = context.call(allocate_an_id)
        self.assertEqual(id_, 3)

        # Subsequent master allocation does not collide
        c2 = self.router.local()
        self.assertEquals(1002, c2.context_id)


if __name__ == '__main__':
    unittest2.main()