File: test_custom_queue.py

package info (click to toggle)
python-aioxmpp 0.13.3-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, sid
  • size: 6,244 kB
  • sloc: python: 97,761; xml: 215; makefile: 155; sh: 63
file content (175 lines) | stat: -rw-r--r-- 4,771 bytes parent folder | download | duplicates (2)
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
########################################################################
# File name: test_custom_queue.py
# This file is part of: aioxmpp
#
# LICENSE
#
# This program 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 3 of the
# License, or (at your option) any later version.
#
# This program 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 this program.  If not, see
# <http://www.gnu.org/licenses/>.
#
########################################################################
import asyncio
import unittest

import aioxmpp.custom_queue as custom_queue

from aioxmpp.testutils import run_coroutine


class TestAsyncDeque(unittest.TestCase):
    def setUp(self):
        self.loop = asyncio.get_event_loop()
        self.q = custom_queue.AsyncDeque(loop=self.loop)

    def test_put_get_cycle_nowait(self):
        self.q.put_nowait(1)
        self.q.put_nowait(2)
        self.q.put_nowait(3)

        self.assertEqual(
            1,
            self.q.get_nowait()
        )
        self.assertEqual(
            2,
            self.q.get_nowait()
        )
        self.assertEqual(
            3,
            self.q.get_nowait()
        )

    def test_putleft_getright_cycle_nowait(self):
        self.q.putleft_nowait(1)
        self.q.putleft_nowait(2)
        self.q.putleft_nowait(3)

        self.assertEqual(
            1,
            self.q.getright_nowait()
        )
        self.assertEqual(
            2,
            self.q.getright_nowait()
        )
        self.assertEqual(
            3,
            self.q.getright_nowait()
        )

    def test_len(self):
        self.assertEqual(0, len(self.q))
        self.q.put_nowait(1)
        self.assertEqual(1, len(self.q))
        self.q.put_nowait(1)
        self.assertEqual(2, len(self.q))
        self.q.get_nowait()
        self.assertEqual(1, len(self.q))

    def test_contains(self):
        self.assertNotIn(1, self.q)
        self.q.put_nowait(1)
        self.assertIn(1, self.q)

    def test_get(self):
        async def putter():
            await asyncio.sleep(0.001)
            self.q.put_nowait(1)

        _, v = run_coroutine(asyncio.gather(
            putter(),
            self.q.get()
        ))

        self.assertEqual(1, v)

    def test_one_producer_many_consumers(self):
        async def putter():
            for i in range(20):
                self.q.put_nowait(i)
                await asyncio.sleep(0)

        async def getter():
            result = []
            for i in range(4):
                result.append(await self.q.get())
                await asyncio.sleep(0)
            return result

        _, vs1, vs2, vs3, vs4, vs5 = run_coroutine(asyncio.gather(
            putter(),
            getter(),
            getter(),
            getter(),
            getter(),
            getter(),
        ))

        self.assertSequenceEqual(
            range(20),
            sorted(vs1 + vs2 + vs3 + vs4 + vs5)
        )

    def test_one_consumer_many_producers(self):
        async def putter(n0):
            for i in range(n0, n0 + 4):
                self.q.put_nowait(i)
                await asyncio.sleep(0)

        async def getter():
            result = []
            for i in range(20):
                result.append(await self.q.get())
                await asyncio.sleep(0)
            return result

        _, _, _, _, _, vs = run_coroutine(asyncio.gather(
            putter(0),
            putter(4),
            putter(8),
            putter(12),
            putter(16),
            getter()
        ))

        self.assertSequenceEqual(
            range(20),
            sorted(vs)
        )

    def test_empty(self):
        self.assertTrue(self.q.empty())
        self.q.put_nowait(1)
        self.assertFalse(self.q.empty())
        self.q.get_nowait()
        self.assertTrue(self.q.empty())

    def test_raise_asyncio_QueueEmpty_on_empty_get(self):
        with self.assertRaises(asyncio.QueueEmpty):
            self.q.get_nowait()

        with self.assertRaises(asyncio.QueueEmpty):
            self.q.getright_nowait()

    def test_clear(self):
        self.q.put_nowait(1)
        self.q.put_nowait(2)
        self.q.clear()
        self.assertTrue(self.q.empty())
        with self.assertRaises(asyncio.QueueEmpty):
            self.q.get_nowait()

    def tearDown(self):
        del self.q
        del self.loop