forked from googleapis/cloud-trace-nodejs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest-util.ts
271 lines (245 loc) · 8.56 KB
/
test-util.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
// Copyright 2015 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
import * as assert from 'assert';
import {describe, it} from 'mocha';
import {inspect} from 'util';
import {Constants} from '../src/constants';
import {Logger} from '../src/logger';
import * as util from '../src/util';
import {TestLogger} from './logger';
const notNull = <T>(x: T | null | undefined): T => {
assert.notStrictEqual(x, null);
assert.notStrictEqual(x, undefined);
return x as T;
};
describe('Singleton', () => {
const logger = new TestLogger();
class MyClass {
constructor(
public config: {},
public logger: Logger
) {}
}
describe('create', () => {
it('creates an instance of the given class', () => {
const createResult = new util.Singleton(MyClass).create({}, logger);
assert.ok(createResult instanceof MyClass);
});
it('passes arguments to the underlying constructor', () => {
const config = {};
const createResult = new util.Singleton(MyClass).create(config, logger);
assert.strictEqual(createResult.logger, logger);
assert.strictEqual(createResult.config, config);
});
it('throws when used more than once, by default', () => {
const singleton = new util.Singleton(MyClass);
singleton.create({}, logger);
assert.throws(() => singleton.create({}, logger));
});
it('creates a new instance when [FORCE_NEW] is true in the config', () => {
const singleton = new util.Singleton(MyClass);
const createResult1 = singleton.create({}, logger);
const createResult2 = singleton.create({[util.FORCE_NEW]: true}, logger);
assert.notStrictEqual(createResult1, createResult2);
});
});
describe('get', () => {
it('throws if create was not called first', () => {
assert.throws(() => new util.Singleton(MyClass).get());
});
it('returns the same value returned by create function', () => {
const singleton = new util.Singleton(MyClass);
const createResult = singleton.create({}, logger);
const getResult = singleton.get();
assert.strictEqual(getResult, createResult);
});
it('does not return a stale value', () => {
const singleton = new util.Singleton(MyClass);
singleton.create({}, logger);
const createResult = singleton.create({[util.FORCE_NEW]: true}, logger);
const getResult = singleton.get();
assert.strictEqual(getResult, createResult);
});
});
});
describe('util.lastOf', () => {
it('should return the last non-null/undefined/NaN parameter', () => {
const {lastOf} = util;
assert.strictEqual(lastOf<number>(1), 1);
assert.strictEqual(lastOf<number>(1, 2, null), 2);
assert.strictEqual(lastOf<number>(1, null, 2), 2);
assert.strictEqual(lastOf<number>(1, 2, undefined), 2);
assert.strictEqual(lastOf<number>(1, 2, NaN), 2);
assert.strictEqual(lastOf<number>(1, 2, null, undefined, NaN, -NaN), 2);
assert.strictEqual(lastOf<number>(1, 0), 0);
assert.strictEqual(lastOf<number | string>(1, ''), '');
});
});
describe('util.truncate', () => {
it('should truncate objects larger than size', () => {
assert.strictEqual(util.truncate('abcdefghijklmno', 5), 'ab...');
});
it('should not truncate objects smaller than size', () => {
assert.strictEqual(util.truncate('abcdefghijklmno', 50), 'abcdefghijklmno');
});
it('should handle unicode characters', () => {
const longName = new Array(120).join('☃');
assert.strictEqual(
util.truncate(longName, Constants.TRACE_SERVICE_SPAN_NAME_LIMIT),
`${new Array(42).join('☃')}...`
);
});
});
describe('util.parseContextFromHeader', () => {
describe('valid inputs', () => {
it('should return expected values: 123456/667;o=1', () => {
const result = notNull(util.parseContextFromHeader('123456/667;o=1'));
assert.strictEqual(result.traceId, '123456');
assert.strictEqual(result.spanId, '667');
assert.strictEqual(result.options, 1);
});
it(
'should return expected values:' +
'123456/123456123456123456123456123456123456;o=1',
() => {
const result = notNull(
util.parseContextFromHeader(
'123456/123456123456123456123456123456123456;o=1'
)
);
assert.strictEqual(result.traceId, '123456');
assert.strictEqual(
result.spanId,
'123456123456123456123456123456123456'
);
assert.strictEqual(result.options, 1);
}
);
it('should return expected values: 123456/667', () => {
const result = notNull(util.parseContextFromHeader('123456/667'));
assert.strictEqual(result.traceId, '123456');
assert.strictEqual(result.spanId, '667');
assert.strictEqual(result.options, undefined);
});
});
describe('invalid inputs', () => {
const inputs = [
'',
null,
undefined,
'123456',
'123456;o=1',
'o=1;123456',
'123;456;o=1',
'123/o=1;456',
'123/abc/o=1',
];
inputs.forEach(s => {
it(`should reject ${s}`, () => {
// TS: Cast s as any rather than coerce it to a value
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const result = util.parseContextFromHeader(s as any);
assert.ok(!result);
});
});
});
});
describe('util.generateTraceContext', () => {
const inputs: util.TraceContext[] = [
{traceId: '123456', spanId: '667', options: 1},
{traceId: '123456', spanId: '667', options: undefined},
];
inputs.forEach(s => {
it(`returns well-formatted trace context for ${inspect(s)}`, () => {
const context = util.generateTraceContext(s);
const parsed = util.parseContextFromHeader(context);
assert.deepStrictEqual(parsed, s);
});
});
it('returns an empty string if passed a falsy value', () => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const context = util.generateTraceContext(null as any);
assert.strictEqual(context, '');
});
});
describe('binary trace context', () => {
const commonTraceId = 'ffeeddccbbaa99887766554433221100';
const testCases: Array<{
structured: util.TraceContext | null;
binary: string;
description: string;
}> = [
{
structured: {
traceId: commonTraceId,
spanId: (0x111111111111).toString(),
options: 1,
},
binary: `0000${commonTraceId}01${'0000111111111111'}02${'01'}`,
description: 'trace context with 48-bit span ID',
},
{
structured: {
traceId: commonTraceId,
spanId: '8603657889541918976',
options: 1,
},
binary: `0000${commonTraceId}01${'7766554433221100'}02${'01'}`,
description: 'trace context with 64-bit span ID',
},
{
structured: {traceId: commonTraceId, spanId: '1', options: 255},
binary: `0000${commonTraceId}01${'0000000000000001'}02${'ff'}`,
description: 'trace context with 8-bit options',
},
{
structured: {traceId: commonTraceId, spanId: '1'},
binary: `0000${commonTraceId}01${'0000000000000001'}02${'00'}`,
description: 'trace context with no options',
},
{
structured: null,
binary: '00',
description: 'incomplete binary trace context (by returning null)',
},
{
structured: null,
binary: '0'.repeat(58),
description: 'bad binary trace context (by returning null)',
},
];
describe('util.serializeTraceContext', () => {
testCases.forEach(
testCase =>
testCase.structured &&
it(`should serialize ${testCase.description}`, () => {
assert.deepStrictEqual(
util.serializeTraceContext(testCase.structured!).toString('hex'),
testCase.binary
);
})
);
});
describe('util.deserializeTraceContext', () => {
testCases.forEach(testCase =>
it(`should deserialize ${testCase.description}`, () => {
assert.deepStrictEqual(
util.deserializeTraceContext(Buffer.from(testCase.binary, 'hex')),
testCase.structured &&
Object.assign({options: 0}, testCase.structured)
);
})
);
});
});