Skip to content

Add a test case that exercises pybind11_protobuf use in parallel. #16

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 2, 2021
Merged
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
24 changes: 24 additions & 0 deletions pybind11_protobuf/tests/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -208,3 +208,27 @@ py_test(
"@com_google_absl_py//absl/testing:parameterized",
],
)

pybind_extension(
name = "thread_module",
srcs = ["thread_module.cc"],
deps = [
":test_cc_proto",
"//pybind11_protobuf:native_proto_caster",
"@com_google_absl//absl/time",
"@com_google_protobuf//:protobuf",
],
)

py_test(
name = "thread_module_test",
srcs = ["thread_module_test.py"],
data = [":thread_module.so"],
python_version = "PY3",
srcs_version = "PY3",
deps = [
":test_py_pb2",
"@com_google_absl_py//absl/testing:absltest",
"@com_google_absl_py//absl/testing:parameterized",
],
)
61 changes: 61 additions & 0 deletions pybind11_protobuf/tests/thread_module.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// Copyright (c) 2021 The Pybind Development Team. All rights reserved.
//
// All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

#include <pybind11/pybind11.h>

#include <functional>
#include <memory>
#include <stdexcept>

#include "absl/time/clock.h"
#include "absl/time/time.h"
#include "pybind11_protobuf/native_proto_caster.h"
#include "pybind11_protobuf/tests/test.pb.h"

namespace py = ::pybind11;

using pybind11::test::TestMessage;

namespace {

PYBIND11_MODULE(thread_module, m) {
m.def(
"make_message",
[](std::string text) -> TestMessage {
TestMessage msg;
msg.set_string_value(std::move(text));
return msg;
},
py::arg("text") = "");

m.def(
"make_message_string_view",
[](std::string_view text) -> TestMessage {
TestMessage msg;
msg.set_string_value(std::string(text));
return msg;
},
py::arg("text") = "");

m.def(
"make_message_no_gil",
[](std::string text) -> TestMessage {
TestMessage msg;
msg.set_string_value(std::move(text));
return msg;
},
py::arg("text") = "", py::call_guard<py::gil_scoped_release>());

m.def(
"make_message_string_view_no_gil",
[](std::string_view text) -> TestMessage {
TestMessage msg;
msg.set_string_value(std::string(text));
return msg;
},
py::arg("text") = "", py::call_guard<py::gil_scoped_release>());
}

} // namespace
50 changes: 50 additions & 0 deletions pybind11_protobuf/tests/thread_module_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
"""Test pybind11_protobuf with multiple threads.

Run with `blaze test :thread_test --config=tsan`.
"""

import concurrent.futures

from absl.testing import absltest
from absl.testing import parameterized
from pybind11_protobuf.tests import thread_module


def make_message(x):
return thread_module.make_message(x)


def make_message_string_view(x):
return thread_module.make_message_string_view(x)


def make_message_no_gil(x):
return thread_module.make_message_no_gil(x)


def make_message_string_view_no_gil(x):
return thread_module.make_message_string_view_no_gil(x)


class ThreadTest(parameterized.TestCase):

@parameterized.named_parameters(
('make_message', make_message),
('make_message_string_view', make_message_string_view),
('make_message_no_gil', make_message_no_gil),
# BUG: https://github.com/pybind/pybind11/issues/2765
# The following fails due to std::string_view casting in pybind11
# ('make_message_string_view_no_gil', make_message_string_view_no_gil),
)
def test_parallel(self, fn):
fn('a')

with concurrent.futures.ThreadPoolExecutor(max_workers=4) as executor:
results = list(executor.map(fn, ['abc'] * 10))

for x in results:
self.assertEqual('abc', x.string_value)


if __name__ == '__main__':
absltest.main()