Skip to content

Commit f24474f

Browse files
committed
test(sdk-metrics): browser compatibility tests
1 parent 6b94e26 commit f24474f

File tree

6 files changed

+135
-21
lines changed

6 files changed

+135
-21
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*!
2+
* Copyright The OpenTelemetry Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
const karmaWebpackConfig = require('../../../karma.webpack');
18+
const karmaBaseConfig = require('../../../karma.base');
19+
20+
module.exports = (config) => {
21+
config.set(Object.assign({}, karmaBaseConfig, {
22+
webpack: karmaWebpackConfig
23+
}))
24+
};

experimental/packages/opentelemetry-sdk-metrics-base/package.json

+12
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@
1212
"compile": "tsc --build tsconfig.all.json",
1313
"clean": "tsc --build --clean tsconfig.all.json",
1414
"test": "nyc ts-mocha -p tsconfig.json 'test/**/*.test.ts'",
15+
"test:browser": "nyc karma start --single-run",
1516
"tdd": "npm run test -- --watch-extensions ts --watch",
17+
"tdd:browser": "karma start",
1618
"codecov": "nyc report --reporter=json && codecov -f coverage/*.json -p ../../",
1719
"lint": "eslint . --ext .ts",
1820
"lint:fix": "eslint . --ext .ts --fix",
@@ -57,6 +59,13 @@
5759
"@types/node": "14.17.33",
5860
"@types/sinon": "10.0.6",
5961
"codecov": "3.8.3",
62+
"karma": "6.3.8",
63+
"karma-chrome-launcher": "3.1.0",
64+
"karma-coverage-istanbul-reporter": "3.0.3",
65+
"karma-jquery": "0.2.4",
66+
"karma-mocha": "2.0.1",
67+
"karma-spec-reporter": "0.0.32",
68+
"karma-webpack": "4.0.2",
6069
"mocha": "7.2.0",
6170
"nyc": "15.1.0",
6271
"rimraf": "3.0.2",
@@ -73,5 +82,8 @@
7382
"@opentelemetry/core": "1.0.1",
7483
"@opentelemetry/resources": "1.0.1",
7584
"lodash.merge": "4.6.2"
85+
},
86+
"overrides": {
87+
"colors": "1.4.0"
7688
}
7789
}

experimental/packages/opentelemetry-sdk-metrics-base/test/export/PeriodicExportingMetricReader.test.ts

+19-18
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import * as assert from 'assert';
2222
import * as sinon from 'sinon';
2323
import { MetricProducer } from '../../src/export/MetricProducer';
2424
import { TimeoutError } from '../../src/utils';
25+
import { assertRejects } from '../test-utils';
2526

2627
const MAX_32_BIT_INT = 2 ** 31 - 1
2728

@@ -94,34 +95,34 @@ describe('PeriodicExportingMetricReader', () => {
9495
}
9596
);
9697
assert.strictEqual(reader.getPreferredAggregationTemporality(), exporter.getPreferredAggregationTemporality());
97-
})
98+
});
9899

99100
it('should throw when interval less or equal to 0', () => {
100101
const exporter = new TestDeltaMetricExporter();
101102
assert.throws(() => new PeriodicExportingMetricReader({
102103
exporter: exporter,
103104
exportIntervalMillis: 0,
104105
exportTimeoutMillis: 0
105-
}), new Error('exportIntervalMillis must be greater than 0'));
106-
})
106+
}), /exportIntervalMillis must be greater than 0/);
107+
});
107108

108109
it('should throw when timeout less or equal to 0', () => {
109110
const exporter = new TestDeltaMetricExporter();
110111
assert.throws(() => new PeriodicExportingMetricReader({
111112
exporter: exporter,
112113
exportIntervalMillis: 1,
113114
exportTimeoutMillis: 0
114-
}), new Error('exportTimeoutMillis must be greater than 0'));
115-
})
115+
}), /exportTimeoutMillis must be greater than 0/);
116+
});
116117

117118
it('should throw when timeout less or equal to interval', () => {
118119
const exporter = new TestDeltaMetricExporter();
119120
assert.throws(() => new PeriodicExportingMetricReader({
120121
exporter: exporter,
121122
exportIntervalMillis: 100,
122123
exportTimeoutMillis: 200
123-
}), new Error('exportIntervalMillis must be greater than or equal to exportTimeoutMillis'));
124-
})
124+
}), /exportIntervalMillis must be greater than or equal to exportTimeoutMillis/);
125+
});
125126

126127
it('should not start exporting', async () => {
127128
const exporter = new TestDeltaMetricExporter();
@@ -136,7 +137,7 @@ describe('PeriodicExportingMetricReader', () => {
136137
await new Promise(resolve => setTimeout(resolve, 50));
137138

138139
exporterMock.verify();
139-
})
140+
});
140141
});
141142

