forked from Azure/azure-sdk-for-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconsumer_client_test.cpp
More file actions
424 lines (370 loc) · 15.9 KB
/
consumer_client_test.cpp
File metadata and controls
424 lines (370 loc) · 15.9 KB
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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
// cspell: words hehe
#include "eventhubs_admin_client.hpp"
#include "eventhubs_test_base.hpp"
#include <azure/core/context.hpp>
#include <azure/identity.hpp>
#include <azure/messaging/eventhubs.hpp>
#include <gtest/gtest.h>
namespace LocalTest {
int i = 0;
void ProcessMessageSuccess(Azure::Core::Amqp::Models::AmqpMessage const& message)
{
(void)message;
GTEST_LOG_(INFO) << "Message Id: " << i++ << std::endl;
}
} // namespace LocalTest
namespace Azure { namespace Messaging { namespace EventHubs { namespace Test {
class ConsumerClientTest : public EventHubsTestBaseParameterized {
void SetUp() override
{
EventHubsTestBaseParameterized::SetUp();
if (m_testContext.IsLiveMode())
{
std::unique_ptr<ProducerClient> producer = CreateProducerClient();
EventDataBatchOptions eventBatchOptions;
eventBatchOptions.PartitionId = "1";
EventDataBatch batch{producer->CreateBatch(eventBatchOptions)};
EXPECT_TRUE(batch.TryAdd(Models::EventData{"Test"}));
EXPECT_NO_THROW(producer->Send(batch));
}
}
protected:
std::string GetEventHubName()
{
if (GetParam() == AuthType::Emulator)
{
return "eh1";
}
return GetEnv("EVENTHUB_NAME");
}
};
TEST_P(ConsumerClientTest, ConnectionStringNoEntityPath_LIVEONLY_)
{
if (GetParam() == AuthType::ConnectionString)
{
std::string const connStringNoEntityPath = GetEnv("EVENTHUB_CONNECTION_STRING");
std::string consumerGroup = GetEnv("EVENTHUB_CONSUMER_GROUP");
std::string eventHubName = GetEnv("EVENTHUB_NAME");
Azure::Messaging::EventHubs::ConsumerClient client(
connStringNoEntityPath, eventHubName, consumerGroup);
EXPECT_EQ(eventHubName, client.GetEventHubName());
}
}
TEST_P(ConsumerClientTest, ConnectionStringEntityPath_LIVEONLY_)
{
if (GetParam() == AuthType::ConnectionString)
{
std::string const connStringWithEntityPath
= GetEnv("EVENTHUB_CONNECTION_STRING") + ";EntityPath=hehe";
std::string consumerGroup = GetEnv("EVENTHUB_CONSUMER_GROUP");
std::string eventHubName = GetEnv("EVENTHUB_NAME");
// The eventHubName parameter must match the name in the connection string.because the
// eventhub name is in the connection string.
EXPECT_ANY_THROW(Azure::Messaging::EventHubs::ConsumerClient client(
connStringWithEntityPath, eventHubName, "$DefaultZ"));
}
}
TEST_P(ConsumerClientTest, ConnectionStringEntityPathNoConsumerGroup_LIVEONLY_)
{
if (GetParam() == AuthType::ConnectionString)
{
std::string const connStringNoEntityPath = GetEnv("EVENTHUB_CONNECTION_STRING");
std::string eventHubName = GetEnv("EVENTHUB_NAME");
Azure::Messaging::EventHubs::ConsumerClient client(connStringNoEntityPath, eventHubName);
EXPECT_EQ(eventHubName, client.GetEventHubName());
EXPECT_EQ("$Default", client.GetConsumerGroup());
}
}
TEST_P(ConsumerClientTest, ConnectionStringEntityPathNoConsumerGroupNoEventHub_LIVEONLY_)
{
if (GetParam() == AuthType::ConnectionString)
{
std::string const connStringNoEntityPath
= GetEnv("EVENTHUB_CONNECTION_STRING") + ";EntityPath=hehe";
Azure::Messaging::EventHubs::ConsumerClient client(connStringNoEntityPath);
EXPECT_EQ("hehe", client.GetEventHubName());
EXPECT_EQ("$Default", client.GetConsumerGroup());
}
}
TEST_P(ConsumerClientTest, ConnectToPartition_LIVEONLY_)
{
Azure::Messaging::EventHubs::ConsumerClientOptions options;
options.ApplicationID
= std::string(testing::UnitTest::GetInstance()->current_test_info()->name())
+ " Application";
options.Name = testing::UnitTest::GetInstance()->current_test_case()->name();
auto client = CreateConsumerClient("", options);
Azure::Messaging::EventHubs::PartitionClientOptions partitionOptions;
partitionOptions.StartPosition.Inclusive = true;
// We want to consume all messages from the earliest.
partitionOptions.StartPosition.Earliest = true;
Azure::Messaging::EventHubs::PartitionClient partitionClient
= client->CreatePartitionClient("1", partitionOptions);
auto events = partitionClient.ReceiveEvents(1);
EXPECT_EQ(events.size(), 1ul);
GTEST_LOG_(INFO) << "Received message " << events[0]->GetRawAmqpMessage();
EXPECT_TRUE(events[0]->EnqueuedTime.HasValue());
EXPECT_TRUE(events[0]->SequenceNumber.HasValue());
EXPECT_TRUE(events[0]->Offset.HasValue());
}
TEST_P(ConsumerClientTest, GetEventHubProperties_LIVEONLY_)
{
std::string eventHubName{GetEventHubName()};
Azure::Messaging::EventHubs::ConsumerClientOptions options;
options.ApplicationID = testing::UnitTest::GetInstance()->current_test_info()->name();
options.Name = testing::UnitTest::GetInstance()->current_test_case()->name();
auto client = CreateConsumerClient("", options);
Azure::Messaging::EventHubs::PartitionClientOptions partitionOptions;
partitionOptions.StartPosition.Inclusive = true;
Azure::Messaging::EventHubs::PartitionClient partitionClient
= client->CreatePartitionClient("0", partitionOptions);
Azure::Messaging::EventHubs::Models::EventHubProperties result;
ASSERT_NO_THROW(result = client->GetEventHubProperties());
EXPECT_EQ(result.Name, eventHubName);
EXPECT_TRUE(result.PartitionIds.size() > 0);
}
TEST_P(ConsumerClientTest, GetPartitionProperties_LIVEONLY_)
{
std::string eventHubName{GetEventHubName()};
Azure::Messaging::EventHubs::ConsumerClientOptions options;
options.ApplicationID = testing::UnitTest::GetInstance()->current_test_info()->name();
options.Name = testing::UnitTest::GetInstance()->current_test_case()->name();
auto client = CreateConsumerClient("", options);
Azure::Messaging::EventHubs::PartitionClientOptions partitionOptions;
partitionOptions.StartPosition.Inclusive = true;
Azure::Messaging::EventHubs::PartitionClient partitionClient
= client->CreatePartitionClient("0", partitionOptions);
auto result = client->GetPartitionProperties("0");
EXPECT_EQ(result.Name, eventHubName);
EXPECT_EQ(result.PartitionId, "0");
}
TEST_P(ConsumerClientTest, GetPartitionPropertiesAuthError_LIVEONLY_)
{
auto credentials{
std::make_shared<Azure::Identity::ClientSecretCredential>("abc", "def", "ghi")};
std::string eventHubName{GetEnv("EVENTHUB_NAME")};
std::string hostName{GetEnv("EVENTHUBS_HOST")};
std::string consumerGroup{GetEnv("EVENTHUB_CONSUMER_GROUP")};
Azure::Messaging::EventHubs::ConsumerClientOptions options;
options.ApplicationID = testing::UnitTest::GetInstance()->current_test_info()->name();
options.Name = testing::UnitTest::GetInstance()->current_test_case()->name();
Azure::Messaging::EventHubs::ConsumerClient client(
hostName, eventHubName, credentials, consumerGroup);
Azure::Messaging::EventHubs::PartitionClientOptions partitionOptions;
partitionOptions.StartPosition.Inclusive = true;
EXPECT_THROW(
client.CreatePartitionClient("0", partitionOptions),
Azure::Core::Credentials::AuthenticationException);
}
TEST_P(ConsumerClientTest, GetEventHubProperties_Multithreaded_LIVEONLY_)
{
std::string eventHubName{GetEventHubName()};
Azure::Messaging::EventHubs::ConsumerClientOptions options;
options.ApplicationID = testing::UnitTest::GetInstance()->current_test_info()->name();
options.Name = testing::UnitTest::GetInstance()->current_test_case()->name();
auto client = CreateConsumerClient();
Azure::Messaging::EventHubs::PartitionClientOptions partitionOptions;
partitionOptions.StartPosition.Inclusive = true;
std::vector<std::thread> threads;
std::vector<size_t> iterationsPerThread;
for (int i = 0; i < 20; i++)
{
threads.emplace_back([&client, eventHubName, &iterationsPerThread]() {
size_t iterations = 0;
std::chrono::system_clock::duration timeout = std::chrono::seconds(3);
std::chrono::system_clock::time_point start = std::chrono::system_clock::now();
while ((std::chrono::system_clock::now() - start) <= timeout)
{
Azure::Messaging::EventHubs::Models::EventHubProperties result;
ASSERT_NO_THROW(result = client->GetEventHubProperties());
EXPECT_EQ(result.Name, eventHubName);
EXPECT_TRUE(result.PartitionIds.size() > 0);
std::this_thread::yield();
iterations++;
}
iterationsPerThread.push_back(iterations);
});
}
GTEST_LOG_(INFO) << "Waiting for threads to finish.";
for (auto& t : threads)
{
if (t.joinable())
{
t.join();
}
}
GTEST_LOG_(INFO) << "Threads finished.";
for (const auto i : iterationsPerThread)
{
GTEST_LOG_(INFO) << "Thread iterations: " << i;
}
}
TEST_P(ConsumerClientTest, GetPartitionProperties_Multithreaded)
{
std::string eventHubName{GetEventHubName()};
Azure::Messaging::EventHubs::ConsumerClientOptions options;
options.ApplicationID = testing::UnitTest::GetInstance()->current_test_info()->name();
options.Name = testing::UnitTest::GetInstance()->current_test_case()->name();
auto client = CreateConsumerClient();
auto ehProperties = client->GetEventHubProperties();
std::vector<std::thread> threads;
std::vector<size_t> iterationsPerThread;
for (const auto& partition : ehProperties.PartitionIds)
{
threads.emplace_back(std::thread([&client, partition, eventHubName, &iterationsPerThread]() {
GTEST_LOG_(INFO) << "Thread started for partition: " << partition << ".\n";
for (int i = 0; i < 20; i++)
{
std::vector<std::thread> partitionThreads;
partitionThreads.emplace_back(
[&client, &partition, eventHubName, &iterationsPerThread]() {
size_t iterations = 0;
std::chrono::system_clock::duration timeout = std::chrono::seconds(3);
std::chrono::system_clock::time_point start = std::chrono::system_clock::now();
while ((std::chrono::system_clock::now() - start) <= timeout)
{
Azure::Messaging::EventHubs::Models::EventHubPartitionProperties result;
ASSERT_NO_THROW(result = client->GetPartitionProperties(partition));
EXPECT_EQ(result.Name, eventHubName);
EXPECT_EQ(result.PartitionId, partition);
std::this_thread::yield();
iterations++;
}
iterationsPerThread.push_back(iterations);
});
for (auto& t : partitionThreads)
{
if (t.joinable())
{
t.join();
}
}
}
GTEST_LOG_(INFO) << "Thread finished for partition: " << partition << ".\n";
}));
}
GTEST_LOG_(INFO) << "Waiting for threads to finish.";
for (auto& t : threads)
{
if (t.joinable())
{
t.join();
}
}
GTEST_LOG_(INFO) << iterationsPerThread.size() << " threads finished.";
}
std::string GetRandomName(const char* baseName = "checkpoint")
{
std::string name = baseName;
name.append(Azure::Core::Uuid::CreateUuid().ToString());
return name;
}
TEST_P(ConsumerClientTest, RetrieveMultipleEvents)
{
// This test depends on being able to create a new eventhub instance, so skip it on the
// emulator.
if (GetParam() == AuthType::Emulator)
{
GTEST_SKIP();
}
// Disabled test for now.
#if 0
EventHubsManagement administrationClient;
auto eventhubNamespace{administrationClient.GetNamespace(GetEnv("EVENTHUBS_NAMESPACE"))};
std::string eventHubName{GetRandomName("eventhub")};
auto eventHub{eventhubNamespace.CreateEventHub(eventHubName)};
eventHub.CreateConsumerGroup(GetEnv("EVENTHUB_CONSUMER_GROUP"));
// Populate the eventhub instance with 50 messages.
constexpr size_t numberOfEvents = 50;
GTEST_LOG_(INFO) << "Populate eventhubs instance.";
{
Azure::Messaging::EventHubs::ProducerClientOptions producerOptions;
producerOptions.ApplicationID = testing::UnitTest::GetInstance()->current_test_info()->name();
producerOptions.Name = testing::UnitTest::GetInstance()->current_test_info()->name();
auto producer{CreateProducerClient(eventHubName)};
EventDataBatchOptions eventBatchOptions;
eventBatchOptions.PartitionId = "0";
EventDataBatch batch{producer->CreateBatch(eventBatchOptions)};
for (size_t i = 0; i < numberOfEvents; ++i)
{
EXPECT_TRUE(batch.TryAdd(Models::EventData{"Test"}));
}
EXPECT_NO_THROW(producer->Send(batch));
}
// Now receive the messages - it should take almost no time because they should have been
// queued up asynchronously.
GTEST_LOG_(INFO) << "Receive events from instance.";
{
Azure::Messaging::EventHubs::ConsumerClientOptions options;
options.ApplicationID = testing::UnitTest::GetInstance()->current_test_info()->name();
options.Name = testing::UnitTest::GetInstance()->current_test_case()->name();
auto client{CreateConsumerClient(eventHubName)};
Azure::Messaging::EventHubs::PartitionClientOptions partitionOptions;
partitionOptions.StartPosition.Earliest = true;
partitionOptions.StartPosition.Inclusive = true;
Azure::Messaging::EventHubs::PartitionClient partitionClient
= client->CreatePartitionClient("0", partitionOptions);
// Sleep for a bit for the messages to be received.
GTEST_LOG_(INFO) << "Sleep until messages received.";
std::this_thread::sleep_for(std::chrono::seconds(2));
size_t totalReceived{0};
{
std::chrono::system_clock::time_point start = std::chrono::system_clock::now();
auto messages = partitionClient.ReceiveEvents(5);
std::chrono::system_clock::time_point end = std::chrono::system_clock::now();
std::chrono::duration<double> elapsed_seconds = end - start;
EXPECT_GE(messages.size(), 1ul);
EXPECT_LE(messages.size(), 5ul);
EXPECT_TRUE(elapsed_seconds.count() < 1);
totalReceived += messages.size();
}
// We should have 45 messages left, which we should get immediately.
do
{
std::chrono::system_clock::time_point start = std::chrono::system_clock::now();
auto messages = partitionClient.ReceiveEvents(50);
std::chrono::system_clock::time_point end = std::chrono::system_clock::now();
std::chrono::duration<double> elapsed_seconds = end - start;
EXPECT_LE(messages.size(), 45ul);
totalReceived += messages.size();
EXPECT_TRUE(elapsed_seconds.count() < 1);
} while (totalReceived < numberOfEvents);
EXPECT_EQ(totalReceived, numberOfEvents);
// We have consumed all the events. Attempting to consume one more should block.
{
Azure::Core::Context timeout = Azure::Core::Context::ApplicationContext.WithDeadline(
Azure::DateTime::clock::now() + std::chrono::seconds(3));
EXPECT_THROW(
partitionClient.ReceiveEvents(50, timeout), Azure::Core::OperationCancelledException);
}
}
eventhubNamespace.DeleteEventHub(eventHubName);
#endif
}
namespace {
static std::string GetSuffix(const testing::TestParamInfo<AuthType>& info)
{
std::string stringValue = "";
switch (info.param)
{
case AuthType::ConnectionString:
stringValue = "ConnectionString_LIVEONLY_";
break;
case AuthType::Key:
stringValue = "Key_LIVEONLY_";
break;
case AuthType::Emulator:
stringValue = "Emulator";
break;
}
return stringValue;
}
} // namespace
INSTANTIATE_TEST_SUITE_P(
EventHubs,
ConsumerClientTest,
::testing::Values(AuthType::Key, AuthType::ConnectionString /*, AuthType::Emulator*/),
GetSuffix);
}}}} // namespace Azure::Messaging::EventHubs::Test