Comparing version
@@ -28,2 +28,6 @@ const GRACEFULL_CLOSE_TIMEOUT = 2000 | ||
_flush () { | ||
this.store.clear() | ||
if (this._namespace._currentContext === this) { | ||
this._namespace._currentContext = null | ||
} | ||
this.asyncIds.forEach((asyncId) => { | ||
@@ -30,0 +34,0 @@ this._namespace.contextsByAsyncId.delete(asyncId) |
@@ -50,5 +50,3 @@ const { createHook, executionAsyncId } = require('async_hooks') | ||
this.contextsByAsyncId.set(asyncId, context) | ||
// HACK: this setTimeout helps ... don't know yet why, but without it we lost the context | ||
setTimeout(() => {}, 0) | ||
this._currentContext = context | ||
return context | ||
@@ -55,0 +53,0 @@ } |
{ | ||
"name": "hello-cls", | ||
"version": "1.0.4", | ||
"version": "1.0.8", | ||
"description": "A Node.js library that implements Continuation-Local Storage", | ||
@@ -44,7 +44,7 @@ "main": "lib/cls.js", | ||
"devDependencies": { | ||
"axios": "^0.19.0", | ||
"axios": "^0.21.1", | ||
"chai": "^4.2.0", | ||
"express": "^4.17.1", | ||
"mocha": "^6.1.4", | ||
"nodemon": "^1.19.1", | ||
"mocha": "^8.2.1", | ||
"nodemon": "^2.0.7", | ||
"nyc": "^14.1.1", | ||
@@ -51,0 +51,0 @@ "standard": "^12.0.1", |
@@ -23,10 +23,15 @@ # hello-cls | ||
// I can't get a value from the namespace if one has not been set yet | ||
console.log(namespace.get('beer')) | ||
// -> undefined | ||
// I can set a value to a given key on the namespace | ||
namespace.set('beer', 🍺) | ||
namespace.set('beer', '🍺') | ||
// I can get a value from the namespace | ||
console.log(namespace.get('beer')) | ||
// -> 🍺 | ||
// -> '🍺' | ||
context.close() | ||
// close with 'true' will flush state immediately | ||
context.close(true) | ||
@@ -33,0 +38,0 @@ // I can't get a value from the namespace if the context is closed |
@@ -6,12 +6,14 @@ /* eslint-env mocha */ | ||
const { Context } = require('../../lib/context') | ||
const { describe, beforeEach, it } = require('mocha') | ||
describe('Context', () => { | ||
let namespace, mockAsyncId | ||
beforeEach(() => { | ||
this.namespace = new Namespace('cookie') | ||
this.mockAsyncId = 1 | ||
namespace = new Namespace('cookie') | ||
mockAsyncId = 1 | ||
}) | ||
describe('Static', () => { | ||
it(`should construct an instance`, () => { | ||
const context = new Context(this.namespace, this.mockAsyncId) | ||
const context = new Context(namespace, mockAsyncId) | ||
expect(context).to.be.an.instanceOf(Context) | ||
@@ -22,26 +24,81 @@ }) | ||
describe('Instance', () => { | ||
let context | ||
beforeEach(() => { | ||
this.context = new Context(this.namespace, this.mockAsyncId) | ||
context = new Context(namespace, mockAsyncId) | ||
}) | ||
it(`should have a private property _namespace`, () => { | ||
expect(this.context).to.have.a.property('_namespace').that.equals(this.namespace) | ||
expect(context).to.have.a.property('_namespace').that.equals(namespace) | ||
}) | ||
it(`should have a public property asyncIds`, () => { | ||
expect(this.context).to.have.a.property('asyncIds').that.is.an.instanceOf(Set) | ||
expect(this.context.asyncIds.size).to.equal(1) | ||
expect(this.context.asyncIds.has(this.mockAsyncId)).to.be.true | ||
expect(context).to.have.a.property('asyncIds').that.is.an.instanceOf(Set) | ||
expect(context.asyncIds.size).to.equal(1) | ||
expect(context.asyncIds.has(mockAsyncId)).to.be.true | ||
}) | ||
it(`should have a public property store`, () => { | ||
expect(this.context).to.have.a.property('store').that.is.an.instanceOf(Map) | ||
expect(this.context.store.size).to.equal(0) | ||
expect(context).to.have.a.property('store').that.is.an.instanceOf(Map) | ||
expect(context.store.size).to.equal(0) | ||
}) | ||
it(`should have a public method close`, () => { | ||
expect(this.context).to.have.a.property('close').that.is.an.instanceOf(Function) | ||
expect(context).to.have.a.property('close').that.is.an.instanceOf(Function) | ||
// TODO: test it further | ||
}) | ||
function contextClosedWithFlush () { | ||
context.close(true) | ||
} | ||
describe('has values stored', () => { | ||
let testName, testValue | ||
beforeEach(() => { | ||
testName = 'testName' | ||
testValue = 'testValue' | ||
context.store.set(testName, testValue) | ||
}) | ||
describe('and is closed with flush', () => { | ||
beforeEach(contextClosedWithFlush) | ||
it(`the store map should be empty`, () => { | ||
expect(context.store.size).to.equal(0) | ||
}) | ||
}) | ||
}) | ||
describe('is the current context in namespace', () => { | ||
beforeEach(() => { | ||
namespace._currentContext = context | ||
}) | ||
describe('and is closed with flush', () => { | ||
beforeEach(contextClosedWithFlush) | ||
it(`should no longer be set as current namespace context`, () => { | ||
expect(namespace._currentContext).to.be.null | ||
}) | ||
}) | ||
}) | ||
describe('is not the current context in namespace', () => { | ||
let otherContext | ||
beforeEach(() => { | ||
let otherMockAsyncId = 2 | ||
otherContext = new Context(namespace, otherMockAsyncId) | ||
namespace._currentContext = otherContext | ||
}) | ||
describe('and is closed with flush', () => { | ||
beforeEach(contextClosedWithFlush) | ||
it(`the current namespace context should be the same`, () => { | ||
expect(namespace._currentContext).to.equal(otherContext) | ||
}) | ||
}) | ||
}) | ||
}) | ||
}) |
@@ -85,3 +85,14 @@ /* eslint-env mocha */ | ||
}) | ||
describe('when initContext is executed', () => { | ||
let context | ||
beforeEach(() => { | ||
context = this.namespace.initContext() | ||
}) | ||
it('then the resulting context should be set as currentContext', () => { | ||
expect(this.namespace._currentContext).to.equal(context) | ||
}) | ||
}) | ||
}) | ||
}) |
22284
34.26%19
18.75%526
38.06%43
13.16%