Skip to content

Commit 18b6265

Browse files
committed
test multiple calls to callbacks
1 parent 324ac5e commit 18b6265

File tree

3 files changed

+22
-1
lines changed

3 files changed

+22
-1
lines changed

TODO.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
11
* Support var args (`va_list`)
22
* Test with actual threads.
3-
* Test that longer-lived (i.e. called more than once) callbacks work.
43
* Build or recommend a library for dealing with C structs/enums/unions.

test/adder/adder.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@
1010
void test_add_async_##t(t a, t b, void (*cb)(t)) {\
1111
cb(a + b);\
1212
}\
13+
void test_add_async_twice_##t(t a, t b, void (*cb)(t)) {\
14+
cb(a + b);\
15+
cb(a + b);\
16+
}\
1317

1418
mk_adder(uint8_t)
1519
mk_adder(uint16_t)

test/test.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ const test = pitesti();
1010
let add;
1111
let addPtr;
1212
let addAsync;
13+
let addTwiceAsync;
1314

1415
const libAdder = path.join(__dirname, 'adder', 'libadder.so');
1516

@@ -34,6 +35,13 @@ test`get functions`(() => {
3435
'void',
3536
['uint32_t', 'uint32_t', ['void', ['uint32_t']]]
3637
);
38+
39+
addTwiceAsync = getNativeFunction(
40+
libAdder,
41+
'test_add_async_twice_uint32_t',
42+
'void',
43+
['uint32_t', 'uint32_t', ['void', ['uint32_t']]]
44+
);
3745
});
3846

3947
test`basic adding`(() => {
@@ -71,4 +79,14 @@ test`promisified adding`(async () => {
7179
assert.strictEqual(await addPromise(5, 3), 8);
7280
});
7381

82+
test`calling callback more than once`((done) => {
83+
let counter = 0;
84+
addTwiceAsync(4, 5, (result) => {
85+
assert.strictEqual(result, 9);
86+
if (++counter === 2) {
87+
done();
88+
}
89+
});
90+
});
91+
7492
test();

0 commit comments

Comments
 (0)