Skip to content

Commit 411ae07

Browse files
authored
test the shared_ptr async destruction (#58)
1 parent 99cc1f9 commit 411ae07

File tree

3 files changed

+27
-0
lines changed

3 files changed

+27
-0
lines changed

test/single.js

100644100755
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#!/usr/bin/env node
2+
13
const fs = require('fs');
24
const path = require('path');
35
const framework = require('./framework');

test/tests/stress.cc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@ std::vector<Hello *> take_and_return_ptr_vector(const std::vector<Hello *> &inpu
5050
std::shared_ptr<Hello> take_and_return_shared_ptr(const std::shared_ptr<Hello> in) { return in; }
5151
std::shared_ptr<Hello> make_shared_ptr(std::string name) { return std::make_shared<Hello>(name); }
5252

53+
std::string take_shared_ptr(std::shared_ptr<Hello> in) {
54+
return in->Greet("Citizen");
55+
}
56+
5357
NOBIND_MODULE(stress, m) {
5458
m.def<Hello>("Hello")
5559
.cons<std::string &>()
@@ -78,4 +82,5 @@ NOBIND_MODULE(stress, m) {
7882

7983
m.def<&take_and_return_shared_ptr, Nobind::ReturnAsync>("take_and_return_shared_ptr");
8084
m.def<&make_shared_ptr, Nobind::ReturnAsync>("make_shared_ptr");
85+
m.def<&take_shared_ptr, Nobind::ReturnAsync>("take_shared_ptr");
8186
}

test/tests/stress.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ describe('stress tests', function () {
126126
hellos[pick] = await dll.take_and_return_shared_ptr(hello);
127127
}
128128
});
129+
129130
it('shared_ptr (start with a shared pointer)', async () => {
130131
// In this case JavaScript will always hold smart pointers
131132
const hellos = await Promise.all((new Array(1000)).fill(dll.make_shared_ptr('Merkwürdigliebe'), 0, 1000));
@@ -138,4 +139,23 @@ describe('stress tests', function () {
138139
hellos[pick] = await dll.take_and_return_shared_ptr(hello);
139140
}
140141
});
142+
143+
it('test shared_ptr destruction in background threads', async () => {
144+
// https://github.com/mmomtchev/nobind/issues/56
145+
// Make nobind create shared pointers out of anonymous
146+
// JS objects and process them asynchronously to ensure
147+
// that the JS reference is never released in a background
148+
// thread
149+
const q = [];
150+
for (let i = 0; i < 5e4; i++) {
151+
const pick = Math.floor(Math.random() * 1000);
152+
q.push(dll.take_shared_ptr(new dll.Hello('Rasczak')));
153+
}
154+
155+
const r = await Promise.all(q);
156+
assert.lengthOf(r, 5e4);
157+
for (const s of r) {
158+
assert.strictEqual(s, 'hello Citizen Rasczak');
159+
}
160+
});
141161
});

0 commit comments

Comments
 (0)