Skip to content

Commit

Permalink
Added test case for issue #867
Browse files Browse the repository at this point in the history
  • Loading branch information
vfdev-5 authored and wjakob committed Jan 28, 2025
1 parent 0035899 commit ba647eb
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
29 changes: 29 additions & 0 deletions tests/test_thread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,23 @@ struct GlobalData {} global_data;

nb::ft_mutex mutex;

struct ClassWithProperty {
public:
ClassWithProperty(int value): value_(value) {}
int get_prop() const { return value_; }
private:
int value_;
};

class ClassWithClassProperty {
public:
ClassWithClassProperty(ClassWithProperty value) : value_(std::move(value)) {};
const ClassWithProperty& get_prop() const { return value_; }
private:
ClassWithProperty value_;
};


NB_MODULE(test_thread_ext, m) {
nb::class_<Counter>(m, "Counter")
.def(nb::init<>())
Expand All @@ -39,4 +56,16 @@ NB_MODULE(test_thread_ext, m) {

nb::class_<GlobalData>(m, "GlobalData")
.def_static("get", [] { return &global_data; }, nb::rv_policy::reference);

nb::class_<ClassWithProperty>(m, "ClassWithProperty")
.def(nb::init<int>(), nb::arg("value"))
.def_prop_ro("prop2", &ClassWithProperty::get_prop);

nb::class_<ClassWithClassProperty>(m, "ClassWithClassProperty")
.def(
"__init__",
[](ClassWithClassProperty* self, ClassWithProperty value) {
new (self) ClassWithClassProperty(std::move(value));
}, nb::arg("value"))
.def_prop_ro("prop1", &ClassWithClassProperty::get_prop);
}
14 changes: 13 additions & 1 deletion tests/test_thread.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import test_thread_ext as t
from test_thread_ext import Counter, GlobalData
from test_thread_ext import Counter, GlobalData, ClassWithProperty, ClassWithClassProperty
from common import parallelize

def test01_object_creation(n_threads=8):
Expand Down Expand Up @@ -88,3 +88,15 @@ def f():
GlobalData.get()

parallelize(f, n_threads=n_threads)


def test07_access_attributes(n_threads=8):
n = 1000
c1 = ClassWithProperty(123)
c2 = ClassWithClassProperty(c1)

def f():
for i in range(n):
_ = c2.prop1.prop2

parallelize(f, n_threads=n_threads)

0 comments on commit ba647eb

Please sign in to comment.