Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make native metrics handle creation singleton #745

Merged
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
6 changes: 4 additions & 2 deletions AutoCollection/NativePerformance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,10 @@ export class AutoCollectNativePerformance {
if (this._isEnabled && AutoCollectNativePerformance._emitter) {
// enable self
AutoCollectNativePerformance._emitter.enable(true, collectionInterval);
this._handle = setInterval(() => this._trackNativeMetrics(), collectionInterval);
this._handle.unref();
if (!this._handle) {
this._handle = setInterval(() => this._trackNativeMetrics(), collectionInterval);
this._handle.unref();
}
} else if (AutoCollectNativePerformance._emitter) {
// disable self
AutoCollectNativePerformance._emitter.enable(false);
Expand Down
13 changes: 13 additions & 0 deletions Tests/AutoCollection/NativePerformance.tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,19 @@ describe("AutoCollection/NativePerformance", () => {
assert.ok(disposeSpy.calledOnce, "dispose is called when second instance is constructed");
});

it("Calling enable multiple times shoud not create multiple timers", () => {
var client = new TelemetryClient("1aa11111-bbbb-1ccc-8ddd-eeeeffff3333");
var native = new AutoCollectNativePerformance(client);
var setIntervalSpy = sinon.spy(global, "setInterval");

assert.ok(native);
assert.doesNotThrow(() => native.enable(true), "Does not throw when tryinig to enable");
assert.doesNotThrow(() => native.enable(true), "Does not throw when trying to enable");
assert.equal(setIntervalSpy.callCount, 1, "setInterval should be singleton");

setIntervalSpy.restore();
});

it("Calling enable when metrics are not available should fail gracefully", () => {
var client = new TelemetryClient("1aa11111-bbbb-1ccc-8ddd-eeeeffff3333");
var native = new AutoCollectNativePerformance(client);
Expand Down