Skip to content

Commit 2ef579e

Browse files
mkrufkykkoopa
authored andcommitted
add tests for AsyncProgressQueueWorker
1 parent 7eb2d74 commit 2ef579e

4 files changed

Lines changed: 121 additions & 0 deletions

File tree

test/binding.gyp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@
4343
"target_name" : "returnemptystring"
4444
, "sources" : [ "cpp/returnemptystring.cpp" ]
4545
}
46+
, {
47+
"target_name" : "asyncprogressqueueworker"
48+
, "sources" : [ "cpp/asyncprogressqueueworker.cpp" ]
49+
}
4650
, {
4751
"target_name" : "asyncworker"
4852
, "sources" : [ "cpp/asyncworker.cpp" ]
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*********************************************************************
2+
* NAN - Native Abstractions for Node.js
3+
*
4+
* Copyright (c) 2017 NAN contributors
5+
*
6+
* MIT License <https://github.com/nodejs/nan/blob/master/LICENSE.md>
7+
********************************************************************/
8+
9+
#ifndef _WIN32
10+
#include <unistd.h>
11+
#define Sleep(x) usleep((x)*1000)
12+
#endif
13+
#include <nan.h>
14+
15+
using namespace Nan; // NOLINT(build/namespaces)
16+
17+
class ProgressQueueWorker : public AsyncProgressQueueWorker<char> {
18+
public:
19+
ProgressQueueWorker(
20+
Callback *callback
21+
, Callback *progress
22+
, int milliseconds
23+
, int iters)
24+
: AsyncProgressQueueWorker(callback), progress(progress)
25+
, milliseconds(milliseconds), iters(iters) {}
26+
27+
~ProgressQueueWorker() {
28+
delete progress;
29+
}
30+
31+
void Execute (const AsyncProgressQueueWorker::ExecutionProgress& progress) {
32+
for (int i = 0; i < iters; ++i) {
33+
progress.Send(reinterpret_cast<const char*>(&i), sizeof(int));
34+
Sleep(milliseconds);
35+
}
36+
}
37+
38+
void HandleProgressCallback(const char *data, size_t count) {
39+
HandleScope scope;
40+
41+
v8::Local<v8::Value> argv[] = {
42+
New<v8::Integer>(*reinterpret_cast<int*>(const_cast<char*>(data)))
43+
};
44+
progress->Call(1, argv);
45+
}
46+
47+
private:
48+
Callback *progress;
49+
int milliseconds;
50+
int iters;
51+
};
52+
53+
NAN_METHOD(DoProgress) {
54+
Callback *progress = new Callback(info[2].As<v8::Function>());
55+
Callback *callback = new Callback(info[3].As<v8::Function>());
56+
AsyncQueueWorker(new ProgressQueueWorker(
57+
callback
58+
, progress
59+
, To<uint32_t>(info[0]).FromJust()
60+
, To<uint32_t>(info[1]).FromJust()));
61+
}
62+
63+
NAN_MODULE_INIT(Init) {
64+
Set(target
65+
, New<v8::String>("a").ToLocalChecked()
66+
, New<v8::FunctionTemplate>(DoProgress)->GetFunction());
67+
}
68+
69+
NODE_MODULE(asyncprogressqueueworker, Init)
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*********************************************************************
2+
* NAN - Native Abstractions for Node.js
3+
*
4+
* Copyright (c) 2017 NAN contributors
5+
*
6+
* MIT License <https://github.com/nodejs/nan/blob/master/LICENSE.md>
7+
********************************************************************/
8+
9+
const test = require('tap').test
10+
, testRoot = require('path').resolve(__dirname, '..')
11+
, bindings = require('bindings')({ module_root: testRoot, bindings: 'asyncprogressqueueworker' });
12+
13+
test('asyncprogressqueueworker1ms', function (t) {
14+
// test with 1 ms sleep
15+
var worker = bindings.a
16+
, progressed = 0
17+
worker(1, 500, function(i) {
18+
t.ok(i === progressed, 'got the progress updates #' + i);
19+
progressed++;
20+
}, function () {
21+
t.ok(progressed === 500, 'got all progress updates')
22+
t.end()
23+
})
24+
})
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*********************************************************************
2+
* NAN - Native Abstractions for Node.js
3+
*
4+
* Copyright (c) 2017 NAN contributors
5+
*
6+
* MIT License <https://github.com/nodejs/nan/blob/master/LICENSE.md>
7+
********************************************************************/
8+
9+
const test = require('tap').test
10+
, testRoot = require('path').resolve(__dirname, '..')
11+
, bindings = require('bindings')({ module_root: testRoot, bindings: 'asyncprogressqueueworker' });
12+
13+
test('asyncprogressqueueworker', function (t) {
14+
// test with 100 ms sleep
15+
var worker = bindings.a
16+
, progressed = 0
17+
worker(100, 5, function(i) {
18+
t.ok(i === progressed, 'got the progress updates #' + i);
19+
progressed++;
20+
}, function () {
21+
t.ok(progressed === 5, 'got all progress updates')
22+
t.end()
23+
})
24+
})

0 commit comments

Comments
 (0)