From 339ac72c1100ea5e33be7ed7a8eeeb4866e6944a Mon Sep 17 00:00:00 2001 From: Jonas Dohse Date: Thu, 12 Feb 2015 14:09:50 +0000 Subject: [PATCH 1/3] Increase test run time --- tests.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests.js b/tests.js index bcaabff..23d8d8c 100644 --- a/tests.js +++ b/tests.js @@ -30,10 +30,11 @@ describe('maxLag', function() { describe('toobusy()', function() { it('should return true after a little load', function(done) { + this.timeout(5000); function load() { if (toobusy()) return done(); var start = new Date(); - while ((new Date() - start) < 250) { + while ((new Date() - start) < 500) { for (var i = 0; i < 1e5;) i++; } setTimeout(load, 0); From 586c1da186fd602032811085bdee674d5a9eedee Mon Sep 17 00:00:00 2001 From: Jonas Dohse Date: Wed, 11 Feb 2015 23:44:53 +0000 Subject: [PATCH 2/3] Use nan 2.0 to support all node version till 4.0 --- .travis.yml | 6 ++++++ binding.gyp | 1 + package.json | 3 ++- toobusy.cc | 57 +++++++++++++++++++++++++++------------------------- 4 files changed, 39 insertions(+), 28 deletions(-) diff --git a/.travis.yml b/.travis.yml index cf7fe07..552ae14 100644 --- a/.travis.yml +++ b/.travis.yml @@ -3,6 +3,12 @@ language: node_js node_js: - 0.8 - 0.10 + - 0.12 + +matrix: + include: + - node_js: 4 + env: CC=clang CXX=clang++ notifications: email: diff --git a/binding.gyp b/binding.gyp index 83e219d..8046e8f 100644 --- a/binding.gyp +++ b/binding.gyp @@ -3,6 +3,7 @@ { 'target_name': 'toobusy', 'include_dirs': [ + " +#include #include #include #include @@ -22,7 +23,8 @@ static uv_timer_t s_timer; static uint32_t s_currentLag; static uint64_t s_lastMark; -Handle TooBusy(const Arguments& args) { +NAN_METHOD(TooBusy) { + Nan::HandleScope scope; // No HandleScope required, because this function allocates no // v8 classes that reside on the heap. bool block = false; @@ -34,44 +36,40 @@ Handle TooBusy(const Arguments& args) { double r = (rand() / (double) RAND_MAX) * 100.0; if (r < pctToBlock) block = true; } - return block ? True() : False(); + info.GetReturnValue().Set(Nan::New(block)); } -Handle ShutDown(const Arguments& args) { +NAN_METHOD(ShutDown) { // No HandleScope required, because this function allocates no // v8 classes that reside on the heap. uv_timer_stop(&s_timer); - return Undefined(); } -Handle Lag(const Arguments& args) { - HandleScope scope; - return scope.Close(Integer::New(s_currentLag)); +NAN_METHOD(Lag) { + Nan::HandleScope scope; + info.GetReturnValue().Set(Nan::New(s_currentLag)); } -Handle HighWaterMark(const Arguments& args) { - HandleScope scope; - - if (args.Length() >= 1) { - if (!args[0]->IsNumber()) { - return v8::ThrowException( - v8::Exception::Error( - v8::String::New("expected numeric first argument"))); +NAN_METHOD(HighWaterMark) { + Nan::HandleScope scope; + if (info.Length() >= 1) { + if (!info[0]->IsNumber()) { + Nan::ThrowError("expected numeric first argument"); + return; } - int hwm = args[0]->Int32Value(); + int hwm = info[0]->Int32Value(); if (hwm < 10) { - return v8::ThrowException( - v8::Exception::Error( - v8::String::New("maximum lag should be greater than 10ms"))); + Nan::ThrowError("maximum lag should be greater than 10ms"); + return; } HIGH_WATER_MARK_MS = hwm; } - return scope.Close(Number::New(HIGH_WATER_MARK_MS)); + info.GetReturnValue().Set(Nan::New(HIGH_WATER_MARK_MS)); } -static void every_second(uv_timer_t* handle, int status) +static void every_second(uv_timer_t* handle) { uint64_t now = uv_hrtime(); @@ -85,13 +83,18 @@ static void every_second(uv_timer_t* handle, int status) s_lastMark = now; }; -extern "C" void init(Handle target) { - HandleScope scope; +__attribute__((unused)) static void every_second(uv_timer_t* handle, int) { + every_second(handle); +} + +extern "C" NAN_MODULE_INIT(init) { + Nan::HandleScope scope; + + Nan::SetMethod(target, "toobusy", TooBusy); + Nan::SetMethod(target, "shutdown", ShutDown); + Nan::SetMethod(target, "lag", Lag); + Nan::SetMethod(target, "maxLag", HighWaterMark); - target->Set(String::New("toobusy"), FunctionTemplate::New(TooBusy)->GetFunction()); - target->Set(String::New("shutdown"), FunctionTemplate::New(ShutDown)->GetFunction()); - target->Set(String::New("lag"), FunctionTemplate::New(Lag)->GetFunction()); - target->Set(String::New("maxLag"), FunctionTemplate::New(HighWaterMark)->GetFunction()); uv_timer_init(uv_default_loop(), &s_timer); uv_timer_start(&s_timer, every_second, POLL_PERIOD_MS, POLL_PERIOD_MS); }; From 12f8e25f69d08f811f489529c5650eaa3c6df7a0 Mon Sep 17 00:00:00 2001 From: Jonas Dohse Date: Mon, 23 Nov 2015 21:00:14 +0100 Subject: [PATCH 3/3] fixup! Increase test run time --- .travis.yml | 8 ++++---- tests.js | 8 +++----- 2 files changed, 7 insertions(+), 9 deletions(-) diff --git a/.travis.yml b/.travis.yml index 552ae14..776c8bc 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,13 +1,13 @@ language: node_js node_js: - - 0.8 - - 0.10 - - 0.12 + - '0.8' + - '0.10' + - '0.12' matrix: include: - - node_js: 4 + - node_js: '4' env: CC=clang CXX=clang++ notifications: diff --git a/tests.js b/tests.js index 23d8d8c..a44e3b1 100644 --- a/tests.js +++ b/tests.js @@ -30,13 +30,11 @@ describe('maxLag', function() { describe('toobusy()', function() { it('should return true after a little load', function(done) { - this.timeout(5000); + toobusy.maxLag(50); function load() { - if (toobusy()) return done(); + if (toobusy()) return done(null); var start = new Date(); - while ((new Date() - start) < 500) { - for (var i = 0; i < 1e5;) i++; - } + while ((new Date() - start) < 100) {} setTimeout(load, 0); } load();