forked from Azure/azure-event-hubs-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconftest.py
227 lines (183 loc) · 7.02 KB
/
conftest.py
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
#-------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for
# license information.
#--------------------------------------------------------------------------
import os
import pytest
import logging
import sys
from tests import get_logger
from azure import eventhub
from azure.eventhub import EventHubClient, Receiver, Offset
from azure.eventprocessorhost import EventProcessorHost
from azure.eventprocessorhost import EventHubPartitionPump
from azure.eventprocessorhost import AzureStorageCheckpointLeaseManager
from azure.eventprocessorhost import AzureBlobLease
from azure.eventprocessorhost import EventHubConfig
from azure.eventprocessorhost.lease import Lease
from azure.eventprocessorhost.partition_pump import PartitionPump
from azure.eventprocessorhost.partition_manager import PartitionManager
from azure.eventprocessorhost.abstract_event_processor import AbstractEventProcessor
log = get_logger(None, logging.DEBUG)
@pytest.fixture()
def live_eventhub_config():
try:
config = {}
config['hostname'] = os.environ['EVENT_HUB_HOSTNAME']
config['event_hub'] = os.environ['EVENT_HUB_NAME']
config['key_name'] = os.environ['EVENT_HUB_SAS_POLICY']
config['access_key'] = os.environ['EVENT_HUB_SAS_KEY']
config['consumer_group'] = "$Default"
config['partition'] = "0"
except KeyError:
pytest.skip("Live EventHub configuration not found.")
else:
return config
@pytest.fixture()
def connection_str():
try:
return os.environ['EVENT_HUB_CONNECTION_STR']
except KeyError:
pytest.skip("No EventHub connection string found.")
@pytest.fixture()
def invalid_hostname():
try:
conn_str = os.environ['EVENT_HUB_CONNECTION_STR']
return conn_str.replace("Endpoint=sb://", "Endpoint=sb://invalid.")
except KeyError:
pytest.skip("No EventHub connection string found.")
@pytest.fixture()
def invalid_key():
try:
conn_str = os.environ['EVENT_HUB_CONNECTION_STR']
return conn_str.replace("SharedAccessKey=", "SharedAccessKey=invalid")
except KeyError:
pytest.skip("No EventHub connection string found.")
@pytest.fixture()
def invalid_policy():
try:
conn_str = os.environ['EVENT_HUB_CONNECTION_STR']
return conn_str.replace("SharedAccessKeyName=", "SharedAccessKeyName=invalid")
except KeyError:
pytest.skip("No EventHub connection string found.")
@pytest.fixture()
def iot_connection_str():
try:
return os.environ['IOT_HUB_CONNECTION_STR']
except KeyError:
pytest.skip("No IotHub connection string found.")
@pytest.fixture()
def device_id():
try:
return os.environ['IOTHUB_DEVICE']
except KeyError:
pytest.skip("No Iothub device ID found.")
@pytest.fixture()
def receivers(connection_str):
client = EventHubClient.from_connection_string(connection_str, debug=True)
eh_hub_info = client.get_eventhub_info()
partitions = eh_hub_info["partition_ids"]
recv_offset = Offset("@latest")
receivers = []
for p in partitions:
receivers.append(client.add_receiver("$default", p, prefetch=500, offset=Offset("@latest")))
client.run()
for r in receivers:
r.receive(timeout=1)
yield receivers
client.stop()
@pytest.fixture()
def senders(connection_str):
client = EventHubClient.from_connection_string(connection_str, debug=False)
eh_hub_info = client.get_eventhub_info()
partitions = eh_hub_info["partition_ids"]
senders = []
for p in partitions:
senders.append(client.add_sender(partition=p))
client.run()
yield senders
client.stop()
@pytest.fixture()
def storage_clm():
try:
storage_clm = AzureStorageCheckpointLeaseManager(
os.environ['AZURE_STORAGE_ACCOUNT'],
os.environ['AZURE_STORAGE_ACCESS_KEY'],
"lease")
return storage_clm
except KeyError:
pytest.skip("Live Storage configuration not found.")
@pytest.fixture()
def eph():
try:
storage_clm = AzureStorageCheckpointLeaseManager(
os.environ['AZURE_STORAGE_ACCOUNT'],
os.environ['AZURE_STORAGE_ACCESS_KEY'],
"lease")
NAMESPACE = os.environ.get('EVENT_HUB_NAMESPACE')
EVENTHUB = os.environ.get('EVENT_HUB_NAME')
USER = os.environ.get('EVENT_HUB_SAS_POLICY')
KEY = os.environ.get('EVENT_HUB_SAS_KEY')
eh_config = EventHubConfig(NAMESPACE, EVENTHUB, USER, KEY, consumer_group="$default")
host = EventProcessorHost(
MockEventProcessor,
eh_config,
storage_clm)
except KeyError:
pytest.skip("Live EventHub configuration not found.")
return host
@pytest.fixture()
def eh_partition_pump(eph):
lease = AzureBlobLease()
lease.with_partition_id("1")
partition_pump = EventHubPartitionPump(eph, lease)
return partition_pump
@pytest.fixture()
def partition_pump(eph):
lease = Lease()
lease.with_partition_id("1")
partition_pump = PartitionPump(eph, lease)
return partition_pump
@pytest.fixture()
def partition_manager(eph):
partition_manager = PartitionManager(eph)
return partition_manager
class MockEventProcessor(AbstractEventProcessor):
"""
Mock Implmentation of AbstractEventProcessor for testing
"""
def __init__(self, params=None):
"""
Init Event processor
"""
self.params = params
self._msg_counter = 0
async def open_async(self, context):
"""
Called by processor host to initialize the event processor.
"""
logging.info("Connection established {}".format(context.partition_id))
async def close_async(self, context, reason):
"""
Called by processor host to indicate that the event processor is being stopped.
(Params) Context:Information about the partition
"""
logging.info("Connection closed (reason {}, id {}, offset {}, sq_number {})".format(
reason, context.partition_id, context.offset, context.sequence_number))
async def process_events_async(self, context, messages):
"""
Called by the processor host when a batch of events has arrived.
This is where the real work of the event processor is done.
(Params) Context: Information about the partition, Messages: The events to be processed.
"""
logging.info("Events processed {} {}".format(context.partition_id, messages))
await context.checkpoint_async()
async def process_error_async(self, context, error):
"""
Called when the underlying client experiences an error while receiving.
EventProcessorHost will take care of recovering from the error and
continuing to pump messages,so no action is required from
(Params) Context: Information about the partition, Error: The error that occured.
"""
logging.error("Event Processor Error {!r}".format(error))