forked from apache/pulsar-client-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTableViewTest.cc
More file actions
265 lines (222 loc) · 9.75 KB
/
TableViewTest.cc
File metadata and controls
265 lines (222 loc) · 9.75 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
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
#include <gtest/gtest.h>
#include <pulsar/Client.h>
#include <chrono>
#include <future>
#include "HttpHelper.h"
#include "PulsarFriend.h"
#include "WaitUtils.h"
#include "lib/LogUtils.h"
#include "lib/TopicName.h"
using namespace pulsar;
static std::string lookupUrl = "pulsar://localhost:6650";
static std::string adminUrl = "http://localhost:8080/";
DECLARE_LOG_OBJECT()
TEST(TableViewTest, testCreateTableView) {
const std::string topic = "testCreateTableView" + std::to_string(time(nullptr));
Client client(lookupUrl);
static const std::string jsonSchema =
R"({"type":"record","name":"cpx","fields":[{"name":"re","type":"double"},{"name":"im","type":"double"}]})";
SchemaInfo schemaInfo(JSON, "test-json", jsonSchema);
ProducerConfiguration producerConfiguration;
producerConfiguration.setSchema(schemaInfo);
Producer producer;
ASSERT_EQ(ResultOk, client.createProducer(topic, producerConfiguration, producer));
// Create table view failed, The schema is not compatible
TableViewConfiguration tableViewConfiguration{.schemaInfo = SchemaInfo(AVRO, "", "")};
TableView tableView;
ASSERT_EQ(ResultIncompatibleSchema, client.createTableView(topic, tableViewConfiguration, tableView));
ASSERT_EQ(ResultConsumerNotInitialized, tableView.close());
// Create table view success.
ASSERT_EQ(ResultOk, client.createTableView(topic, {.schemaInfo = schemaInfo}, tableView));
ASSERT_EQ(ResultOk, tableView.close());
client.close();
}
TEST(TableViewTest, testSimpleTableView) {
const std::string topic = "testSimpleTableView" + std::to_string(time(nullptr));
Client client(lookupUrl);
ProducerConfiguration producerConfiguration;
Producer producer;
ASSERT_EQ(ResultOk, client.createProducer(topic, producerConfiguration, producer));
auto count = 20;
for (int i = 0; i < count; ++i) {
auto msg = MessageBuilder()
.setPartitionKey("key" + std::to_string(i))
.setContent("value" + std::to_string(i))
.build();
ASSERT_EQ(ResultOk, producer.send(msg));
}
// Create table view and assert size.
TableView tableView;
ASSERT_EQ(ResultOk, client.createTableView(topic, {}, tableView));
ASSERT_EQ(tableView.size(), count);
// Send some more messages, The 0 ~ count message key/value is duplicated send.
for (int i = 0; i < count * 2; ++i) {
auto msg = MessageBuilder()
.setPartitionKey("key" + std::to_string(i))
.setContent("value" + std::to_string(i))
.build();
ASSERT_EQ(ResultOk, producer.send(msg));
}
waitUntil(
std::chrono::seconds(2), [&] { return tableView.size() == count * 2; }, 1000);
ASSERT_EQ(tableView.size(), count * 2);
// assert interfaces.
std::string value;
ASSERT_TRUE(tableView.getValue("key1", value));
ASSERT_EQ(value, "value1");
ASSERT_TRUE(tableView.retrieveValue("key1", value));
ASSERT_EQ(value, "value1");
ASSERT_FALSE(tableView.containsKey("key1"));
ASSERT_EQ(tableView.snapshot().size(), count * 2 - 1);
ASSERT_EQ(tableView.size(), 0);
client.close();
}
TEST(TableViewTest, testPublishEmptyValue) {
const std::string topic = "testPublishEmptyValue" + std::to_string(time(nullptr));
Client client(lookupUrl);
ProducerConfiguration producerConfiguration;
Producer producer;
ASSERT_EQ(ResultOk, client.createProducer(topic, producerConfiguration, producer));
auto count = 20;
for (int i = 0; i < count; ++i) {
auto msg = MessageBuilder()
.setPartitionKey("key" + std::to_string(i))
.setContent("value" + std::to_string(i))
.build();
ASSERT_EQ(ResultOk, producer.send(msg));
}
// Create table view failed, The schema is not compatible
TableView tableView;
ASSERT_EQ(ResultOk, client.createTableView(topic, {}, tableView));
ASSERT_EQ(tableView.size(), count);
// Set the v of k1 is empty
auto msg = MessageBuilder().setPartitionKey("key1").setContent("").build();
ASSERT_EQ(ResultOk, producer.send(msg));
waitUntil(
std::chrono::seconds(2), [&] { return tableView.size() == count - 1; }, 1000);
ASSERT_EQ(tableView.size(), count - 1);
// assert interfaces.
std::string value;
ASSERT_TRUE(!tableView.containsKey("key1"));
ASSERT_TRUE(!tableView.getValue("key1", value));
ASSERT_TRUE(value.empty());
client.close();
}
TEST(TableViewTest, testNotSupportNonPersistentTopic) {
const std::string topic = TopicDomain::NonPersistent +
"://public/default/testNotSupportNonPersistentTopic" +
std::to_string(time(nullptr));
Client client(lookupUrl);
TableView tableView;
ASSERT_EQ(ResultNotAllowedError, client.createTableView(topic, {}, tableView));
client.close();
}
TEST(TableViewTest, testMultiTopicAndAutoUpdatePartitions) {
std::string uniqueTimeStr = std::to_string(time(nullptr));
std::string topic = "persistent://public/default/testMultiTopicAndAutoUpdatePartitions" + uniqueTimeStr;
ClientConfiguration clientConfiguration;
clientConfiguration.setPartititionsUpdateInterval(1);
Client client(lookupUrl, clientConfiguration);
// create partition is 5
{
std::string url = adminUrl +
"admin/v2/persistent/public/default/testMultiTopicAndAutoUpdatePartitions" +
uniqueTimeStr + "/partitions";
int res = makePutRequest(url, "5");
LOG_INFO("res = " << res);
ASSERT_FALSE(res != 204 && res != 409);
}
ProducerConfiguration producerConfiguration;
Producer producer;
ASSERT_EQ(ResultOk, client.createProducer(topic, producerConfiguration, producer));
auto count = 20;
for (int i = 0; i < count; ++i) {
auto msg = MessageBuilder()
.setPartitionKey("key" + std::to_string(i))
.setContent("value" + std::to_string(i))
.build();
ASSERT_EQ(ResultOk, producer.send(msg));
}
TableView tableView;
ASSERT_EQ(ResultOk, client.createTableView(topic, {}, tableView));
ASSERT_EQ(tableView.size(), count);
// update partitions is 10
{
std::string url = adminUrl +
"admin/v2/persistent/public/default/testMultiTopicAndAutoUpdatePartitions" +
uniqueTimeStr + "/partitions";
int res = makePostRequest(url, "10");
LOG_INFO("res = " << res);
ASSERT_FALSE(res != 204 && res != 409);
}
waitUntil(
std::chrono::seconds(5), [&] { return PulsarFriend::getPartitionProducerSize(producer) == 10; }, 200);
ASSERT_EQ(PulsarFriend::getPartitionProducerSize(producer), 10);
for (int i = count; i < count * 2; ++i) {
auto msg = MessageBuilder()
.setPartitionKey("key" + std::to_string(i))
.setContent("value" + std::to_string(i))
.build();
ASSERT_EQ(ResultOk, producer.send(msg));
}
waitUntil(
std::chrono::seconds(10), [&] { return tableView.size() == count * 2; }, 200);
ASSERT_EQ(tableView.size(), count * 2);
client.close();
}
TEST(TableViewTest, testMessageEncryption) {
ClientConfiguration config;
Client client(lookupUrl);
std::string topicName = "testMessageEncryption" + std::to_string(time(nullptr));
std::string PUBLIC_CERT_FILE_PATH = "../test-conf/public-key.client-rsa.pem";
std::string PRIVATE_CERT_FILE_PATH = "../test-conf/private-key.client-rsa.pem";
std::shared_ptr<pulsar::DefaultCryptoKeyReader> keyReader =
std::make_shared<pulsar::DefaultCryptoKeyReader>(PUBLIC_CERT_FILE_PATH, PRIVATE_CERT_FILE_PATH);
ProducerConfiguration producerConfig;
producerConfig.addEncryptionKey("client-rsa.pem");
producerConfig.setCryptoKeyReader(keyReader);
Producer producer;
ASSERT_EQ(ResultOk, client.createProducer(topicName, producerConfig, producer));
TableViewConfiguration tvConf;
tvConf.cryptoKeyReader = keyReader;
TableView tableView;
ASSERT_EQ(ResultOk, client.createTableView(topicName, tvConf, tableView));
int numberOfMessages = 10;
std::string msgContent = "msg-content";
std::set<std::string> valuesSent;
Message msg;
for (int i = 0; i < numberOfMessages; ++i) {
std::string key = std::to_string(i);
auto value = msgContent + key;
valuesSent.emplace(value);
msg = MessageBuilder().setPartitionKey(key).setContent(value).build();
ASSERT_EQ(ResultOk, producer.send(msg));
}
std::set<std::string> valuesReceived;
for (int i = 0; i < numberOfMessages; ++i) {
std::string key = std::to_string(i);
std::string value;
ASSERT_EQ(ResultOk, tableView.getValue(key, value));
valuesReceived.emplace(value);
}
ASSERT_EQ(valuesSent, valuesReceived);
ASSERT_EQ(ResultOk, client.close());
}