Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions src/node_threadsafe_cow-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,11 @@ T* ThreadsafeCopyOnWrite<T>::Write::operator->() {
}

template <typename T>
ThreadsafeCopyOnWrite<T>::Impl::Impl(const Impl& other) {
RwLock::ScopedReadLock lock(other.mutex);
data = other.data;
}
ThreadsafeCopyOnWrite<T>::Impl::Impl(const Impl& other)
: data([&other]() {
RwLock::ScopedReadLock lock(other.mutex);
return other.data;
}()) {}

} // namespace node

Expand Down
17 changes: 17 additions & 0 deletions test/cctest/test_per_process.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#include <string>

using node::ThreadsafeCopyOnWrite;
using node::builtins::BuiltinLoader;
using node::builtins::BuiltinSourceMap;

Expand All @@ -18,6 +19,22 @@ class PerProcessTest : public ::testing::Test {

namespace {

struct CopyConstructOnly {
explicit CopyConstructOnly(int value) : value(value) {}
CopyConstructOnly() = delete;
CopyConstructOnly(const CopyConstructOnly&) = default;
CopyConstructOnly& operator=(const CopyConstructOnly&) = delete;

int value;
};

TEST(ThreadsafeCopyOnWriteTest, CloneCopyConstructsData) {
const ThreadsafeCopyOnWrite<CopyConstructOnly> original(42);
auto copy = original;

EXPECT_EQ(copy.write()->value, 42);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Optional test improvement

The existing assertion proves cloning succeeded, but the test could additionally demonstrate COW independence:

TEST(ThreadsafeCopyOnWriteTest, CloneCopyConstructsData) {
  const ThreadsafeCopyOnWrite<CopyConstructOnly> original(42);
  auto copy = original;

  copy.write()->value = 43;

  EXPECT_EQ(original.read()->value, 42);
  EXPECT_EQ(copy.read()->value, 43);
}

}

TEST_F(PerProcessTest, EmbeddedSources) {
const auto& sources = PerProcessTest::get_sources_for_test();
ASSERT_TRUE(std::any_of(sources.cbegin(), sources.cend(), [](auto p) {
Expand Down
Loading