|
| 1 | +/** |
| 2 | + * Copyright 2019, OpenCensus 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 | +import * as assert from 'assert'; |
| 18 | +import {TagMap, TagTtl} from '../src'; |
| 19 | +import {deserializeTextFormat, MAX_NUMBER_OF_TAGS, serializeTextFormat} from '../src/tags/propagation/text-format'; |
| 20 | + |
| 21 | +const K1 = { |
| 22 | + name: 'k1' |
| 23 | +}; |
| 24 | +const K2 = { |
| 25 | + name: 'k2' |
| 26 | +}; |
| 27 | + |
| 28 | +const V1 = { |
| 29 | + value: 'v1' |
| 30 | +}; |
| 31 | +const V2 = { |
| 32 | + value: 'v2' |
| 33 | +}; |
| 34 | + |
| 35 | +describe('Text Format Serializer', () => { |
| 36 | + const emptyTagMap = new TagMap(); |
| 37 | + |
| 38 | + const singleTagMap = new TagMap(); |
| 39 | + singleTagMap.set(K1, V1); |
| 40 | + |
| 41 | + const multipleTagMap = new TagMap(); |
| 42 | + multipleTagMap.set(K1, V1); |
| 43 | + multipleTagMap.set(K2, V2); |
| 44 | + |
| 45 | + const NO_PROPAGATION_MD = {tagTtl: TagTtl.NO_PROPAGATION}; |
| 46 | + const nonPropagatingTagMap = new TagMap(); |
| 47 | + nonPropagatingTagMap.set(K1, V1, NO_PROPAGATION_MD); |
| 48 | + |
| 49 | + describe('serializeTextFormat', () => { |
| 50 | + it('should serialize empty tag map', () => { |
| 51 | + const textFormat = serializeTextFormat(emptyTagMap); |
| 52 | + assert.equal(textFormat, ''); |
| 53 | + }); |
| 54 | + |
| 55 | + it('should serialize with one tag map', () => { |
| 56 | + const textFormat = serializeTextFormat(singleTagMap); |
| 57 | + assert.deepEqual(textFormat, 'k1=v1'); |
| 58 | + }); |
| 59 | + |
| 60 | + it('should serialize with multiple tag', () => { |
| 61 | + const textFormat = serializeTextFormat(multipleTagMap); |
| 62 | + assert.deepEqual(textFormat, 'k1=v1,k2=v2'); |
| 63 | + }); |
| 64 | + |
| 65 | + it('should skip non propagating tag', () => { |
| 66 | + const textFormat = serializeTextFormat(nonPropagatingTagMap); |
| 67 | + assert.deepEqual(textFormat, ''); |
| 68 | + }); |
| 69 | + |
| 70 | + it('should throw an error when exceeds the max number of tags', () => { |
| 71 | + const tags = new TagMap(); |
| 72 | + for (let i = 0; i < MAX_NUMBER_OF_TAGS + 1; i++) { |
| 73 | + tags.set({name: `name-${i}`}, {value: `value-${i}`}); |
| 74 | + } |
| 75 | + |
| 76 | + assert.throws(() => { |
| 77 | + serializeTextFormat(tags); |
| 78 | + }, /^Error: Number of tags in the TagMap exceeds limit 180/); |
| 79 | + }); |
| 80 | + }); |
| 81 | + |
| 82 | + describe('deserializeTextFormat', () => { |
| 83 | + it('should deserialize empty string', () => { |
| 84 | + const deserializedTagMap = deserializeTextFormat(''); |
| 85 | + assert.deepEqual(deserializedTagMap.tags.size, 0); |
| 86 | + }); |
| 87 | + |
| 88 | + it('should deserialize with one key value pair', () => { |
| 89 | + const deserializedTagMap = deserializeTextFormat('k1=v1'); |
| 90 | + assert.deepEqual(deserializedTagMap.tags.size, 1); |
| 91 | + assert.deepEqual(deserializedTagMap, singleTagMap); |
| 92 | + }); |
| 93 | + |
| 94 | + it('should deserialize with multiple pairs', () => { |
| 95 | + const deserializedTagMap = deserializeTextFormat('k1=v1,k2=v2'); |
| 96 | + assert.deepEqual(deserializedTagMap.tags.size, 2); |
| 97 | + assert.deepEqual(deserializedTagMap, multipleTagMap); |
| 98 | + }); |
| 99 | + |
| 100 | + it('should deserialize with white spaces tag', () => { |
| 101 | + const expectedTagMap = new TagMap(); |
| 102 | + expectedTagMap.set(K1, {value: ' v1'}); |
| 103 | + expectedTagMap.set({name: ' k2'}, {value: 'v 2'}); |
| 104 | + |
| 105 | + const deserializedTagMap = deserializeTextFormat('k1= v1, k2=v 2'); |
| 106 | + assert.deepEqual(deserializedTagMap.tags.size, 2); |
| 107 | + assert.deepEqual(deserializedTagMap, expectedTagMap); |
| 108 | + }); |
| 109 | + |
| 110 | + it('should throw an error when tags are malformed', () => { |
| 111 | + assert.throws(() => { |
| 112 | + deserializeTextFormat('k1,v1,k2=v2'); |
| 113 | + }, /^Error: Malformed tag k1/); |
| 114 | + }); |
| 115 | + }); |
| 116 | +}); |
0 commit comments