File: test_stress_queues.py

package info (click to toggle)
python-azure 20201208%2Bgit-6
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 1,437,920 kB
  • sloc: python: 4,287,452; javascript: 269; makefile: 198; sh: 187; xml: 106
file content (352 lines) | stat: -rw-r--r-- 17,587 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
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
#-------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for
# license information.
#--------------------------------------------------------------------------

from datetime import datetime, timedelta
import logging
import pytest
import sys
import time

from azure.servicebus import ServiceBusClient, AutoLockRenewer
from azure.servicebus._common.constants import ServiceBusReceiveMode

from devtools_testutils import AzureMgmtTestCase, CachedResourceGroupPreparer

from servicebus_preparer import ServiceBusNamespacePreparer, ServiceBusQueuePreparer
from stress_tests.stress_test_base import StressTestRunner, ReceiveType

LOGGING_ENABLE = False

class ServiceBusQueueStressTests(AzureMgmtTestCase):

    @pytest.mark.liveTest
    @pytest.mark.live_test_only
    @CachedResourceGroupPreparer(name_prefix='servicebustest')
    @ServiceBusNamespacePreparer(name_prefix='servicebustest')
    @ServiceBusQueuePreparer(name_prefix='servicebustest')
    def test_stress_queue_send_and_receive(self, servicebus_namespace_connection_string, servicebus_queue):
        sb_client = ServiceBusClient.from_connection_string(
            servicebus_namespace_connection_string, logging_enable=LOGGING_ENABLE)

        stress_test = StressTestRunner(senders = [sb_client.get_queue_sender(servicebus_queue.name)],
                                       receivers = [sb_client.get_queue_receiver(servicebus_queue.name)],
                                       duration=timedelta(seconds=60))

        result = stress_test.run()
        assert(result.total_sent > 0)
        assert(result.total_received > 0)


    @pytest.mark.liveTest
    @pytest.mark.live_test_only
    @CachedResourceGroupPreparer(name_prefix='servicebustest')
    @ServiceBusNamespacePreparer(name_prefix='servicebustest')
    @ServiceBusQueuePreparer(name_prefix='servicebustest')
    def test_stress_queue_send_and_pull_receive(self, servicebus_namespace_connection_string, servicebus_queue):
        sb_client = ServiceBusClient.from_connection_string(
            servicebus_namespace_connection_string, logging_enable=LOGGING_ENABLE)

        stress_test = StressTestRunner(senders = [sb_client.get_queue_sender(servicebus_queue.name)],
                                       receivers = [sb_client.get_queue_receiver(servicebus_queue.name)],
                                       receive_type=ReceiveType.pull,
                                       duration=timedelta(seconds=60))

        result = stress_test.run()
        assert(result.total_sent > 0)
        assert(result.total_received > 0)


    @pytest.mark.liveTest
    @pytest.mark.live_test_only
    @CachedResourceGroupPreparer(name_prefix='servicebustest')
    @ServiceBusNamespacePreparer(name_prefix='servicebustest')
    @ServiceBusQueuePreparer(name_prefix='servicebustest')
    def test_stress_queue_batch_send_and_receive(self, servicebus_namespace_connection_string, servicebus_queue):
        sb_client = ServiceBusClient.from_connection_string(
            servicebus_namespace_connection_string, logging_enable=LOGGING_ENABLE)

        stress_test = StressTestRunner(senders = [sb_client.get_queue_sender(servicebus_queue.name)],
                                       receivers = [sb_client.get_queue_receiver(servicebus_queue.name)],
                                       duration=timedelta(seconds=60),
                                       send_batch_size=5)

        result = stress_test.run()
        assert(result.total_sent > 0)
        assert(result.total_received > 0)


    @pytest.mark.liveTest
    @pytest.mark.live_test_only
    @CachedResourceGroupPreparer(name_prefix='servicebustest')
    @ServiceBusNamespacePreparer(name_prefix='servicebustest')
    @ServiceBusQueuePreparer(name_prefix='servicebustest')
    def test_stress_queue_slow_send_and_receive(self, servicebus_namespace_connection_string, servicebus_queue):
        sb_client = ServiceBusClient.from_connection_string(
            servicebus_namespace_connection_string, logging_enable=LOGGING_ENABLE)

        stress_test = StressTestRunner(senders = [sb_client.get_queue_sender(servicebus_queue.name)],
                                       receivers = [sb_client.get_queue_receiver(servicebus_queue.name)],
                                       duration=timedelta(seconds=3501*3),
                                       send_delay=3500)

        result = stress_test.run()
        assert(result.total_sent > 0)
        assert(result.total_received > 0)


    @pytest.mark.liveTest
    @pytest.mark.live_test_only
    @CachedResourceGroupPreparer(name_prefix='servicebustest')
    @ServiceBusNamespacePreparer(name_prefix='servicebustest')
    @ServiceBusQueuePreparer(name_prefix='servicebustest')
    def test_stress_queue_receive_and_delete(self, servicebus_namespace_connection_string, servicebus_queue):
        sb_client = ServiceBusClient.from_connection_string(
            servicebus_namespace_connection_string, logging_enable=LOGGING_ENABLE)

        stress_test = StressTestRunner(senders = [sb_client.get_queue_sender(servicebus_queue.name)],
                                       receivers = [sb_client.get_queue_receiver(servicebus_queue.name, receive_mode=ServiceBusReceiveMode.RECEIVE_AND_DELETE)],
                                       should_complete_messages = False,
                                       duration=timedelta(seconds=60))

        result = stress_test.run()
        assert(result.total_sent > 0)
        assert(result.total_received > 0)


    @pytest.mark.liveTest
    @pytest.mark.live_test_only
    @CachedResourceGroupPreparer(name_prefix='servicebustest')
    @ServiceBusNamespacePreparer(name_prefix='servicebustest')
    @ServiceBusQueuePreparer(name_prefix='servicebustest')
    def test_stress_queue_unsettled_messages(self, servicebus_namespace_connection_string, servicebus_queue):
        sb_client = ServiceBusClient.from_connection_string(
            servicebus_namespace_connection_string, logging_enable=LOGGING_ENABLE)

        stress_test = StressTestRunner(senders = [sb_client.get_queue_sender(servicebus_queue.name)],
                                       receivers = [sb_client.get_queue_receiver(servicebus_queue.name)],
                                       duration = timedelta(seconds=350),
                                       should_complete_messages = False)

        result = stress_test.run()
        # This test is prompted by reports of an issue where enough unsettled messages saturate a service-side cache
        # and prevent further receipt.
        assert(result.total_sent > 2500)
        assert(result.total_received > 2500)


    @pytest.mark.liveTest
    @pytest.mark.live_test_only
    @CachedResourceGroupPreparer(name_prefix='servicebustest')
    @ServiceBusNamespacePreparer(name_prefix='servicebustest')
    @ServiceBusQueuePreparer(name_prefix='servicebustest')
    def test_stress_queue_receive_large_batch_size(self, servicebus_namespace_connection_string, servicebus_queue):
        sb_client = ServiceBusClient.from_connection_string(
            servicebus_namespace_connection_string, logging_enable=LOGGING_ENABLE)

        stress_test = StressTestRunner(senders = [sb_client.get_queue_sender(servicebus_queue.name)],
                                       receivers = [sb_client.get_queue_receiver(servicebus_queue.name, prefetch_count=50)],
                                       duration = timedelta(seconds=60),
                                       max_message_count = 50)

        result = stress_test.run()
        assert(result.total_sent > 0)
        assert(result.total_received > 0)

    # Cannot be defined at local scope due to pickling into multiproc runner.
    class ReceiverTimeoutStressTestRunner(StressTestRunner):
        def on_send(self, state, sent_message, sender):
            '''Called on every successful send'''
            if state.total_sent % 10 == 0:
                # To make receive time out, in push mode this delay would trigger receiver reconnection
                time.sleep(self.max_wait_time + 5)

    @pytest.mark.liveTest
    @pytest.mark.live_test_only
    @CachedResourceGroupPreparer(name_prefix='servicebustest')
    @ServiceBusNamespacePreparer(name_prefix='servicebustest')
    @ServiceBusQueuePreparer(name_prefix='servicebustest')
    def test_stress_queue_pull_receive_timeout(self, servicebus_namespace_connection_string, servicebus_queue):
        sb_client = ServiceBusClient.from_connection_string(
            servicebus_namespace_connection_string, logging_enable=LOGGING_ENABLE)
        
        stress_test = ServiceBusQueueStressTests.ReceiverTimeoutStressTestRunner(
            senders = [sb_client.get_queue_sender(servicebus_queue.name)],
            receivers = [sb_client.get_queue_receiver(servicebus_queue.name)],
            max_wait_time = 5,
            receive_type=ReceiveType.pull,
            duration=timedelta(seconds=600))

        result = stress_test.run()
        assert(result.total_sent > 0)
        assert(result.total_received > 0)


    class LongRenewStressTestRunner(StressTestRunner):
        def on_receive(self, state, received_message, receiver):
            '''Called on every successful receive'''
            renewer = AutoLockRenewer()
            renewer.register(receiver, received_message, max_lock_renewal_duration=300)
            time.sleep(300)

    @pytest.mark.liveTest
    @pytest.mark.live_test_only
    @CachedResourceGroupPreparer(name_prefix='servicebustest')
    @ServiceBusNamespacePreparer(name_prefix='servicebustest')
    @ServiceBusQueuePreparer(name_prefix='servicebustest')
    def test_stress_queue_long_renew_send_and_receive(self, servicebus_namespace_connection_string, servicebus_queue):
        sb_client = ServiceBusClient.from_connection_string(
            servicebus_namespace_connection_string, debug=False)

        stress_test = ServiceBusQueueStressTests.LongRenewStressTestRunner(
                                       senders = [sb_client.get_queue_sender(servicebus_queue.name)],
                                       receivers = [sb_client.get_queue_receiver(servicebus_queue.name)],
                                       duration=timedelta(seconds=3000),
                                       send_delay=300)

        result = stress_test.run()
        assert(result.total_sent > 0)
        assert(result.total_received > 0)


    class LongSessionRenewStressTestRunner(StressTestRunner):
        def on_receive(self, state, received_message, receiver):
            '''Called on every successful receive'''
            renewer = AutoLockRenewer()
            def on_fail(renewable, error):
                print("FAILED AUTOLOCKRENEW: " + str(error))
            renewer.register(receiver, receiver.session, max_lock_renewal_duration=600, on_lock_renew_failure=on_fail)

    @pytest.mark.liveTest
    @pytest.mark.live_test_only
    @CachedResourceGroupPreparer(name_prefix='servicebustest')
    @ServiceBusNamespacePreparer(name_prefix='servicebustest')
    @ServiceBusQueuePreparer(name_prefix='servicebustest', requires_session=True)
    def test_stress_queue_long_renew_session_send_and_receive(self, servicebus_namespace_connection_string, servicebus_queue):
        sb_client = ServiceBusClient.from_connection_string(
            servicebus_namespace_connection_string, debug=False)

        session_id = 'test_stress_queue_long_renew_send_and_receive'

        stress_test = ServiceBusQueueStressTests.LongSessionRenewStressTestRunner(
                                       senders = [sb_client.get_queue_sender(servicebus_queue.name)],
                                       receivers = [sb_client.get_queue_receiver(servicebus_queue.name, session_id=session_id)],
                                       duration=timedelta(seconds=3000),
                                       send_delay=300,
                                       send_session_id=session_id)

        result = stress_test.run()
        assert(result.total_sent > 0)
        assert(result.total_received > 0)


    class Peekon_receiveStressTestRunner(StressTestRunner):
        def on_receive_batch(self, state, received_message, receiver):
            '''Called on every successful receive'''
            assert receiver.peek_messages()[0]

    @pytest.mark.liveTest
    @pytest.mark.live_test_only
    @CachedResourceGroupPreparer(name_prefix='servicebustest')
    @ServiceBusNamespacePreparer(name_prefix='servicebustest')
    @ServiceBusQueuePreparer(name_prefix='servicebustest')
    def test_stress_queue_peek_messages(self, servicebus_namespace_connection_string, servicebus_queue):
        sb_client = ServiceBusClient.from_connection_string(
            servicebus_namespace_connection_string, debug=False)

        stress_test = ServiceBusQueueStressTests.Peekon_receiveStressTestRunner(
                                       senders = [sb_client.get_queue_sender(servicebus_queue.name)],
                                       receivers = [sb_client.get_queue_receiver(servicebus_queue.name)],
                                       duration = timedelta(seconds=300),
                                       receive_delay = 30,
                                       receive_type = ReceiveType.none)

        result = stress_test.run()
        assert(result.total_sent > 0)
        # TODO: This merits better validation, to be implemented alongside full metric spread.


    class RestartHandlerStressTestRunner(StressTestRunner):
        def post_receive(self, state, receiver):
            '''Called after completion of every successful receive'''
            if state.total_received % 3 == 0:
                receiver.__exit__()
                receiver.__enter__()

        def on_send(self, state, sent_message, sender):
            '''Called after completion of every successful receive'''
            if state.total_sent % 3 == 0:
                sender.__exit__()
                sender.__enter__()

    @pytest.mark.liveTest
    @pytest.mark.live_test_only
    @pytest.mark.skip(reason='This test is disabled unless re-openability of handlers is desired and re-enabled')
    @CachedResourceGroupPreparer(name_prefix='servicebustest')
    @ServiceBusNamespacePreparer(name_prefix='servicebustest')
    @ServiceBusQueuePreparer(name_prefix='servicebustest')
    def test_stress_queue_close_and_reopen(self, servicebus_namespace_connection_string, servicebus_queue):
        sb_client = ServiceBusClient.from_connection_string(
            servicebus_namespace_connection_string, debug=False)

        stress_test = ServiceBusQueueStressTests.RestartHandlerStressTestRunner(
                                       senders = [sb_client.get_queue_sender(servicebus_queue.name)],
                                       receivers = [sb_client.get_queue_receiver(servicebus_queue.name)],
                                       duration = timedelta(seconds=300),
                                       receive_delay = 30,
                                       send_delay = 10)

        result = stress_test.run()
        assert(result.total_sent > 0)
        assert(result.total_received > 0)

    # This test validates that all individual messages are received contiguously over a long time period.
    # (e.g. not dropped for whatever reason, not sent, or not received)
    class DroppedMessageCheckerStressTestRunner(StressTestRunner):
        def on_receive(self, state, received_message, receiver):
            '''Called on every successful receive'''
            last_seen = getattr(state, 'last_seen', -1)
            noncontiguous = getattr(state, 'noncontiguous', set())
            body = int(str(received_message))
            if body == last_seen+1:
                last_seen += 1
                if noncontiguous:
                    while (last_seen+1) in noncontiguous:
                        last_seen += 1
                        noncontiguous.remove(last_seen)
            else:
                noncontiguous.add(body)
            state.noncontiguous = noncontiguous
            state.last_seen = last_seen

        def pre_process_message_body(self, payload):
            '''Called when constructing message body'''
            try:
                body = self._message_id
            except:
                self._message_id = 0
                body = 0
            self._message_id += 1

            return str(body)

    @pytest.mark.liveTest
    @pytest.mark.live_test_only
    @CachedResourceGroupPreparer(name_prefix='servicebustest')
    @ServiceBusNamespacePreparer(name_prefix='servicebustest')
    @ServiceBusQueuePreparer(name_prefix='servicebustest')
    def test_stress_queue_check_for_dropped_messages(self, servicebus_namespace_connection_string, servicebus_queue):
        sb_client = ServiceBusClient.from_connection_string(
            servicebus_namespace_connection_string, debug=False)

        stress_test = ServiceBusQueueStressTests.DroppedMessageCheckerStressTestRunner(
                                       senders = [sb_client.get_queue_sender(servicebus_queue.name)],
                                       receivers = [sb_client.get_queue_receiver(servicebus_queue.name)],
                                       receive_type=ReceiveType.pull,
                                       duration=timedelta(seconds=3000))

        result = stress_test.run()
        assert(result.total_sent > 0)
        assert(result.total_received > 0)