Skip to content

Commit

Permalink
Merge pull request #2 from d3m3vilurr/last-changes
Browse files Browse the repository at this point in the history
Sync up last changes
  • Loading branch information
kibae authored Apr 5, 2019
2 parents 53a07c4 + b856a49 commit 5027e97
Show file tree
Hide file tree
Showing 15 changed files with 205 additions and 153 deletions.
18 changes: 18 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
language: node_js

os:
- linux
- osx

addons:
apt:
packages:
- valgrind

script:
- if [[ "$TRAVIS_OS_NAME" == "linux" ]]; then npm run valgrind; fi
- if [[ "$TRAVIS_OS_NAME" == "osx" ]]; then npm test; fi

node_js:
- 8
- 10
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
lru-native2
===========
[![Build Status](https://travis-ci.org/d3m3vilurr/node-lru-native.svg?branch=master)](https://travis-ci.org/d3m3vilurr/node-lru-native)

This is an implementation of a simple in-memory cache for node.js, supporting LRU (least-recently-used) eviction
and TTL expirations.
Expand Down
5 changes: 5 additions & 0 deletions binding.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@
"target_name": "native",
"sources": ["src/native.cc", "src/LRUCache.cc"],
"cflags": [ "-std=c++0x", "-O2" ],
"conditions": [
['OS=="osx"', {
"cflags": [ "-std=c++11", "-O2" ]
}]
],
"include_dirs" : [
"<!(node -e \"require('nan')\")"
]
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@
},
"devDependencies": {
"chai": "latest",
"coffee-script": "1.7.1",
"mocha": "latest",
"nan": "^2.2.0"
},
"scripts": {
"valgrind": "valgrind --leak-check=full --show-possibly-lost=no node --expose-gc --trace-gc node_modules/mocha/bin/_mocha -R spec --compilers coffee:coffee-script"
"test": "node --expose-gc --trace-gc node_modules/mocha/bin/_mocha -R spec",
"valgrind": "valgrind --leak-check=full --show-possibly-lost=no node --expose-gc --trace-gc node_modules/mocha/bin/_mocha -R spec"
}
}
2 changes: 1 addition & 1 deletion src/LRUCache.cc
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ NAN_METHOD(LRUCache::New) {
const int argc = 1;
Local<Value> argv[argc] = { info[0] };
Local<v8::Function> ctor = Nan::New<v8::Function>(constructor);
info.GetReturnValue().Set(ctor->NewInstance(argc, argv));
info.GetReturnValue().Set(Nan::NewInstance(ctor, argc, argv).ToLocalChecked());
}
}

Expand Down
5 changes: 0 additions & 5 deletions src/LRUCache.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,8 @@

using namespace v8;

#ifdef __APPLE__
#include <tr1/unordered_map>
#define unordered_map std::tr1::unordered_map
#else
#include <unordered_map>
#define unordered_map std::unordered_map
#endif

class LRUCache : public Nan::ObjectWrap {

Expand Down
57 changes: 0 additions & 57 deletions test/basics.tests.coffee

This file was deleted.

67 changes: 67 additions & 0 deletions test/basics.tests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
const assert = require('chai').assert;
const LRUCache = require('../index.js');

describe('basics', () => {
describe('a cache with default settings', () => {
let cache = undefined;
beforeEach(() => {
cache = new LRUCache();
});

it('should be able to set and retrieve primitive values', () => {
cache.set('foo', 42);
const value = cache.get('foo');
assert.equal(value, 42);
});

it('should be able to set and retrieve string values', () => {
cache.set('foo', 'bar');
const value = cache.get('foo');
assert.equal(value, 'bar');
});

it('should be able to set and retrieve object values', () => {
const obj = {example: 42}
cache.set('foo', obj);
const value = cache.get('foo');
//assert(value === obj, 'an unexpected item was returned');
});

describe('when remove() is called', () => {
it('should remove the specified item', () => {
cache.set('foo', 42);
assert.equal(cache.get('foo'), 42);
cache.remove('foo');
const value = cache.get('foo');
assert(value === undefined, `expected result to be undefined, was #{value}`);
});
});

describe('when size() is called', () => {
it('should return the correct size', () => {
assert.equal(cache.size(), 0);
cache.set('foo', 42);
assert.equal(cache.size(), 1);
});
});

describe('when clear() is called', () => {
it('should remove all items in the cache', () => {
cache.set('foo', 42);
cache.set('bar', 21);
cache.clear();
assert(cache.get('foo') === undefined);
assert(cache.get('bar') === undefined)
});
});

describe('when stats() is called', () => {
it('should return fun statistics', () => {
cache.set('foo', 42);
const stats = cache.stats();
assert(stats.size == 1, `expected stats.size to be 1, was #{stats.size}`);
});
});
});
});

37 changes: 0 additions & 37 deletions test/exceptions.tests.coffee

This file was deleted.

53 changes: 53 additions & 0 deletions test/exceptions.tests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
const assert = require('chai').assert;
const LRUCache = require('..');

describe('exceptions', () => {
describe('a cache with default settings', () => {
let cache = undefined;
beforeEach(() => {
cache = new LRUCache();
});

describe('when get() is called with no arguments', () => {
it('should throw a RangeError', () => {
assert.throw(() => cache.get(), RangeError);
});
});

describe('when get() is called with two arguments', () => {
it('should throw a RangeError', () => {
assert.throw(() => cache.get('foo', 'bar'), RangeError);
});
});

describe('when set() is called with no arguments', () => {
it('should throw a RangeError', () => {
assert.throw(() => cache.set(), RangeError);
});
});

describe('when set() is called with one argument', () => {
it('should throw a RangeError', () => {
assert.throw(() => cache.set('foo'), RangeError);
});
});

describe('when set() is called with three arguments', () => {
it('should throw a RangeError', () => {
assert.throw(() => cache.set('foo', 'bar', 'baz'), RangeError);
});
});

describe('when remove() is called with no arguments', () => {
it('should throw a RangeError', () => {
assert.throw(() => cache.remove(), RangeError);
});
});

describe('when remove() is called with two arguments', () => {
it('should throw a RangeError', () => {
assert.throw(()=> cache.remove('foo', 'bar'), RangeError);
});
});
});
});
26 changes: 0 additions & 26 deletions test/maxAge.tests.coffee

This file was deleted.

30 changes: 30 additions & 0 deletions test/maxAge.tests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
const assert = require('chai').assert;
const LRUCache = require('..');

describe('maxAge', () => {
describe('a cache with a maxAge of 100ms', () => {
let cache = undefined;
beforeEach(() => {
cache = new LRUCache({maxAge: 100});
});

describe('when a value is requested less than 100ms after it is added', () => {
it('should return the value', () => {
cache.set('foo', 42);
value = cache.get('foo');
assert.equal(value, 42);
});
});

describe('when the value is requested more than 100ms after it is added', () => {
it('should return undefined', (done) => {
cache.set('foo', 42);
setTimeout(() => {
value = cache.get('foo');
assert(value === undefined, `value was #{value}, expected undefined`);
done();
}, 200);
});
});
});
});
24 changes: 0 additions & 24 deletions test/maxElements.tests.coffee

This file was deleted.

Loading

0 comments on commit 5027e97

Please sign in to comment.