[go: up one dir, main page]

File: test_condition.py

package info (click to toggle)
toro 1.0.1-3
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 324 kB
  • sloc: python: 2,058; makefile: 128
file content (136 lines) | stat: -rw-r--r-- 4,211 bytes parent folder | download | duplicates (4)
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
"""
Test toro.Condition.
"""

from datetime import timedelta
import time

from tornado import gen
from tornado.testing import gen_test, AsyncTestCase


import toro
from test import make_callback, assert_raises


class TestCondition(AsyncTestCase):
    def test_str(self):
        c = toro.Condition()
        self.assertTrue('Condition' in str(c))
        self.assertFalse('waiters' in str(c))
        c.wait()
        self.assertTrue('waiters' in str(c))

    @gen_test
    def test_notify(self):
        c = toro.Condition(self.io_loop)
        self.io_loop.add_timeout(time.time() + 0.1, c.notify)
        yield c.wait()

    def test_notify_1(self):
        c = toro.Condition()
        history = []
        c.wait().add_done_callback(make_callback('wait1', history))
        c.wait().add_done_callback(make_callback('wait2', history))
        c.notify(1)
        history.append('notify1')
        c.notify(1)
        history.append('notify2')
        self.assertEqual(['wait1', 'notify1', 'wait2', 'notify2'], history)

    def test_notify_n(self):
        c = toro.Condition()
        history = []
        for i in range(6):
            c.wait().add_done_callback(make_callback(i, history))

        c.notify(3)

        # Callbacks execute in the order they were registered
        self.assertEqual(list(range(3)), history)
        c.notify(1)
        self.assertEqual(list(range(4)), history)
        c.notify(2)
        self.assertEqual(list(range(6)), history)

    def test_notify_all(self):
        c = toro.Condition()
        history = []
        for i in range(4):
            c.wait().add_done_callback(make_callback(i, history))

        c.notify_all()
        history.append('notify_all')

        # Callbacks execute in the order they were registered
        self.assertEqual(
            list(range(4)) + ['notify_all'],
            history)

    @gen_test
    def test_wait_timeout(self):
        c = toro.Condition(self.io_loop)
        st = time.time()
        with assert_raises(toro.Timeout):
            yield c.wait(deadline=timedelta(seconds=0.1))

        duration = time.time() - st
        self.assertAlmostEqual(0.1, duration, places=1)

    @gen_test
    def test_wait_timeout_preempted(self):
        c = toro.Condition(self.io_loop)
        st = time.time()

        # This fires before the wait times out
        self.io_loop.add_timeout(st + .1, c.notify)
        yield c.wait(deadline=timedelta(seconds=0.2))
        duration = time.time() - st

        # Verify we were awakened by c.notify(), not by timeout
        self.assertAlmostEqual(0.1, duration, places=1)

    @gen_test
    def test_notify_n_with_timeout(self):
        # Register callbacks 0, 1, 2, and 3. Callback 1 has a timeout.
        # Wait for that timeout to expire, then do notify(2) and make
        # sure everyone runs. Verifies that a timed-out callback does
        # not count against the 'n' argument to notify().
        c = toro.Condition(self.io_loop)
        st = time.time()
        history = []

        c.wait().add_done_callback(make_callback(0, history))
        c.wait(deadline=timedelta(seconds=.1)).add_done_callback(
            make_callback(1, history))

        c.wait().add_done_callback(make_callback(2, history))
        c.wait().add_done_callback(make_callback(3, history))

        # Wait for callback 1 to time out
        yield gen.Task(self.io_loop.add_timeout, st + 0.2)
        self.assertEqual(['Timeout'], history)

        c.notify(2)
        self.assertEqual(['Timeout', 0, 2], history)
        c.notify()
        self.assertEqual(['Timeout', 0, 2, 3], history)

    @gen_test
    def test_notify_all_with_timeout(self):
        c = toro.Condition(self.io_loop)
        st = time.time()
        history = []

        c.wait().add_done_callback(make_callback(0, history))
        c.wait(deadline=timedelta(seconds=.1)).add_done_callback(
            make_callback(1, history))

        c.wait().add_done_callback(make_callback(2, history))

        # Wait for callback 1 to time out
        yield gen.Task(self.io_loop.add_timeout, st + 0.2)
        self.assertEqual(['Timeout'], history)

        c.notify_all()
        self.assertEqual(['Timeout', 0, 2], history)