From 461dc4ba9ed486b41aa83e804efacb55fccd29ab Mon Sep 17 00:00:00 2001 From: Marc Pichler Date: Mon, 6 Feb 2023 22:55:01 +0100 Subject: [PATCH] fix(sdk-metrics): fix flaky LastValueAggregator test by using fake timer (#3587) * fix(sdk-metrics): fix flaky LastValueAggregator test by using fake timers * fix(changelog): add changelog entry. * fix(sdk-metrics): change LastValueAggregation timer increment to 100ms --- CHANGELOG.md | 2 ++ .../test/aggregator/LastValue.test.ts | 23 ++++++++++++++----- 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 33c3c17e8..8184b771a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -19,6 +19,8 @@ For experimental package changes, see the [experimental CHANGELOG](experimental/ ### :house: (Internal) +* fix(sdk-metrics): ix flaky LastValueAggregator test by using fake timer [#3587](https://github.com/open-telemetry/opentelemetry-js/pull/3587) @pichlermarc + ## 1.9.1 ### :bug: (Bug Fix) diff --git a/packages/sdk-metrics/test/aggregator/LastValue.test.ts b/packages/sdk-metrics/test/aggregator/LastValue.test.ts index 206b4c4ac..2fd35156c 100644 --- a/packages/sdk-metrics/test/aggregator/LastValue.test.ts +++ b/packages/sdk-metrics/test/aggregator/LastValue.test.ts @@ -16,15 +16,26 @@ import { HrTime } from '@opentelemetry/api'; import * as assert from 'assert'; +import * as sinon from 'sinon'; import { AggregationTemporality } from '../../src'; import { LastValueAccumulation, LastValueAggregator, } from '../../src/aggregator'; import { MetricData, DataPointType } from '../../src/export/MetricData'; -import { commonValues, defaultInstrumentDescriptor, sleep } from '../util'; +import { commonValues, defaultInstrumentDescriptor } from '../util'; describe('LastValueAggregator', () => { + let clock: sinon.SinonFakeTimers; + + beforeEach(() => { + clock = sinon.useFakeTimers(); + }); + + afterEach(() => { + sinon.restore(); + }); + describe('createAccumulation', () => { it('no exceptions on createAccumulation', () => { const aggregator = new LastValueAggregator(); @@ -47,16 +58,16 @@ describe('LastValueAggregator', () => { assert.deepStrictEqual(aggregator.merge(prev, delta), expected); }); - it('return the newly sampled accumulation', async () => { + it('return the newly sampled accumulation', () => { const aggregator = new LastValueAggregator(); const accumulation1 = aggregator.createAccumulation([0, 0]); const accumulation2 = aggregator.createAccumulation([1, 1]); accumulation1.record(2); - await sleep(1); + clock.tick(100); accumulation2.record(3); // refresh the accumulation1 - await sleep(1); + clock.tick(100); accumulation1.record(4); assert.deepStrictEqual( @@ -92,7 +103,7 @@ describe('LastValueAggregator', () => { assert.deepStrictEqual(aggregator.diff(prev, curr), expected); }); - it('return the newly sampled accumulation', async () => { + it('return the newly sampled accumulation', () => { const aggregator = new LastValueAggregator(); const accumulation1 = aggregator.createAccumulation([0, 0]); const accumulation2 = aggregator.createAccumulation([1, 1]); @@ -100,7 +111,7 @@ describe('LastValueAggregator', () => { accumulation1.record(2); accumulation2.record(3); // refresh the accumulation1 - await sleep(1); + clock.tick(100); accumulation1.record(4); assert.deepStrictEqual(