1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2023 Google LLC. All rights reserved.
3 //
4 // Use of this source code is governed by a BSD-style
5 // license that can be found in the LICENSE file or at
6 // https://developers.google.com/open-source/licenses/bsd
7
8 #include "google/protobuf/hpb/internal/message_lock.h"
9
10 #include <atomic>
11 #include <mutex>
12 #include <string>
13 #include <thread>
14
15 #include <gmock/gmock.h>
16 #include <gtest/gtest.h>
17 #include "absl/hash/hash.h"
18 #include "absl/log/absl_check.h"
19 #include "google/protobuf/compiler/hpb/tests/test_model.upb.proto.h"
20 #include "google/protobuf/hpb/hpb.h"
21 #include "upb/mem/arena.hpp"
22
23 #ifndef ASSERT_OK
24 #define ASSERT_OK(x) ASSERT_TRUE(x.ok())
25 #endif // ASSERT_OK
26 #ifndef EXPECT_OK
27 #define EXPECT_OK(x) EXPECT_TRUE(x.ok())
28 #endif // EXPECT_OK
29
30 namespace hpb_unittest::protos {
31
32 namespace {
33
GenerateTestData()34 std::string GenerateTestData() {
35 TestModel model;
36 model.set_str1("str");
37 ThemeExtension extension1;
38 extension1.set_ext_name("theme");
39 ABSL_CHECK_OK(::hpb::SetExtension(&model, theme, extension1));
40 ThemeExtension extension2;
41 extension2.set_ext_name("theme_extension");
42 ABSL_CHECK_OK(
43 ::hpb::SetExtension(&model, ThemeExtension::theme_extension, extension2));
44 ::upb::Arena arena;
45 auto bytes = ::hpb::Serialize(&model, arena);
46 ABSL_CHECK_OK(bytes);
47 return std::string(bytes->data(), bytes->size());
48 }
49
50 std::mutex m[8];
unlock_func(const void * msg)51 void unlock_func(const void* msg) { m[absl::HashOf(msg) & 0x7].unlock(); }
lock_func(const void * msg)52 ::hpb::internal::UpbExtensionUnlocker lock_func(const void* msg) {
53 m[absl::HashOf(msg) & 0x7].lock();
54 return &unlock_func;
55 }
56
TestConcurrentExtensionAccess(::hpb::ExtensionRegistry registry)57 void TestConcurrentExtensionAccess(::hpb::ExtensionRegistry registry) {
58 ::hpb::internal::upb_extension_locker_global.store(&lock_func,
59 std::memory_order_release);
60 const std::string payload = GenerateTestData();
61 TestModel parsed_model = ::hpb::Parse<TestModel>(payload, registry).value();
62 const auto test_main = [&] { EXPECT_EQ("str", parsed_model.str1()); };
63 const auto test_theme = [&] {
64 ASSERT_TRUE(::hpb::HasExtension(&parsed_model, theme));
65 auto ext = hpb::GetExtension(&parsed_model, theme);
66 ASSERT_OK(ext);
67 EXPECT_EQ((*ext)->ext_name(), "theme");
68 };
69 const auto test_theme_extension = [&] {
70 auto ext =
71 hpb::GetExtension(&parsed_model, ThemeExtension::theme_extension);
72 ASSERT_OK(ext);
73 EXPECT_EQ((*ext)->ext_name(), "theme_extension");
74 };
75 const auto test_serialize = [&] {
76 ::upb::Arena arena;
77 EXPECT_OK(::hpb::Serialize(&parsed_model, arena));
78 };
79 const auto test_copy_constructor = [&] {
80 TestModel copy_a = parsed_model;
81 TestModel copy_b = parsed_model;
82 EXPECT_EQ(copy_a.has_str1(), copy_b.has_str1());
83 };
84 std::thread t1(test_main);
85 std::thread t2(test_main);
86 std::thread t3(test_theme);
87 std::thread t4(test_theme);
88 std::thread t5(test_theme_extension);
89 std::thread t6(test_theme_extension);
90 std::thread t7(test_serialize);
91 std::thread t8(test_copy_constructor);
92 t1.join();
93 t2.join();
94 t3.join();
95 t4.join();
96 t5.join();
97 t6.join();
98 t7.join();
99 t8.join();
100 test_main();
101 test_theme();
102 test_theme_extension();
103 }
104
TEST(CppGeneratedCode,ConcurrentAccessDoesNotRaceBothLazy)105 TEST(CppGeneratedCode, ConcurrentAccessDoesNotRaceBothLazy) {
106 ::upb::Arena arena;
107 TestConcurrentExtensionAccess({{}, arena});
108 }
109
TEST(CppGeneratedCode,ConcurrentAccessDoesNotRaceOneLazyOneEager)110 TEST(CppGeneratedCode, ConcurrentAccessDoesNotRaceOneLazyOneEager) {
111 ::upb::Arena arena;
112 TestConcurrentExtensionAccess({{&theme}, arena});
113 TestConcurrentExtensionAccess({{&ThemeExtension::theme_extension}, arena});
114 }
115
TEST(CppGeneratedCode,ConcurrentAccessDoesNotRaceBothEager)116 TEST(CppGeneratedCode, ConcurrentAccessDoesNotRaceBothEager) {
117 ::upb::Arena arena;
118 TestConcurrentExtensionAccess(
119 {{&theme, &ThemeExtension::theme_extension}, arena});
120 }
121
122 } // namespace
123 } // namespace hpb_unittest::protos
124