|
17 | 17 | import * as assert from 'assert';
|
18 | 18 | import {BaseView, globalStats, StatsEventListener, TagKey, TagMap, TagValue} from '../src';
|
19 | 19 | import {AggregationType, LastValueData, Measure, Measurement, MeasureType, MeasureUnit, View} from '../src/stats/types';
|
| 20 | +import * as tagger from '../src/tags/tagger'; |
20 | 21 |
|
21 | 22 | class TestExporter implements StatsEventListener {
|
22 | 23 | registeredViews: View[] = [];
|
@@ -225,5 +226,40 @@ describe('Stats', () => {
|
225 | 226 | globalStats.record(measurments, tagMap);
|
226 | 227 | assert.equal(testExporter.recordedMeasurements.length, 0);
|
227 | 228 | });
|
| 229 | + |
| 230 | + it('should record against implicit context when set', () => { |
| 231 | + const tags = new TagMap(); |
| 232 | + tags.set(tagKeys[0], {value: 'value1'}); |
| 233 | + tags.set(tagKeys[1], {value: 'value2'}); |
| 234 | + const measurement = {measure, value: 1}; |
| 235 | + tagger.withTagContext(tags, () => { |
| 236 | + globalStats.record([measurement]); |
| 237 | + }); |
| 238 | + |
| 239 | + assert.strictEqual(testExporter.recordedMeasurements.length, 1); |
| 240 | + assert.deepEqual(testExporter.recordedMeasurements[0], measurement); |
| 241 | + aggregationData = |
| 242 | + testExporter.registeredViews[0].getSnapshot( |
| 243 | + [{value: 'value1'}, {value: 'value2'}]) as LastValueData; |
| 244 | + assert.strictEqual(aggregationData.value, measurement.value); |
| 245 | + assert.deepStrictEqual( |
| 246 | + aggregationData.tagValues, [{value: 'value1'}, {value: 'value2'}]); |
| 247 | + }); |
| 248 | + |
| 249 | + it('should record against implicit context when not set or empty', () => { |
| 250 | + const UNKNOWN_TAG_VALUE: TagValue = null; |
| 251 | + globalStats.registerExporter(testExporter); |
| 252 | + const measurement = {measure, value: 2211}; |
| 253 | + tagger.withTagContext(tagger.EMPTY_TAG_MAP, () => { |
| 254 | + globalStats.record([measurement]); |
| 255 | + }); |
| 256 | + |
| 257 | + aggregationData = |
| 258 | + testExporter.registeredViews[0].getSnapshot( |
| 259 | + [UNKNOWN_TAG_VALUE, UNKNOWN_TAG_VALUE]) as LastValueData; |
| 260 | + assert.strictEqual(aggregationData.value, measurement.value); |
| 261 | + assert.deepStrictEqual( |
| 262 | + aggregationData.tagValues, [UNKNOWN_TAG_VALUE, UNKNOWN_TAG_VALUE]); |
| 263 | + }); |
228 | 264 | });
|
229 | 265 | });
|
0 commit comments