Skip to content

Commit

Permalink
chore: fix tests of exporter-collector-proto
Browse files Browse the repository at this point in the history
  • Loading branch information
vmarchaud committed Oct 24, 2020
1 parent ef79804 commit af95af1
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import {
ensureExportedCounterIsCorrect,
ensureExportedObserverIsCorrect,
ensureExportedValueRecorderIsCorrect,
MockedResponse,
} from './helper';
import { MetricRecord } from '@opentelemetry/metrics';

Expand All @@ -39,14 +40,6 @@ const fakeRequest = {
write: function () {},
};

const mockRes = {
statusCode: 200,
};

const mockResError = {
statusCode: 400,
};

// send is lazy loading file so need to wait a bit
const waitTimeMS = 20;

Expand Down Expand Up @@ -164,9 +157,11 @@ describe('CollectorMetricExporter - node with proto over http', () => {
collectorExporter.export(metrics, responseSpy);

setTimeout(() => {
const mockRes = new MockedResponse(200);
const args = spyRequest.args[0];
const callback = args[1];
callback(mockRes);
mockRes.send('success');
setTimeout(() => {
const response: any = spyLoggerDebug.args[1][0];
assert.strictEqual(response, 'statusCode: 200');
Expand All @@ -191,12 +186,17 @@ describe('CollectorMetricExporter - node with proto over http', () => {
collectorExporter.export(metrics, responseSpy);

setTimeout(() => {
const mockRes = new MockedResponse(400);
const args = spyRequest.args[0];
const callback = args[1];
callback(mockResError);
callback(mockRes);
mockRes.send('failed');
setTimeout(() => {
const response = spyLoggerError.args[0][0] as string;
assert.ok(response.includes('"code":"400"'));
const error = JSON.parse(
spyLoggerError.args[0][0]
) as collectorTypes.CollectorExporterError;
assert.strictEqual(error.code, '400');
assert.strictEqual(error.data, 'failed');
assert.strictEqual(responseSpy.args[0][0], 1);
done();
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import {
ensureExportTraceServiceRequestIsSet,
ensureProtoSpanIsCorrect,
mockedReadableSpan,
MockedResponse,
} from './helper';

const fakeRequest = {
Expand All @@ -36,14 +37,6 @@ const fakeRequest = {
write: function () {},
};

const mockRes = {
statusCode: 200,
};

const mockResError = {
statusCode: 400,
};

// send is lazy loading file so need to wait a bit
const waitTimeMS = 20;

Expand Down Expand Up @@ -130,9 +123,11 @@ describe('CollectorTraceExporter - node with proto over http', () => {
collectorExporter.export(spans, responseSpy);

setTimeout(() => {
const mockRes = new MockedResponse(200);
const args = spyRequest.args[0];
const callback = args[1];
callback(mockRes);
mockRes.send('success');
setTimeout(() => {
const response: any = spyLoggerDebug.args[1][0];
assert.strictEqual(response, 'statusCode: 200');
Expand All @@ -157,13 +152,17 @@ describe('CollectorTraceExporter - node with proto over http', () => {
collectorExporter.export(spans, responseSpy);

setTimeout(() => {
const mockResError = new MockedResponse(400);
const args = spyRequest.args[0];
const callback = args[1];
callback(mockResError);
mockResError.send('failed');
setTimeout(() => {
const response = spyLoggerError.args[0][0] as string;

assert.ok(response.includes('"code":"400"'));
const error = JSON.parse(
spyLoggerError.args[0][0]
) as collectorTypes.CollectorExporterError;
assert.strictEqual(error.code, '400');
assert.strictEqual(error.data, 'failed');
assert.strictEqual(responseSpy.args[0][0], 1);
done();
});
Expand Down
20 changes: 20 additions & 0 deletions packages/opentelemetry-exporter-collector-proto/test/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { Resource } from '@opentelemetry/resources';
import { collectorTypes } from '@opentelemetry/exporter-collector';
import * as assert from 'assert';
import { MeterProvider, MetricRecord } from '@opentelemetry/metrics';
import { Stream } from 'stream';

const meterProvider = new MeterProvider({
interval: 30000,
Expand Down Expand Up @@ -415,3 +416,22 @@ export function ensureExportMetricsServiceRequestIsSet(
const metrics = resourceMetrics[0].instrumentationLibraryMetrics[0].metrics;
assert.strictEqual(metrics.length, 3, 'Metrics are missing');
}

export class MockedResponse extends Stream {
constructor(private _code: number, private _msg?: string) {
super();
}

send(data: string) {
this.emit('data', data);
this.emit('end');
}

get statusCode() {
return this._code;
}

get statusMessage() {
return this._msg;
}
}

0 comments on commit af95af1

Please sign in to comment.