Make native metrics handle creation singleton (#745)

* Make native metrics handle creation singleton
Fixes #743

* Add tests for making native metrics handle creation singleton

Co-authored-by: Sriramprabhu Sankaraguru <s.sankara@sea.samsung.com>
This commit is contained in:
Sriramprabhu Sankaraguru 2021-03-29 11:15:04 -07:00 коммит произвёл GitHub
Родитель a735789f5e
Коммит 8b5306840d
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
2 изменённых файлов: 17 добавлений и 2 удалений

Просмотреть файл

@ -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);

Просмотреть файл

@ -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);