File: test_stress_queues.py

package info (click to toggle)
python-azure 20230112%2Bgit-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 749,544 kB
  • sloc: python: 6,815,827; javascript: 287; makefile: 195; xml: 109; sh: 105
file content (332 lines) | stat: -rw-r--r-- 15,371 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
#-------------------------------------------------------------------------
# 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 timedelta
import time
import os
import pytest
from dotenv import load_dotenv
#from argparse import ArgumentParser

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

from stress_test_base import StressTestRunner, ReceiveType

ENV_FILE = os.environ.get('ENV_FILE')
load_dotenv(dotenv_path=ENV_FILE, override=True)

LOGGING_ENABLE = False
SERVICE_BUS_CONNECTION_STR = os.environ.get('SERVICE_BUS_CONNECTION_STR')
SERVICEBUS_QUEUE_NAME = os.environ.get('SERVICE_BUS_QUEUE_NAME')

@pytest.mark.liveTest
@pytest.mark.live_test_only
def test_stress_queue_send_and_receive():
    sb_client = ServiceBusClient.from_connection_string(
        SERVICE_BUS_CONNECTION_STR, 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),
                                    azure_monitor_metric=AzureMonitorMetric("test_stress_queue_send_and_receive")
                                    )

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


@pytest.mark.liveTest
@pytest.mark.live_test_only
def test_stress_queue_send_and_pull_receive():
    sb_client = ServiceBusClient.from_connection_string(
        SERVICE_BUS_CONNECTION_STR, 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),
                                    azure_monitor_metric=AzureMonitorMetric("test_stress_queue_send_and_pull_receive")
                                    )

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


@pytest.mark.liveTest
@pytest.mark.live_test_only
def test_stress_queue_batch_send_and_receive():
    sb_client = ServiceBusClient.from_connection_string(
        SERVICE_BUS_CONNECTION_STR, 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,
                                    azure_monitor_metric=AzureMonitorMetric("test_stress_queue_batch_send_and_receive")
                                    )

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


@pytest.mark.liveTest
@pytest.mark.live_test_only
def test_stress_queue_slow_send_and_receive():
    sb_client = ServiceBusClient.from_connection_string(
        SERVICE_BUS_CONNECTION_STR, 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,
                                    azure_monitor_metric=AzureMonitorMetric("test_stress_queue_slow_send_and_receive")
                                    )

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


@pytest.mark.liveTest
@pytest.mark.live_test_only
def test_stress_queue_receive_and_delete():
    sb_client = ServiceBusClient.from_connection_string(
        SERVICE_BUS_CONNECTION_STR, 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),
                                    azure_monitor_metric=AzureMonitorMetric("test_stress_queue_slow_send_and_receive")
                                    )

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


@pytest.mark.liveTest
@pytest.mark.live_test_only
def test_stress_queue_unsettled_messages():
    sb_client = ServiceBusClient.from_connection_string(
        SERVICE_BUS_CONNECTION_STR, 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,
                                    azure_monitor_metric=AzureMonitorMetric("test_stress_queue_unsettled_messages")
                                    )

    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
def test_stress_queue_receive_large_batch_size():
    sb_client = ServiceBusClient.from_connection_string(
        SERVICE_BUS_CONNECTION_STR, 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,
                                    azure_monitor_metric=AzureMonitorMetric("test_stress_queue_receive_large_batch_size")
                                    )

    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
def test_stress_queue_pull_receive_timeout():
    sb_client = ServiceBusClient.from_connection_string(
        SERVICE_BUS_CONNECTION_STR, logging_enable=LOGGING_ENABLE)
    stress_test = 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),
        azure_monitor_metric=AzureMonitorMetric("test_stress_queue_pull_receive_timeout")
        )

    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
def test_stress_queue_long_renew_send_and_receive():
    sb_client = ServiceBusClient.from_connection_string(
        SERVICE_BUS_CONNECTION_STR, logging_enable=LOGGING_ENABLE)
    stress_test = 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,
                                    azure_monitor_metric=AzureMonitorMetric("test_stress_queue_long_renew_send_and_receive")
                                    )

    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
def test_stress_queue_long_renew_session_send_and_receive():
    sb_client = ServiceBusClient.from_connection_string(
        SERVICE_BUS_CONNECTION_STR, logging_enable=LOGGING_ENABLE)
    session_id = 'test_stress_queue_long_renew_send_and_receive'

    stress_test = 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,
                                    azure_monitor_metric=AzureMonitorMetric("test_stress_queue_long_renew_session_send_and_receive")
                                    )

    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
def test_stress_queue_peek_messages():
    sb_client = ServiceBusClient.from_connection_string(
        SERVICE_BUS_CONNECTION_STR, logging_enable=LOGGING_ENABLE)
    stress_test = 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,
                                    azure_monitor_metric=AzureMonitorMetric("test_stress_queue_peek_messages")
                                    )

    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')
def test_stress_queue_close_and_reopen():
    sb_client = ServiceBusClient.from_connection_string(
        SERVICE_BUS_CONNECTION_STR, logging_enable=LOGGING_ENABLE)
    stress_test = 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,
                                    azure_monitor_metric=AzureMonitorMetric("test_stress_queue_close_and_reopen")
                                    )

    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:
            _message_id = 0
            body = 0
        _message_id += 1

        return str(body)

@pytest.mark.liveTest
@pytest.mark.live_test_only
def test_stress_queue_check_for_dropped_messages():
    sb_client = ServiceBusClient.from_connection_string(
        SERVICE_BUS_CONNECTION_STR, logging_enable=LOGGING_ENABLE)
    stress_test = 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),
                                    azure_monitor_metric=AzureMonitorMetric("test_stress_queue_check_for_dropped_messages")
                                    )

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

if __name__ == '__main__':
    #parser = ArgumentParser()
    pytest.main()