142143
describe('setMetricProducer', () => {
@@ -151,7 +152,7 @@ describe('PeriodicExportingMetricReader', () => {
151152
reader.setMetricProducer(new TestMetricProducer());
152153
const result = await exporter.waitForNumberOfExports(2);
153154

154-
assert.deepEqual(result, [[], []]);
155+
assert.deepStrictEqual(result, [[], []]);
155156
await reader.shutdown();
156157
});
157158
});
@@ -169,7 +170,7 @@ describe('PeriodicExportingMetricReader', () => {
169170
reader.setMetricProducer(new TestMetricProducer());
170171

171172
const result = await exporter.waitForNumberOfExports(2);
172-
assert.deepEqual(result, [[], []]);
173+
assert.deepStrictEqual(result, [[], []]);
173174

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

190191
const result = await exporter.waitForNumberOfExports(2);
191-
assert.deepEqual(result, [[], []]);
192+
assert.deepStrictEqual(result, [[], []]);
192193

193194
exporter.throwException = false;
194195
await reader.shutdown();
@@ -227,7 +228,7 @@ describe('PeriodicExportingMetricReader', () => {
227228
});
228229

229230
reader.setMetricProducer(new TestMetricProducer());
230-
await assert.rejects(() => reader.forceFlush({ timeoutMillis: 20 }),
231+
await assertRejects(() => reader.forceFlush({ timeoutMillis: 20 }),
231232
TimeoutError);
232233
await reader.shutdown();
233234
});
@@ -241,7 +242,7 @@ describe('PeriodicExportingMetricReader', () => {
241242
exportTimeoutMillis: 80,
242243
});
243244

244-
await assert.rejects(() => reader.forceFlush());
245+
await assertRejects(() => reader.forceFlush(), /Error during forceFlush/);
245246
});
246247

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

296297
reader.setMetricProducer(new TestMetricProducer());
297-
await assert.rejects(() => reader.shutdown({ timeoutMillis: 20 }),
298+
await assertRejects(() => reader.shutdown({ timeoutMillis: 20 }),
298299
TimeoutError);
299300
});
300301

@@ -326,7 +327,7 @@ describe('PeriodicExportingMetricReader', () => {
326327
exportTimeoutMillis: 80,
327328
});
328329

329-
await assert.rejects(() => reader.shutdown());
330+
await assertRejects(() => reader.shutdown(), /Error during forceFlush/);
330331
});
331332
})
332333
;
@@ -340,7 +341,7 @@ describe('PeriodicExportingMetricReader', () => {
340341
exportTimeoutMillis: 80,
341342
});
342343

343-
await assert.rejects(() => reader.collect());
344+
await assertRejects(() => reader.collect(), /MetricReader is not bound to a MetricProducer/);
344345
});
345346

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

356357
await reader.shutdown();
357-
assert.deepEqual([], await reader.collect());
358+
assert.deepStrictEqual([], await reader.collect());
358359
});
359360

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

371-
await assert.rejects(
372+
await assertRejects(
372373
() => reader.collect({ timeoutMillis: 20 }),
373374
TimeoutError
374375
);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/*
2+
* Copyright The OpenTelemetry Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
const testsContext = require.context('.', true, /test$/);
18+
testsContext.keys().forEach(testsContext);
19+
20+
const srcContext = require.context('.', true, /src$/);
21+
srcContext.keys().forEach(srcContext);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Copyright The OpenTelemetry Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
/**
18+
* Changes to this file should be applied to opentelemetry-core/test/test-utils.ts too.
19+
*/
20+
21+
import * as assert from 'assert';
22+
23+
interface ErrorLikeConstructor {
24+
new(): Error;
25+
}
26+
27+
/**
28+
* Node.js v8.x and browser compatible `assert.rejects`.
29+
*/
30+
export async function assertRejects(actual: any, expected: RegExp | ErrorLikeConstructor) {
31+
let rejected;
32+
try {
33+
if (typeof actual === 'function') {
34+
await actual();
35+
} else {
36+
await actual;
37+
}
38+
} catch (err) {
39+
rejected = true;
40+
assert.throws(() => {
41+
throw err;
42+
}, expected);
43+
}
44+
assert(rejected, 'Promise not rejected');
45+
}

packages/opentelemetry-core/test/test-utils.ts

+14-3
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,26 @@
1616

1717
import * as assert from 'assert';
1818

19+
interface ErrorLikeConstructor {
20+
new(): Error;
21+
}
22+
1923
/**
2024
* Node.js v8.x and browser compatible `assert.rejects`.
2125
*/
22-
export async function assertRejects(promise: any, expect: any) {
26+
export async function assertRejects(actual: any, expected: RegExp | ErrorLikeConstructor) {
27+
let rejected;
2328
try {
24-
await promise;
29+
if (typeof actual === 'function') {
30+
await actual();
31+
} else {
32+
await actual;
33+
}
2534
} catch (err) {
35+
rejected = true;
2636
assert.throws(() => {
2737
throw err;
28-
}, expect);
38+
}, expected);
2939
}
40+
assert(rejected, 'Promise not rejected');
3041
}

0 commit comments

Comments
 (0)