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

test(sdk-metrics): browser compatibility tests #2709

Merged
merged 3 commits into from
Jan 18, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*!
* Copyright The 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.
*/

const karmaWebpackConfig = require('../../../karma.webpack');
const karmaBaseConfig = require('../../../karma.base');

module.exports = (config) => {
config.set(Object.assign({}, karmaBaseConfig, {
webpack: karmaWebpackConfig
}))
};
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
"compile": "tsc --build tsconfig.all.json",
"clean": "tsc --build --clean tsconfig.all.json",
"test": "nyc ts-mocha -p tsconfig.json 'test/**/*.test.ts'",
"test:browser": "nyc karma start --single-run",
"tdd": "npm run test -- --watch-extensions ts --watch",
"tdd:browser": "karma start",
"codecov": "nyc report --reporter=json && codecov -f coverage/*.json -p ../../",
"lint": "eslint . --ext .ts",
"lint:fix": "eslint . --ext .ts --fix",
Expand Down Expand Up @@ -57,6 +59,12 @@
"@types/node": "14.17.33",
"@types/sinon": "10.0.6",
"codecov": "3.8.3",
"karma": "6.3.8",
"karma-chrome-launcher": "3.1.0",
"karma-coverage-istanbul-reporter": "3.0.3",
"karma-mocha": "2.0.1",
"karma-spec-reporter": "0.0.32",
"karma-webpack": "4.0.2",
"mocha": "7.2.0",
"nyc": "15.1.0",
"rimraf": "3.0.2",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import * as assert from 'assert';
import * as sinon from 'sinon';
import { MetricProducer } from '../../src/export/MetricProducer';
import { TimeoutError } from '../../src/utils';
import { assertRejects } from '../test-utils';

const MAX_32_BIT_INT = 2 ** 31 - 1;

Expand Down Expand Up @@ -102,7 +103,7 @@ describe('PeriodicExportingMetricReader', () => {
exporter: exporter,
exportIntervalMillis: 0,
exportTimeoutMillis: 0
}), new Error('exportIntervalMillis must be greater than 0'));
}), /exportIntervalMillis must be greater than 0/);
});

it('should throw when timeout less or equal to 0', () => {
Expand All @@ -111,7 +112,7 @@ describe('PeriodicExportingMetricReader', () => {
exporter: exporter,
exportIntervalMillis: 1,
exportTimeoutMillis: 0
}), new Error('exportTimeoutMillis must be greater than 0'));
}), /exportTimeoutMillis must be greater than 0/);
});

it('should throw when timeout less or equal to interval', () => {
Expand All @@ -120,7 +121,7 @@ describe('PeriodicExportingMetricReader', () => {
exporter: exporter,
exportIntervalMillis: 100,
exportTimeoutMillis: 200
}), new Error('exportIntervalMillis must be greater than or equal to exportTimeoutMillis'));
}), /exportIntervalMillis must be greater than or equal to exportTimeoutMillis/);
});

