-
Notifications
You must be signed in to change notification settings - Fork 465
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6192e70
commit 16f7249
Showing
8 changed files
with
664 additions
and
0 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
#include "napi.h" | ||
|
||
#include <chrono> | ||
#include <condition_variable> | ||
#include <mutex> | ||
#include <thread> | ||
|
||
#if (NAPI_VERSION > 3) | ||
|
||
using namespace Napi; | ||
|
||
namespace { | ||
|
||
struct ProgressData { | ||
size_t progress; | ||
}; | ||
|
||
class TestWorker : public AsyncProgressWorker<ProgressData> { | ||
public: | ||
static void DoWork(const CallbackInfo& info) { | ||
bool succeed = info[0].As<Boolean>(); | ||
Object resource = info[1].As<Object>(); | ||
Function cb = info[2].As<Function>(); | ||
Value data = info[3]; | ||
|
||
TestWorker* worker = new TestWorker(cb, "TestResource", resource); | ||
worker->Receiver().Set("data", data); | ||
worker->_succeed = succeed; | ||
worker->Queue(); | ||
} | ||
|
||
protected: | ||
void Execute(const ExecutionProgress& progress) override { | ||
std::unique_lock<std::mutex> lock(_cvm); | ||
progress.Send(new ProgressData{0}, 1); | ||
_cv.wait(lock); | ||
|
||
progress.Send(new ProgressData{50}, 1); | ||
_cv.wait(lock); | ||
if (!_succeed) { | ||
SetError("test error"); | ||
} | ||
progress.Send(new ProgressData{75}, 1); | ||
_cv.wait(lock); | ||
} | ||
|
||
void OnProgress(const ProgressData* data, size_t /* count */) override { | ||
FunctionReference& callback = Callback(); | ||
Napi::Env env = Env(); | ||
if (!callback.IsEmpty()) { | ||
Value err = env.Undefined(); | ||
Number progress = Number::New(env, data->progress); | ||
callback.Call(Receiver().Value(), { err, progress }); | ||
} | ||
_cv.notify_one(); | ||
} | ||
|
||
std::vector<napi_value> GetResult(Napi::Env env) override { | ||
Value err = env.Undefined(); | ||
Number progress = Number::New(env, 100); | ||
return {err, progress}; | ||
} | ||
|
||
private: | ||
TestWorker(Function cb, const char* resource_name, const Object& resource) | ||
: AsyncProgressWorker(cb, resource_name, resource) {} | ||
std::condition_variable _cv; | ||
std::mutex _cvm; | ||
bool _succeed; | ||
}; | ||
|
||
} | ||
|
||
Object InitAsyncProgressWorker(Env env) { | ||
Object exports = Object::New(env); | ||
exports["doWork"] = Function::New(env, TestWorker::DoWork); | ||
return exports; | ||
} | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
'use strict'; | ||
const buildType = process.config.target_defaults.default_configuration; | ||
const common = require('./common') | ||
const assert = require('assert'); | ||
|
||
test(require(`./build/${buildType}/binding.node`)); | ||
test(require(`./build/${buildType}/binding_noexcept.node`)); | ||
|
||
function test(binding) { | ||
success(binding); | ||
fail(binding); | ||
return; | ||
} | ||
|
||
function success(binding) { | ||
const expected = [0, 50, 75, 100]; | ||
const actual = []; | ||
binding.asyncprogressworker.doWork(true, {}, common.mustCall((err, _progress) => { | ||
if (err) { | ||
assert.fail(err); | ||
return; | ||
} | ||
actual.push(_progress); | ||
if (actual.length === expected.length) { | ||
assert.deepEqual(actual, expected); | ||
} | ||
}, expected.length)); | ||
} | ||
|
||
function fail(binding) { | ||
let err = undefined | ||
binding.asyncprogressworker.doWork(false, {}, (_err) => { | ||
err = _err; | ||
}); | ||
process.on('exit', () => { | ||
assert.throws(() => { throw err }, /test error/) | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters