Skip to content

Commit

Permalink
fix(context store): trace error for enable / disable twice
Browse files Browse the repository at this point in the history
  • Loading branch information
winguse committed Apr 2, 2018
1 parent 7dafabf commit 216ed1d
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 4 deletions.
24 changes: 20 additions & 4 deletions src/envoy-context-store.ts
Expand Up @@ -31,6 +31,7 @@ class NodeInfo {
}

const store = new Map<number, NodeInfo>();
let enabled = false;

/**
* clean up will decrease the reference count.
Expand Down Expand Up @@ -72,7 +73,12 @@ const asyncHook = asyncHooks.createHook({
* i.e. put it in your application's start.
*/
function enable() {
asyncHook.enable();
if (!enabled) {
asyncHook.enable();
enabled = true;
} else {
console.trace("[envoy-node] You want to enable the enabled store");
}
}

/**
Expand All @@ -81,8 +87,13 @@ function enable() {
* This function is not intended to be call in the application life cycle.
*/
function disable() {
asyncHook.disable();
store.clear();
if (enabled) {
asyncHook.disable();
store.clear();
enabled = false;
} else {
console.trace("[envoy-node] You want to disable the disabled store");
}
}

function markContext(triggerAsyncId: number, context: EnvoyContext) {
Expand Down Expand Up @@ -143,9 +154,14 @@ function get(): EnvoyContext | undefined {
return getContext(asyncId);
}

function isEnabled() {
return enabled;
}

export default {
enable,
disable,
set,
get
get,
isEnabled
};
14 changes: 14 additions & 0 deletions test/envoy-context-store.test.ts
Expand Up @@ -55,6 +55,7 @@ describe("Envoy context store", () => {
return data;
});
store.enable();
expect(store.isEnabled()).toBeTruthy();
for (const data of testData) {
await root(data);
}
Expand Down Expand Up @@ -82,4 +83,17 @@ describe("Envoy context store", () => {
expect(console.trace).toBeCalled();
console.trace = originalTrace;
});

it("should trace error when enable / disable twice", () => {
const originalTrace = console.trace;
console.trace = jest.fn();
store.enable();
store.enable();
expect(console.trace).toBeCalled();
console.trace = jest.fn();
store.disable();
store.disable();
expect(console.trace).toBeCalled();
console.trace = originalTrace;
});
});

0 comments on commit 216ed1d

Please sign in to comment.