Skip to content

Commit

Permalink
context_provider: exception free (envoyproxy#37457)
Browse files Browse the repository at this point in the history
Risk Level: low
Testing: updated tests
Docs Changes: n/a
Release Notes: n/a
envoyproxy/envoy-mobile#176

Signed-off-by: Alyssa Wilk <[email protected]>
  • Loading branch information
alyssawilk authored Dec 3, 2024
1 parent 5b1ab35 commit 602ebb2
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 20 deletions.
10 changes: 6 additions & 4 deletions envoy/config/context_provider.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,19 @@ class ContextProvider {
* @param resource_type_url resource type URL for context parameter.
* @param key parameter key.
* @param value parameter value.
* @return if the update was successful.
*/
virtual void setDynamicContextParam(absl::string_view resource_type_url, absl::string_view key,
absl::string_view value) PURE;
virtual absl::Status setDynamicContextParam(absl::string_view resource_type_url,
absl::string_view key, absl::string_view value) PURE;

/**
* Unset a dynamic context parameter.
* @param resource_type_url resource type URL for context parameter.
* @param key parameter key.
* @return if the update was successful.
*/
virtual void unsetDynamicContextParam(absl::string_view resource_type_url,
absl::string_view key) PURE;
virtual absl::Status unsetDynamicContextParam(absl::string_view resource_type_url,
absl::string_view key) PURE;

/**
* Register a callback for notification when the dynamic context changes.
Expand Down
12 changes: 6 additions & 6 deletions source/common/config/context_provider_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,20 @@ class ContextProviderImpl : public ContextProvider {
}
return xds::core::v3::ContextParams::default_instance();
};
void setDynamicContextParam(absl::string_view resource_type_url, absl::string_view key,
absl::string_view value) override {
absl::Status setDynamicContextParam(absl::string_view resource_type_url, absl::string_view key,
absl::string_view value) override {
ASSERT_IS_MAIN_OR_TEST_THREAD();
(*dynamic_context_[resource_type_url]
.mutable_params())[toStdStringView(key)] = // NOLINT(std::string_view)
toStdStringView(value); // NOLINT(std::string_view)
THROW_IF_NOT_OK(update_cb_helper_.runCallbacks(resource_type_url));
return update_cb_helper_.runCallbacks(resource_type_url);
}
void unsetDynamicContextParam(absl::string_view resource_type_url,
absl::string_view key) override {
absl::Status unsetDynamicContextParam(absl::string_view resource_type_url,
absl::string_view key) override {
ASSERT_IS_MAIN_OR_TEST_THREAD();
dynamic_context_[resource_type_url].mutable_params()->erase(
toStdStringView(key)); // NOLINT(std::string_view)
THROW_IF_NOT_OK(update_cb_helper_.runCallbacks(resource_type_url));
return update_cb_helper_.runCallbacks(resource_type_url);
}
ABSL_MUST_USE_RESULT Common::CallbackHandlePtr
addDynamicContextUpdateCallback(UpdateNotificationCb callback) const override {
Expand Down
8 changes: 4 additions & 4 deletions test/common/config/context_provider_impl_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -56,26 +56,26 @@ TEST(ContextProviderTest, DynamicContextParameters) {
EXPECT_EQ(0, update_count);

// Setting a DCP.
context_provider.setDynamicContextParam("some_type", "foo", "bar");
ASSERT_TRUE(context_provider.setDynamicContextParam("some_type", "foo", "bar").ok());
EXPECT_EQ(1, update_count);
EXPECT_EQ("some_type", last_updated_resource);
EXPECT_EQ("bar", context_provider.dynamicContext("some_type").params().at("foo"));

// Updating a DCP.
context_provider.setDynamicContextParam("some_type", "foo", "baz");
ASSERT_TRUE(context_provider.setDynamicContextParam("some_type", "foo", "baz").ok());
EXPECT_EQ(2, update_count);
EXPECT_EQ("some_type", last_updated_resource);
EXPECT_EQ("baz", context_provider.dynamicContext("some_type").params().at("foo"));

// Setting a DCP on an unrelated resource.
context_provider.setDynamicContextParam("other_type", "foo", "bar");
ASSERT_TRUE(context_provider.setDynamicContextParam("other_type", "foo", "bar").ok());
EXPECT_EQ(3, update_count);
EXPECT_EQ("other_type", last_updated_resource);
EXPECT_EQ("baz", context_provider.dynamicContext("some_type").params().at("foo"));
EXPECT_EQ("bar", context_provider.dynamicContext("other_type").params().at("foo"));

// Unsetting a DCP
context_provider.unsetDynamicContextParam("some_type", "foo");
ASSERT_TRUE(context_provider.unsetDynamicContextParam("some_type", "foo").ok());
EXPECT_EQ(4, update_count);
EXPECT_EQ("some_type", last_updated_resource);
EXPECT_EQ(0, context_provider.dynamicContext("some_type").params().count("foo"));
Expand Down
2 changes: 1 addition & 1 deletion test/common/local_info/local_info_impl_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ TEST(LocalInfoTest, DynamicContextUpdate) {
Stats::TestUtil::TestSymbolTable symbol_table;
LocalInfoImpl local_info(*symbol_table, {}, {}, nullptr, "zone_name", "cluster_name",
"node_name");
local_info.contextProvider().setDynamicContextParam("foo", "bar", "baz");
EXPECT_TRUE(local_info.contextProvider().setDynamicContextParam("foo", "bar", "baz").ok());
EXPECT_EQ("baz", local_info.node().dynamic_parameters().at("foo").params().at("bar"));
}

Expand Down
12 changes: 10 additions & 2 deletions test/integration/server.cc
Original file line number Diff line number Diff line change
Expand Up @@ -100,14 +100,22 @@ void IntegrationTestServer::waitUntilListenersReady() {
void IntegrationTestServer::setDynamicContextParam(absl::string_view resource_type_url,
absl::string_view key, absl::string_view value) {
server().dispatcher().post([this, resource_type_url, key, value]() {
server().localInfo().contextProvider().setDynamicContextParam(resource_type_url, key, value);
ASSERT_TRUE(server()
.localInfo()
.contextProvider()
.setDynamicContextParam(resource_type_url, key, value)
.ok());
});
}

void IntegrationTestServer::unsetDynamicContextParam(absl::string_view resource_type_url,
absl::string_view key) {
server().dispatcher().post([this, resource_type_url, key]() {
server().localInfo().contextProvider().unsetDynamicContextParam(resource_type_url, key);
ASSERT_TRUE(server()
.localInfo()
.contextProvider()
.unsetDynamicContextParam(resource_type_url, key)
.ok());
});
}

Expand Down
4 changes: 2 additions & 2 deletions test/mocks/config/mocks.h
Original file line number Diff line number Diff line change
Expand Up @@ -186,10 +186,10 @@ class MockContextProvider : public ContextProvider {
MOCK_METHOD(const xds::core::v3::ContextParams&, nodeContext, (), (const));
MOCK_METHOD(const xds::core::v3::ContextParams&, dynamicContext,
(absl::string_view resource_type_url), (const));
MOCK_METHOD(void, setDynamicContextParam,
MOCK_METHOD(absl::Status, setDynamicContextParam,
(absl::string_view resource_type_url, absl::string_view key,
absl::string_view value));
MOCK_METHOD(void, unsetDynamicContextParam,
MOCK_METHOD(absl::Status, unsetDynamicContextParam,
(absl::string_view resource_type_url, absl::string_view key));
MOCK_METHOD(Common::CallbackHandlePtr, addDynamicContextUpdateCallback,
(UpdateNotificationCb callback), (const));
Expand Down
1 change: 0 additions & 1 deletion tools/code_format/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,6 @@ paths:
- source/extensions/common/matcher/trie_matcher.h
- envoy/common/exception.h
- source/common/upstream/upstream_impl.h
- source/common/config/context_provider_impl.h
- source/common/protobuf/utility.h
# legacy core files which throw exceptions. We can add to this list but strongly prefer
# StausOr where possible.
Expand Down

0 comments on commit 602ebb2

Please sign in to comment.