Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 34 additions & 7 deletions tests/ProtobufNativeSchemaTest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,33 @@ using namespace pulsar;

static std::string lookupUrl = "pulsar://localhost:6650";

const ::google::protobuf::Descriptor* getTestMessageDescriptor() {
#if GOOGLE_PROTOBUF_VERSION < 3020000
::proto::TestMessage msg;
return msg.GetDescriptor();
#else
return ::proto::TestMessage::GetDescriptor();
#endif
}

const ::google::protobuf::Descriptor* getExternalMessageDescriptor() {
#if GOOGLE_PROTOBUF_VERSION < 3020000
::proto::external::ExternalMessage msg;
return msg.GetDescriptor();
#else
return ::proto::TestMessage::GetDescriptor();
#endif
}

const ::google::protobuf::Descriptor* getDemoPersonDescriptor() {
#if GOOGLE_PROTOBUF_VERSION < 3020000
::padding::demo::Person p;
return p.GetDescriptor();
#else
return ::padding::demo::Person::GetDescriptor();
#endif
}

TEST(ProtobufNativeSchemaTest, testSchemaJson) {
const std::string expectedSchemaJson =
"{\"fileDescriptorSet\":"
Expand All @@ -40,7 +67,7 @@ TEST(ProtobufNativeSchemaTest, testSchemaJson) {
"NzYWdlEhMKC3N0cmluZ0ZpZWxkGAEgASgJEhMKC2RvdWJsZUZpZWxkGAIgASgBQjUKJW9yZy5hcGFjaGUucHVsc2FyLmNsaWVudC"
"5zY2hlbWEucHJvdG9CDEV4dGVybmFsVGVzdGIGcHJvdG8z\",\"rootMessageTypeName\":\"proto.TestMessage\","
"\"rootFileDescriptorName\":\"Test.proto\"}";
const auto schemaInfo = createProtobufNativeSchema(::proto::TestMessage::GetDescriptor());
const auto schemaInfo = createProtobufNativeSchema(getTestMessageDescriptor());

ASSERT_EQ(schemaInfo.getSchemaType(), pulsar::PROTOBUF_NATIVE);
ASSERT_TRUE(schemaInfo.getName().empty());
Expand All @@ -52,7 +79,7 @@ TEST(ProtobufNativeSchemaTest, testAutoCreateSchema) {
const std::string topicPrefix = "ProtobufNativeSchemaTest-testAutoCreateSchema-";
Client client(lookupUrl);

const auto schemaInfo = createProtobufNativeSchema(::proto::TestMessage::GetDescriptor());
const auto schemaInfo = createProtobufNativeSchema(getTestMessageDescriptor());
Producer producer;
ASSERT_EQ(ResultOk, client.createProducer(topicPrefix + "producer",
ProducerConfiguration().setSchema(schemaInfo), producer));
Expand All @@ -73,15 +100,15 @@ TEST(ProtobufNativeSchemaTest, testSchemaIncompatibility) {
};

// Create the protobuf native schema automatically
ASSERT_EQ(ResultOk, createProducerResult(::proto::TestMessage::GetDescriptor()));
ASSERT_EQ(ResultOk, createProducerResult(getTestMessageDescriptor()));
producer.close();

// Try to create producer with another protobuf generated class
ASSERT_EQ(ResultIncompatibleSchema,
createProducerResult(::proto::external::ExternalMessage::GetDescriptor()));
createProducerResult(getExternalMessageDescriptor()));

// Try to create producer with the original schema again
ASSERT_EQ(ResultOk, createProducerResult(::proto::TestMessage::GetDescriptor()));
ASSERT_EQ(ResultOk, createProducerResult(getTestMessageDescriptor()));

// createProtobufNativeSchema() cannot accept a null descriptor
try {
Expand All @@ -97,7 +124,7 @@ TEST(ProtobufNativeSchemaTest, testEndToEnd) {
const std::string topic = "ProtobufSchemaTest-testEndToEnd";
Client client(lookupUrl);

const auto schemaInfo = createProtobufNativeSchema(::proto::TestMessage::GetDescriptor());
const auto schemaInfo = createProtobufNativeSchema(getTestMessageDescriptor());
Consumer consumer;
ASSERT_EQ(ResultOk,
client.subscribe(topic, "my-sub", ConsumerConfiguration().setSchema(schemaInfo), consumer));
Expand Down Expand Up @@ -128,7 +155,7 @@ TEST(ProtobufNativeSchemaTest, testEndToEnd) {
}

TEST(ProtobufNativeSchemaTest, testBase64WithPadding) {
const auto schemaInfo = createProtobufNativeSchema(::padding::demo::Person::GetDescriptor());
const auto schemaInfo = createProtobufNativeSchema(getDemoPersonDescriptor());
const auto schemaJson = schemaInfo.getSchema();
size_t pos = schemaJson.find(R"(","rootMessageTypeName":)");
ASSERT_NE(pos, std::string::npos);
Expand Down