diff --git a/AutoCollection/NativePerformance.ts b/AutoCollection/NativePerformance.ts index 25a750143..a66c87e6f 100644 --- a/AutoCollection/NativePerformance.ts +++ b/AutoCollection/NativePerformance.ts @@ -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); diff --git a/Tests/AutoCollection/NativePerformance.tests.ts b/Tests/AutoCollection/NativePerformance.tests.ts index b5b403b1b..764909032 100644 --- a/Tests/AutoCollection/NativePerformance.tests.ts +++ b/Tests/AutoCollection/NativePerformance.tests.ts @@ -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);