Skip to content

Commit

Permalink
First Unit test complete for GenericSubscriptionWrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
Markus Klemm committed Sep 22, 2020
1 parent 83abae4 commit 5b77a85
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 12 deletions.
6 changes: 3 additions & 3 deletions napi.h
Original file line number Diff line number Diff line change
Expand Up @@ -2501,10 +2501,10 @@ class GenericSubscriptionWrapper {
private:
struct internal : std::enable_shared_from_this<internal> {
Napi::Promise::Deferred deferred_;
Napi::ThreadSafeFunction function_;
conversion_function_t conversion_function_;
Napi::Reference<Napi::Function> js_function_;
Napi::ThreadSafeFunction function_;
Napi::Function conversion_wrapper_;
conversion_function_t conversion_function_;
std::shared_future<T> result_;
unsubscribe_function_t unsubscribe_function_;

Expand All @@ -2527,7 +2527,7 @@ class GenericSubscriptionWrapper {
"Generic subscription wrapper", 1, 1);

auto unsubscribe_call = Napi::Function::New(deferred_.Env(),
[me = this->shared_from_this()](const auto &info) {
[me = this->shared_from_this()](const auto &) {
me->unsubscribe_function_();
me->tear_down();
});
Expand Down
29 changes: 27 additions & 2 deletions test/generic_subscription_wrapper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,41 @@
#include <future>
#include <utility>
#include <functional>
#include <atomic>

#if (NAPI_VERSION > 3)

#ifdef NAPI_CPP_EXCEPTIONS

namespace {

Napi::Value SubScribeFor10Calls(const Napi::CallbackInfo&) {
Napi::Value convert_int(const Napi::Env &env, const std::shared_future<std::uint32_t> & future) {
return Napi::Number::From(env, future.get());
}

Napi::Value SubScribeFor10Calls(const Napi::CallbackInfo& info) {
Napi::Promise::Deferred deferred = Napi::Promise::Deferred::New(info.Env());

auto wrapper = Napi::GenericSubscriptionWrapper<std::uint32_t>(
deferred, &convert_int, info[0].As<Napi::Function>());


auto t = std::thread([wrapper = std::move(wrapper)]() mutable {
std::atomic<bool> run{true};
wrapper.set_unsubscription_function([&run]() {
run = false;
});
for (std::uint32_t counter{}; run; ++counter) {
auto future = std::async(std::launch::deferred, [counter]() {
return counter;
});
wrapper.get_native_callback()(future.share());
std::this_thread::sleep_for(std::chrono::milliseconds(20));
}
});
t.detach();

return {};
return deferred.Promise();
}


Expand Down
38 changes: 31 additions & 7 deletions test/generic_subscription_wrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,39 @@ const buildType = process.config.target_defaults.default_configuration;

module.exports = test(require(`./build/${buildType}/binding.node`));

async function testNormal10TimesCallAndUnsubscribe(bindingFunction) {
class Handler {
constructor(finish) {
this.countsLeft = 10;
this.callCounter = 0;
this.finish = finish;
}
iteration(input) {
++this.callCounter;
if (--this.countsLeft === 0) {
this.finish();
}
}
}

const functionToTest = bindingFunction.generic_subscription_wrapper.SubScribeFor10Calls;

let handler = null; let unsub = null;
let p = new Promise(resolve => {
handler = new Handler(() => resolve());
unsub = functionToTest((input) => handler.iteration(input))
});

await p;
await unsub.then(u => (u()));


assert.equal(handler.callCounter, 10);
}

async function test(binding) {
const tests = [
async function testNormal10TimesCallAndUnsubscribe(bindingFunction) {
const functionToTest = bindingFunction.generic_subscription_wrapper.SubScribeFor10Calls;
assert.doesNotReject(functionToTest(5));
const result = await functionToTest(5);
assert.equal(result.value, 125);
assert.equal(result.error, undefined);
},
testNormal10TimesCallAndUnsubscribe,
];

for (let f of tests) {
Expand Down

0 comments on commit 5b77a85

Please sign in to comment.