[go: nahoru, domu]

Skip to content

Commit

Permalink
fix(pubsub): get future value before returning (#13241)
Browse files Browse the repository at this point in the history
  • Loading branch information
alevenberg committed Dec 8, 2023
1 parent e7df5df commit 32f90f1
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 3 deletions.
7 changes: 5 additions & 2 deletions google/cloud/pubsub/integration_tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,11 @@ function (google_cloud_cpp_pubsub_define_integration_tests)

set(pubsub_client_integration_tests
# cmake-format: sort
blocking_publisher_integration_test.cc subscriber_integration_test.cc
subscription_admin_integration_test.cc topic_admin_integration_test.cc)
blocking_publisher_integration_test.cc
publisher_integration_test.cc
subscriber_integration_test.cc
subscription_admin_integration_test.cc
topic_admin_integration_test.cc)

# Export the list of unit tests to a .bzl file so we do not need to maintain
# the list in two places.
Expand Down
107 changes: 107 additions & 0 deletions google/cloud/pubsub/integration_tests/publisher_integration_test.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
// Copyright 2023 Google LLC
//
// Licensed 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
//
// https://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 "google/cloud/pubsub/admin/topic_admin_client.h"
#include "google/cloud/pubsub/publisher.h"
#include "google/cloud/pubsub/testing/random_names.h"
#include "google/cloud/pubsub/testing/test_retry_policies.h"
#include "google/cloud/pubsub/topic_admin_client.h"
#include "google/cloud/pubsub/version.h"
#include "google/cloud/credentials.h"
#include "google/cloud/internal/getenv.h"
#include "google/cloud/internal/random.h"
#include "google/cloud/opentelemetry_options.h"
#include "google/cloud/testing_util/integration_test.h"
#include "google/cloud/testing_util/opentelemetry_matchers.h"
#include "google/cloud/testing_util/status_matchers.h"
#include <gmock/gmock.h>
#include <algorithm>

namespace google {
namespace cloud {
namespace pubsub {
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN
namespace {

using ::google::cloud::testing_util::IsOk;
using ::google::cloud::testing_util::StatusIs;
using ::testing::AnyOf;

class PublisherIntegrationTest
: public ::google::cloud::testing_util::IntegrationTest {
protected:
void SetUp() override {
auto project_id =
google::cloud::internal::GetEnv("GOOGLE_CLOUD_PROJECT").value_or("");
ASSERT_FALSE(project_id.empty());
generator_ = google::cloud::internal::DefaultPRNG(std::random_device{}());
topic_ = Topic(project_id, pubsub_testing::RandomTopicId(generator_));
options_ = Options{};
auto const using_emulator =
internal::GetEnv("PUBSUB_EMULATOR_HOST").has_value();
if (using_emulator) {
options_ = Options{}
.set<UnifiedCredentialsOption>(MakeInsecureCredentials())
.set<internal::UseInsecureChannelOption>(true);
}

auto topic_admin = pubsub_admin::TopicAdminClient(
pubsub_admin::MakeTopicAdminConnection(options_));

auto topic_metadata = topic_admin.CreateTopic(topic_.FullName());
ASSERT_THAT(topic_metadata,
AnyOf(IsOk(), StatusIs(StatusCode::kAlreadyExists)));
}

void TearDown() override {
auto topic_admin = pubsub_admin::TopicAdminClient(
pubsub_admin::MakeTopicAdminConnection(options_));

auto delete_topic = topic_admin.DeleteTopic(topic_.FullName());
EXPECT_THAT(delete_topic, AnyOf(IsOk(), StatusIs(StatusCode::kNotFound)));
}

google::cloud::Options options_;
google::cloud::internal::DefaultPRNG generator_;
Topic topic_ = Topic("unused", "unused");
};

TEST_F(PublisherIntegrationTest, Basic) {
auto publisher = Publisher(MakePublisherConnection(topic_, options_));
auto publish =
publisher.Publish(MessageBuilder().SetData("test data").Build()).get();
ASSERT_STATUS_OK(publish);
}

TEST_F(PublisherIntegrationTest, TracingEnabled) {
options_.set<OpenTelemetryTracingOption>(true);
auto publisher = Publisher(MakePublisherConnection(topic_, options_));
auto publish =
publisher.Publish(MessageBuilder().SetData("test data").Build()).get();
ASSERT_STATUS_OK(publish);
}

TEST_F(PublisherIntegrationTest, TracingDisabled) {
options_.set<OpenTelemetryTracingOption>(false);
auto publisher = Publisher(MakePublisherConnection(topic_, options_));
auto publish =
publisher.Publish(MessageBuilder().SetData("test data").Build()).get();
ASSERT_STATUS_OK(publish);
}

} // namespace
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END
} // namespace pubsub
} // namespace cloud
} // namespace google
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

pubsub_client_integration_tests = [
"blocking_publisher_integration_test.cc",
"publisher_integration_test.cc",
"subscriber_integration_test.cc",
"subscription_admin_integration_test.cc",
"topic_admin_integration_test.cc",
Expand Down
2 changes: 1 addition & 1 deletion google/cloud/pubsub/internal/tracing_batch_sink.cc
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ class TracingBatchSink : public BatchSink {
internal::EndSpan(*span);
}
internal::DetachOTelContext(oc);
return f;
return f.get();
});
}

Expand Down

0 comments on commit 32f90f1

Please sign in to comment.