it('should not start exporting', async () => {
Expand Down Expand Up @@ -151,7 +152,7 @@ describe('PeriodicExportingMetricReader', () => {
reader.setMetricProducer(new TestMetricProducer());
const result = await exporter.waitForNumberOfExports(2);

assert.deepEqual(result, [[], []]);
assert.deepStrictEqual(result, [[], []]);
await reader.shutdown();
});
});
Expand All @@ -169,7 +170,7 @@ describe('PeriodicExportingMetricReader', () => {
reader.setMetricProducer(new TestMetricProducer());

const result = await exporter.waitForNumberOfExports(2);
assert.deepEqual(result, [[], []]);
assert.deepStrictEqual(result, [[], []]);

exporter.throwException = false;
await reader.shutdown();
Expand All @@ -188,7 +189,7 @@ describe('PeriodicExportingMetricReader', () => {
reader.setMetricProducer(new TestMetricProducer());

const result = await exporter.waitForNumberOfExports(2);
assert.deepEqual(result, [[], []]);
assert.deepStrictEqual(result, [[], []]);

exporter.throwException = false;
await reader.shutdown();
Expand Down Expand Up @@ -227,7 +228,7 @@ describe('PeriodicExportingMetricReader', () => {
});

reader.setMetricProducer(new TestMetricProducer());
await assert.rejects(() => reader.forceFlush({ timeoutMillis: 20 }),
await assertRejects(() => reader.forceFlush({ timeoutMillis: 20 }),
TimeoutError);
await reader.shutdown();
});
Expand All @@ -241,7 +242,7 @@ describe('PeriodicExportingMetricReader', () => {
exportTimeoutMillis: 80,
});

await assert.rejects(() => reader.forceFlush());
await assertRejects(() => reader.forceFlush(), /Error during forceFlush/);
});

it('should not forceFlush exporter after shutdown', async () => {
Expand Down Expand Up @@ -294,7 +295,7 @@ describe('PeriodicExportingMetricReader', () => {
});

reader.setMetricProducer(new TestMetricProducer());
await assert.rejects(() => reader.shutdown({ timeoutMillis: 20 }),
await assertRejects(() => reader.shutdown({ timeoutMillis: 20 }),
TimeoutError);
});

Expand Down Expand Up @@ -326,7 +327,7 @@ describe('PeriodicExportingMetricReader', () => {
exportTimeoutMillis: 80,
});

await assert.rejects(() => reader.shutdown());
await assertRejects(() => reader.shutdown(), /Error during forceFlush/);
});
})
;
Expand All @@ -340,7 +341,7 @@ describe('PeriodicExportingMetricReader', () => {
exportTimeoutMillis: 80,
});

await assert.rejects(() => reader.collect());
await assertRejects(() => reader.collect(), /MetricReader is not bound to a MetricProducer/);
});

it('should return empty on shut-down instance', async () => {
Expand All @@ -354,7 +355,7 @@ describe('PeriodicExportingMetricReader', () => {
reader.setMetricProducer(new TestMetricProducer());

await reader.shutdown();
assert.deepEqual([], await reader.collect());
assert.deepStrictEqual([], await reader.collect());
});

it('should time out when timeoutMillis is set', async () => {
Expand All @@ -368,7 +369,7 @@ describe('PeriodicExportingMetricReader', () => {
producer.collectionTime = 40;
reader.setMetricProducer(producer);

await assert.rejects(
await assertRejects(
() => reader.collect({ timeoutMillis: 20 }),
TimeoutError
);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* Copyright The 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
*
* https://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.
*/

const testsContext = require.context('.', true, /test$/);
testsContext.keys().forEach(testsContext);

const srcContext = require.context('.', true, /src$/);
srcContext.keys().forEach(srcContext);
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Copyright The 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
*
* https://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.
*/

/**
* Changes to this file should be applied to opentelemetry-core/test/test-utils.ts too.
*/

import * as assert from 'assert';

interface ErrorLikeConstructor {
new(): Error;
}

/**
* Node.js v8.x and browser compatible `assert.rejects`.
*/
export async function assertRejects(actual: any, expected: RegExp | ErrorLikeConstructor) {
let rejected;
try {
if (typeof actual === 'function') {
await actual();
} else {
await actual;
}
} catch (err) {
rejected = true;
assert.throws(() => {
throw err;
}, expected);
}
assert(rejected, 'Promise not rejected');
}
17 changes: 14 additions & 3 deletions packages/opentelemetry-core/test/test-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,26 @@

import * as assert from 'assert';

interface ErrorLikeConstructor {
new(): Error;
}

/**
* Node.js v8.x and browser compatible `assert.rejects`.
*/
export async function assertRejects(promise: any, expect: any) {
export async function assertRejects(actual: any, expected: RegExp | ErrorLikeConstructor) {
let rejected;
try {
await promise;
if (typeof actual === 'function') {
await actual();
} else {
await actual;
}
} catch (err) {
rejected = true;
assert.throws(() => {
throw err;
}, expect);
}, expected);
}
assert(rejected, 'Promise not rejected');
}