Skip to content

Commit

Permalink
Examples segfault handler (#253)
Browse files Browse the repository at this point in the history
  • Loading branch information
morenol committed Sep 23, 2023
1 parent f97b6b0 commit c8efd47
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 21 deletions.
4 changes: 3 additions & 1 deletion examples/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@ clean:
make -C cleanup clean
make -C logging clean

install:
npm install

test: test-function test-cb test-async-cb test-promise test-json test-class-simple \
test: install test-function test-cb test-async-cb test-promise test-json test-class-simple \
test-class-wrapper test-class-async test-stream test-buffer test-array test-bigint test-logging\
test-cleanup

Expand Down
12 changes: 7 additions & 5 deletions examples/class-async/test.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
const assert = require('assert');
const SegfaultHandler = require('segfault-handler');
SegfaultHandler.registerHandler('crash.log');

let addon = require('./dist');

let obj = new addon.MyObject(10);


obj.plusTwo(10).then( (val) => {
console.log("plus two is ",val);
obj.plusTwo(10).then((val) => {
console.log("plus two is ", val);
});

obj.multiply2(-1).then( (obj3) => {
console.log("multiply two ",obj3.value);
obj.multiply2(-1).then((obj3) => {
console.log("multiply two ", obj3.value);
});

obj.sleep((msg) => {
assert.equal(msg,"hello world");;
assert.equal(msg, "hello world");;
});
10 changes: 6 additions & 4 deletions examples/class-wrapper/test.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
const assert = require('assert');
const SegfaultHandler = require('segfault-handler');
SegfaultHandler.registerHandler('crash.log');

let addon = require('./dist');

let t = new addon.TestObject();
t.value = 20;
assert.equal(t.value2,20);
assert.equal(t.value2, 20);


assert.equal(addon.simple(5).value2,5);
assert.equal(addon.simple(5).value2, 5);

addon.create(10).then( (test_object) => {
console.log("test value is %s",test_object.value2);
addon.create(10).then((test_object) => {
console.log("test value is %s", test_object.value2);
});
44 changes: 44 additions & 0 deletions examples/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions examples/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"name": "node-bindgen-examples",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"segfault-handler": "^1.3.0"
}
}
14 changes: 8 additions & 6 deletions examples/promise/test.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
const assert = require('assert');
let addon = require('./dist');
const SegfaultHandler = require('segfault-handler');
SegfaultHandler.registerHandler('crash.log');


addon.hello(5).then((val) => {
assert.equal(val,15);
console.log("promise test succeed: %s",val);
assert.equal(val, 15);
console.log("promise test succeed: %s", val);
});

(
async () => {
let val = await addon.hello(5);
assert.equal(val,15);
})();
async () => {
let val = await addon.hello(5);
assert.equal(val, 15);
})();


(async () => {
Expand Down
12 changes: 7 additions & 5 deletions examples/stream/test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
const assert = require('assert');
let addon = require('./dist');
const SegfaultHandler = require('segfault-handler');
SegfaultHandler.registerHandler('crash.log');


const EventEmitter = require('events').EventEmitter;
Expand All @@ -8,24 +10,24 @@ const emitter = new EventEmitter();
let sum = 0;

emitter.on('data', (evt) => {
console.log("received event",evt);
sum = sum + evt;
console.log("received event", evt);
sum = sum + evt;
})

let factory = new addon.StreamFactory();

// test for error
assert.throws( () => factory.stream(20,emitter.emit.bind(emitter)),{
assert.throws(() => factory.stream(20, emitter.emit.bind(emitter)), {
message: 'count: 20 should be less than or equal to 10'
});


factory.stream(10,emitter.emit.bind(emitter));
factory.stream(10, emitter.emit.bind(emitter));

console.log("stream started");

// wait for stream to finish, since stream produce event at every 100ms we should wait at least 1 second
setTimeout(() => {
console.log("timer finished");
assert.equal(sum,45);
assert.equal(sum, 45);
}, 3000); // Made a bit larger so it reliably works on Travis

0 comments on commit c8efd47

Please sign in to comment.