From c8efd47a4516397961eeb57be3bd78107c037186 Mon Sep 17 00:00:00 2001 From: morenol Date: Sat, 23 Sep 2023 18:51:32 +0000 Subject: [PATCH] Examples segfault handler (#253) --- examples/Makefile | 4 +++- examples/class-async/test.js | 12 ++++++---- examples/class-wrapper/test.js | 10 ++++---- examples/package-lock.json | 44 ++++++++++++++++++++++++++++++++++ examples/package.json | 14 +++++++++++ examples/promise/test.js | 14 ++++++----- examples/stream/test.js | 12 ++++++---- 7 files changed, 89 insertions(+), 21 deletions(-) create mode 100644 examples/package-lock.json create mode 100644 examples/package.json diff --git a/examples/Makefile b/examples/Makefile index 5238f938..0b0faee8 100644 --- a/examples/Makefile +++ b/examples/Makefile @@ -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 diff --git a/examples/class-async/test.js b/examples/class-async/test.js index a50dad17..cb81632d 100644 --- a/examples/class-async/test.js +++ b/examples/class-async/test.js @@ -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");; }); diff --git a/examples/class-wrapper/test.js b/examples/class-wrapper/test.js index dd6e7c7d..f5e04af3 100644 --- a/examples/class-wrapper/test.js +++ b/examples/class-wrapper/test.js @@ -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); }); \ No newline at end of file diff --git a/examples/package-lock.json b/examples/package-lock.json new file mode 100644 index 00000000..c0b8b563 --- /dev/null +++ b/examples/package-lock.json @@ -0,0 +1,44 @@ +{ + "name": "node-bindgen-examples", + "version": "1.0.0", + "lockfileVersion": 3, + "requires": true, + "packages": { + "": { + "name": "node-bindgen-examples", + "version": "1.0.0", + "license": "ISC", + "dependencies": { + "segfault-handler": "^1.3.0" + } + }, + "node_modules/bindings": { + "version": "1.5.0", + "resolved": "https://registry.npmjs.org/bindings/-/bindings-1.5.0.tgz", + "integrity": "sha512-p2q/t/mhvuOj/UeLlV6566GD/guowlr0hHxClI0W9m7MWYkL1F0hLo+0Aexs9HSPCtR1SXQ0TD3MMKrXZajbiQ==", + "dependencies": { + "file-uri-to-path": "1.0.0" + } + }, + "node_modules/file-uri-to-path": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/file-uri-to-path/-/file-uri-to-path-1.0.0.tgz", + "integrity": "sha512-0Zt+s3L7Vf1biwWZ29aARiVYLx7iMGnEUl9x33fbB/j3jR81u/O2LbqK+Bm1CDSNDKVtJ/YjwY7TUd5SkeLQLw==" + }, + "node_modules/nan": { + "version": "2.18.0", + "resolved": "https://registry.npmjs.org/nan/-/nan-2.18.0.tgz", + "integrity": "sha512-W7tfG7vMOGtD30sHoZSSc/JVYiyDPEyQVso/Zz+/uQd0B0L46gtC+pHha5FFMRpil6fm/AoEcRWyOVi4+E/f8w==" + }, + "node_modules/segfault-handler": { + "version": "1.3.0", + "resolved": "https://registry.npmjs.org/segfault-handler/-/segfault-handler-1.3.0.tgz", + "integrity": "sha512-p7kVHo+4uoYkr0jmIiTBthwV5L2qmWtben/KDunDZ834mbos+tY+iO0//HpAJpOFSQZZ+wxKWuRo4DxV02B7Lg==", + "hasInstallScript": true, + "dependencies": { + "bindings": "^1.2.1", + "nan": "^2.14.0" + } + } + } +} diff --git a/examples/package.json b/examples/package.json new file mode 100644 index 00000000..46c57f34 --- /dev/null +++ b/examples/package.json @@ -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" + } +} diff --git a/examples/promise/test.js b/examples/promise/test.js index cb2679d1..4f99e552 100644 --- a/examples/promise/test.js +++ b/examples/promise/test.js @@ -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 () => { diff --git a/examples/stream/test.js b/examples/stream/test.js index 4e13ff8c..4059ff3c 100644 --- a/examples/stream/test.js +++ b/examples/stream/test.js @@ -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; @@ -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