internal change
PiperOrigin-RevId: 735343531
Change-Id: I6dc4d96d32e13b66dce1ad44561b3b35cb4fdf6d
diff --git a/cc_bindings_from_rs/test/bridging/protobuf/foo_lib.rs b/cc_bindings_from_rs/test/bridging/protobuf/foo_lib.rs
index f295339..d08c4e2 100644
--- a/cc_bindings_from_rs/test/bridging/protobuf/foo_lib.rs
+++ b/cc_bindings_from_rs/test/bridging/protobuf/foo_lib.rs
@@ -20,4 +20,12 @@
pub fn request_stats(&self) -> FooRequestStatsView {
self.stats.as_view()
}
+
+ pub fn clone_request_stats(&self) -> FooRequestStats {
+ self.stats.clone()
+ }
+
+ pub fn update_request_stats(&mut self, updated_stats: FooRequestStats) {
+ self.stats = updated_stats;
+ }
}
diff --git a/cc_bindings_from_rs/test/bridging/protobuf/foo_lib_test.cc b/cc_bindings_from_rs/test/bridging/protobuf/foo_lib_test.cc
index 35d7b8f..de704dc 100644
--- a/cc_bindings_from_rs/test/bridging/protobuf/foo_lib_test.cc
+++ b/cc_bindings_from_rs/test/bridging/protobuf/foo_lib_test.cc
@@ -4,15 +4,13 @@
#include "cc_bindings_from_rs/test/bridging/protobuf/foo_lib.h"
-#include <memory>
-
#include "gtest/gtest.h"
#include "cc_bindings_from_rs/test/bridging/protobuf/foo.proto.h"
namespace crubit {
namespace {
-TEST(ProtoBridging, Basic) {
+TEST(ProtoBridging, ViewAndMutTypes) {
foo_service::FooRequest req;
req.set_input("hello world");
foo_service::FooResponse rsp;
@@ -27,5 +25,25 @@
EXPECT_EQ(req_stats->num_requests(), 1);
}
+TEST(ProtoBridging, OwnedMessages) {
+ foo_lib::FooService service;
+
+ foo_service::FooRequestStats cloned_req_stats = service.clone_request_stats();
+ EXPECT_EQ(cloned_req_stats.num_requests(), 0);
+
+ // Update the local message and check that the service's stats are not
+ // updated i.e. it was actually cloned.
+ cloned_req_stats.set_num_requests(2);
+ EXPECT_EQ(service.request_stats()->num_requests(), 0);
+
+ service.update_request_stats(cloned_req_stats);
+ EXPECT_EQ(service.request_stats()->num_requests(), 2);
+
+ // Update the local message again and check that the service's stats are
+ // not updated.
+ cloned_req_stats.set_num_requests(3);
+ EXPECT_EQ(service.request_stats()->num_requests(), 2);
+}
+
} // namespace
} // namespace crubit
\ No newline at end of file