Skip to content

Commit

Permalink
Fix testSchemaIncompatibility for Protobuf 3.20.0 or later (#282)
Browse files Browse the repository at this point in the history
fixes #193

### Motivation

`getExternalMessageDescriptor` returns the descriptor of `TestMessage`
rather than `ExternalMessage` for Protobuf 3.20.0 or later. And the
conditional compilation is meaningless here because `GetDescriptor` is
always a static method while it didn't exist for some early versions of
Protobuf. The official documentation suggests using `descriptor()`.

### Modifications

Use `descriptor()` method to get the descriptor of a PB class.
  • Loading branch information
BewareMyPower authored Jun 13, 2023
1 parent d03e46a commit 9bfed6d
Showing 1 changed file with 8 additions and 34 deletions.
42 changes: 8 additions & 34 deletions tests/ProtobufNativeSchemaTest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -29,33 +29,6 @@ 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 @@ -69,7 +42,7 @@ TEST(ProtobufNativeSchemaTest, testSchemaJson) {
"NzYWdlEhMKC3N0cmluZ0ZpZWxkGAEgASgJEhMKC2RvdWJsZUZpZWxkGAIgASgBQjUKJW9yZy5hcGFjaGUucHVsc2FyLmNsaWVudC"
"5zY2hlbWEucHJvdG9CDEV4dGVybmFsVGVzdGIGcHJvdG8z\",\"rootMessageTypeName\":\"proto.TestMessage\","
"\"rootFileDescriptorName\":\"Test.proto\"}";
const auto schemaInfo = createProtobufNativeSchema(getTestMessageDescriptor());
const auto schemaInfo = createProtobufNativeSchema(::proto::TestMessage::descriptor());

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

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

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

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

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

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

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

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

0 comments on commit 9bfed6d

Please sign in to comment.