-
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
630c005
commit 01d157c
Showing
7 changed files
with
296 additions
and
0 deletions.
There are no files selected for viewing
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,78 @@ | ||
#include "napi.h" | ||
|
||
#include <chrono> | ||
#include <condition_variable> | ||
#include <mutex> | ||
#include <thread> | ||
|
||
#if (NAPI_VERSION > 3) | ||
|
||
using namespace Napi; | ||
|
||
namespace { | ||
|
||
struct ProgressData { | ||
int32_t progress; | ||
}; | ||
|
||
class TestWorker : public AsyncProgressQueueWorker<ProgressData> { | ||
public: | ||
static void DoWork(const CallbackInfo& info) { | ||
int32_t times = info[0].As<Number>().Int32Value(); | ||
Function cb = info[1].As<Function>(); | ||
Function progress = info[2].As<Function>(); | ||
|
||
TestWorker* worker = new TestWorker(cb, progress, "TestResource", Object::New(info.Env())); | ||
worker->_times = times; | ||
worker->Queue(); | ||
} | ||
|
||
protected: | ||
void Execute(const ExecutionProgress& progress) override { | ||
if (_times < 0) { | ||
SetError("test error"); | ||
} | ||
ProgressData data{0}; | ||
for (int32_t idx = 0; idx < _times; idx++) { | ||
data.progress = idx; | ||
progress.Send(&data, 1); | ||
} | ||
// keep worker alive until we processed all progress. | ||
if (_times > 0) { | ||
std::unique_lock<std::mutex> lock(_cvm); | ||
_cv.wait(lock); | ||
} | ||
} | ||
|
||
void OnProgress(const ProgressData* data, size_t /* count */) override { | ||
Napi::Env env = Env(); | ||
if (!_progress.IsEmpty()) { | ||
Number progress = Number::New(env, data->progress); | ||
_progress.MakeCallback(Receiver().Value(), { progress }); | ||
} | ||
if (data->progress + 1 == _times) { | ||
_cv.notify_one(); | ||
} | ||
} | ||
|
||
private: | ||
TestWorker(Function cb, Function progress, const char* resource_name, const Object& resource) | ||
: AsyncProgressQueueWorker(cb, resource_name, resource) { | ||
_progress.Reset(progress, 1); | ||
} | ||
|
||
std::condition_variable _cv; | ||
std::mutex _cvm; | ||
int32_t _times; | ||
FunctionReference _progress; | ||
}; | ||
|
||
} | ||
|
||
Object InitAsyncProgressQueueWorker(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,42 @@ | ||
'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({ asyncprogressqueueworker }) { | ||
success(asyncprogressqueueworker); | ||
fail(asyncprogressqueueworker); | ||
return; | ||
} | ||
|
||
function success(binding) { | ||
const expected = [0, 1, 2, 3]; | ||
const actual = []; | ||
binding.doWork(expected.length, | ||
common.mustCall((err) => { | ||
if (err) { | ||
assert.fail(err); | ||
} | ||
}), | ||
common.mustCall((_progress) => { | ||
actual.push(_progress); | ||
if (actual.length === expected.length) { | ||
assert.deepEqual(actual, expected); | ||
} | ||
}, expected.length) | ||
); | ||
} | ||
|
||
function fail(binding) { | ||
binding.doWork(-1, | ||
common.mustCall((err) => { | ||
assert.throws(() => { throw err }, /test error/) | ||
}), | ||
() => { | ||
assert.fail('unexpected progress report'); | ||
} | ||
); | ||
} |
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