Skip to content

Commit

Permalink
refactor(metric-reader): combine TestMetricExporter with WaitingMetri…
Browse files Browse the repository at this point in the history
…cExporter
  • Loading branch information
pichlermarc committed Dec 22, 2021
1 parent a09f25e commit fd9b6c5
Showing 1 changed file with 15 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,48 +26,17 @@ import { ReaderResultCode } from '../../src/export/ReaderResult';
const MAX_32_BIT_INT = 2 ** 31 - 1

class TestMetricExporter extends MetricExporter {
metricDataList: MetricData[] = []
public exportTime = 0;
public throwException = false;

async export(batch: MetricData[]): Promise<void> {
this.metricDataList.push(...batch);
if (this.throwException) {
throw new Error('Error during export');
}
await new Promise(resolve => setTimeout(resolve, this.exportTime));
}

async forceFlush(): Promise<void> {
}

getPreferredAggregationTemporality(): AggregationTemporality {
return AggregationTemporality.CUMULATIVE;
}
}

class TestDeltaMetricExporter extends TestMetricExporter {
override getPreferredAggregationTemporality(): AggregationTemporality {
return AggregationTemporality.DELTA;
}
}

class WaitingMetricExporter extends MetricExporter {
private _batches: MetricData[][];
public throwException: boolean;

constructor() {
super();
this._batches = [];
this.throwException = false;
}
private _batches: MetricData[][] = [];

async export(batch: MetricData[]): Promise<void> {
this._batches.push([]);

if (this.throwException) {
throw new Error('Error during export');
}
await new Promise(resolve => setTimeout(resolve, this.exportTime));
}

async forceFlush(): Promise<void> {
Expand All @@ -89,6 +58,12 @@ class WaitingMetricExporter extends MetricExporter {
}
}

class TestDeltaMetricExporter extends TestMetricExporter {
override getPreferredAggregationTemporality(): AggregationTemporality {
return AggregationTemporality.DELTA;
}
}

class TestMetricProducer implements MetricProducer {
async collect(): Promise<MetricData[]> {
return [];
Expand Down Expand Up @@ -157,7 +132,7 @@ describe('PeriodicExportingMetricReader', () => {

describe('setMetricProducer', () => {
it('should start exporting periodically', async () => {
const exporter = new WaitingMetricExporter();
const exporter = new TestMetricExporter();
const reader = new PeriodicExportingMetricReader({
exporter: exporter,
exportIntervalMillis: 50,
Expand All @@ -169,12 +144,12 @@ describe('PeriodicExportingMetricReader', () => {

assert.deepEqual(result, [[], []]);
await new Promise(resolve => reader.shutdown({ done: resolve }));
}).timeout(1000);
});
});

describe('periodic export', () => {
it('should keep running on export errors', async () => {
const exporter = new WaitingMetricExporter();
const exporter = new TestMetricExporter();
exporter.throwException = true;
const reader = new PeriodicExportingMetricReader({
exporter: exporter,
Expand Down Expand Up @@ -237,7 +212,7 @@ describe('PeriodicExportingMetricReader', () => {
reader.shutdown({ done: _result => done() });
}
});
}).timeout(1000);
});

it('should return FAILED when handler throws', done => {
const exporter = new TestMetricExporter();
Expand Down Expand Up @@ -319,7 +294,7 @@ describe('PeriodicExportingMetricReader', () => {
done();
}
});
}).timeout(1000);
});

it('should return FAILED when called twice', done => {
const exporter = new TestMetricExporter();
Expand Down Expand Up @@ -402,8 +377,7 @@ describe('PeriodicExportingMetricReader', () => {
})
});
}
})

})
});
});
})
});

0 comments on commit fd9b6c5

Please sign in to comment.