Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ language: node_js
node_js:
- 0.8
- 0.10
- 0.11

notifications:
email:
Expand Down
1 change: 1 addition & 0 deletions binding.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
{
'target_name': 'toobusy',
'include_dirs': [
"<!(node -e \"require('nan')\")"
],
'sources': [
'toobusy.cc',
Expand Down
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@
"homepage": "https://github.com/lloyd/node-toobusy",
"version": "0.2.4",
"dependencies": {
"bindings": "1.1.0"
"bindings": "1.2.0",
"nan": "1.2.0"
},
"devDependencies": {
"should": "1.2.1",
"mocha": "1.7.0"
"should": "4.0.1",
"mocha": "1.20.1"
},
"maintainers": [
{
Expand Down
11 changes: 6 additions & 5 deletions tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ should = require('should'),
toobusy = require('./');

describe('the library', function() {
it('should export a couple functions', function(done) {
(toobusy).should.be.a('function');
(toobusy.maxLag).should.be.a('function');
(toobusy.shutdown).should.be.a('function');
it('should export functions', function(done) {
(toobusy).should.be.a.Function;
(toobusy.lag).should.be.a.Function;
(toobusy.maxLag).should.be.a.Function;
(toobusy.shutdown).should.be.a.Function;
done();
});
});
Expand All @@ -33,7 +34,7 @@ describe('toobusy()', function() {
function load() {
if (toobusy()) return done();
var start = new Date();
while ((new Date() - start) < 250) {
while ((new Date() - start) < 300) {
for (var i = 0; i < 1e5;) i++;
}
setTimeout(load, 0);
Expand Down
46 changes: 24 additions & 22 deletions toobusy.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#else
#include <sys/time.h>
#endif
#include "nan.h"

using namespace v8;

Expand All @@ -22,7 +23,8 @@ static uv_timer_t s_timer;
static uint32_t s_currentLag;
static uint64_t s_lastMark;

Handle<Value> TooBusy(const Arguments& args) {
NAN_METHOD(TooBusy) {
NanScope();
// No HandleScope required, because this function allocates no
// v8 classes that reside on the heap.
bool block = false;
Expand All @@ -34,44 +36,40 @@ Handle<Value> TooBusy(const Arguments& args) {
double r = (rand() / (double) RAND_MAX) * 100.0;
if (r < pctToBlock) block = true;
}
return block ? True() : False();
NanReturnValue(block ? NanTrue() : NanFalse());
}

Handle<Value> 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();
NanReturnUndefined();
}

Handle<Value> Lag(const Arguments& args) {
HandleScope scope;
return scope.Close(Integer::New(s_currentLag));
NAN_METHOD(Lag) {
NanScope();
NanReturnValue(NanNew<Integer>(s_currentLag));
}

Handle<Value> HighWaterMark(const Arguments& args) {
HandleScope scope;
NAN_METHOD(HighWaterMark) {
NanScope();

if (args.Length() >= 1) {
if (!args[0]->IsNumber()) {
return v8::ThrowException(
v8::Exception::Error(
v8::String::New("expected numeric first argument")));
return NanThrowError("expected numeric first argument");
}
int hwm = args[0]->Int32Value();
if (hwm < 10) {
return v8::ThrowException(
v8::Exception::Error(
v8::String::New("maximum lag should be greater than 10ms")));
return NanThrowError("maximum lag should be greater than 10ms");
}
HIGH_WATER_MARK_MS = hwm;
}

return scope.Close(Number::New(HIGH_WATER_MARK_MS));
NanReturnValue(NanNew<Number>(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();

Expand All @@ -85,13 +83,17 @@ static void every_second(uv_timer_t* handle, int status)
s_lastMark = now;
};

static void every_second(uv_timer_t* handle, int status)
{
every_second(handle);
}

extern "C" void init(Handle<Object> target) {
HandleScope scope;

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());
target->Set(NanNew<String>("toobusy"), NanNew<FunctionTemplate>(TooBusy)->GetFunction());
target->Set(NanNew<String>("shutdown"), NanNew<FunctionTemplate>(ShutDown)->GetFunction());
target->Set(NanNew<String>("lag"), NanNew<FunctionTemplate>(Lag)->GetFunction());
target->Set(NanNew<String>("maxLag"), NanNew<FunctionTemplate>(HighWaterMark)->GetFunction());
uv_timer_init(uv_default_loop(), &s_timer);
uv_timer_start(&s_timer, every_second, POLL_PERIOD_MS, POLL_PERIOD_MS);
};
Expand Down