-
Notifications
You must be signed in to change notification settings - Fork 825
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(scope-manager): Add no-op implementations of ScopeManager
- Loading branch information
Showing
5 changed files
with
141 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/** | ||
* Copyright 2019, OpenTelemetry Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import * as types from './types'; | ||
|
||
export class NoopScopeManager implements types.ScopeManager { | ||
active(): unknown { | ||
return null; | ||
} | ||
|
||
with<T extends (...args: unknown[]) => ReturnType<T>>( | ||
scope: unknown, | ||
fn: T | ||
): ReturnType<T> { | ||
return fn(); | ||
} | ||
|
||
bind<T>(target: T, scope?: unknown): T { | ||
return target; | ||
} | ||
|
||
enable(): this { | ||
return this; | ||
} | ||
|
||
disable(): this { | ||
return this; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,3 +15,4 @@ | |
*/ | ||
|
||
export * from './types'; | ||
export * from './NoopScopeManager'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
92 changes: 92 additions & 0 deletions
92
packages/opentelemetry-scope-base/test/noop/NoopScopeManager.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
/** | ||
* Copyright 2019, OpenTelemetry Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import * as assert from 'assert'; | ||
import { NoopScopeManager } from '../../src'; | ||
|
||
describe('NoopScopeManager', () => { | ||
let scopeManager: NoopScopeManager; | ||
|
||
describe('.enable()', () => { | ||
it('should works', () => { | ||
assert.doesNotThrow(() => { | ||
scopeManager = new NoopScopeManager(); | ||
assert(scopeManager.enable() === scopeManager, 'should return this'); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('.disable()', () => { | ||
it('should works', () => { | ||
assert.doesNotThrow(() => { | ||
assert(scopeManager.disable() === scopeManager, 'should return this'); | ||
}); | ||
scopeManager.enable(); | ||
}); | ||
}); | ||
|
||
describe('.with()', () => { | ||
it('should run the callback (null as target)', done => { | ||
scopeManager.with(null, done); | ||
}); | ||
|
||
it('should run the callback (object as target)', done => { | ||
const test = { a: 1 }; | ||
scopeManager.with(test, () => { | ||
assert.strictEqual( | ||
scopeManager.active(), | ||
null, | ||
'should not have scope' | ||
); | ||
return done(); | ||
}); | ||
}); | ||
|
||
it('should run the callback (when disabled)', done => { | ||
scopeManager.disable(); | ||
scopeManager.with(null, () => { | ||
scopeManager.enable(); | ||
return done(); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('.active()', () => { | ||
it('should always return null (when enabled)', () => { | ||
assert.strictEqual(scopeManager.active(), null, 'should not have scope'); | ||
}); | ||
|
||
it('should always return null (when disabled)', () => { | ||
scopeManager.disable(); | ||
assert.strictEqual(scopeManager.active(), null, 'should not have scope'); | ||
scopeManager.enable(); | ||
}); | ||
}); | ||
|
||
describe('.bind()', () => { | ||
it('should return the same target (when enabled)', () => { | ||
const test = { a: 1 }; | ||
assert.deepStrictEqual(scopeManager.bind(test), test); | ||
}); | ||
|
||
it('should return the same target (when disabled)', () => { | ||
scopeManager.disable(); | ||
const test = { a: 1 }; | ||
assert.deepStrictEqual(scopeManager.bind(test), test); | ||
scopeManager.enable(); | ||
}); | ||
}); | ||
}); |