| 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 |
1×
1×
1×
1×
1×
2×
2×
1×
1×
2×
2×
1×
1×
2×
2×
1×
1×
1×
1×
1×
1×
1×
35×
35×
185×
185×
185×
35×
100×
99×
1×
34×
1×
2×
2×
2×
4×
4×
2×
2×
2×
1×
2×
2×
2×
1×
1×
2×
2×
1×
1×
1×
1×
1×
1×
1×
2×
2×
2×
1×
9×
| // @flow
// Copyright (c) 2016 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
import deepEqual from 'deep-equal';
import opentracing from 'opentracing';
import Span from './span';
import Utils from './util';
export default class TestUtils {
static traceIdEqual(span: Span, traceId: number): boolean {
var spanTraceId = span.context().traceId
if (spanTraceId == null && traceId == null) {
return true;
}
return deepEqual(spanTraceId, Utils.encodeInt64(traceId));
}
static spanIdEqual(span: Span, spanId: number): boolean {
var spanContextId = span.context().spanId
if (spanContextId == null && spanId == null) {
return true;
}
return deepEqual(spanContextId, Utils.encodeInt64(spanId));
}
static parentIdEqual(span: Span, parentId: number): boolean {
var spanParentId = span.context().parentId
if (spanParentId == null && parentId == null) {
return true;
}
return deepEqual(spanParentId, Utils.encodeInt64(parentId));
}
static flagsEqual(span: Span, flags: number): boolean {
var spanFlagsId = span.context().flags;
return spanFlagsId === flags;
}
static operationNameEqual(span: Span, operationName: string): boolean {
return span._operationName === operationName;
}
static startTimeEqual(span: Span, startTime: number): boolean {
return span._startTime === startTime;
}
static durationEqual(span: Span, duration: number): boolean {
return span._duration === duration;
}
static hasTags(span: Span, tags: any): boolean {
// TODO(oibe) make this work for duplicate tags
let expectedTags = {};
for (let i = 0; i < span._tags.length; i++) {
let key = span._tags[i].key;
let value = span._tags[i].value;
expectedTags[key] = value;
}
for (let tag in tags) {
if (tags.hasOwnProperty(tag) && expectedTags.hasOwnProperty(tag)) {
if (IexpectedTags[tag] !== tags[tag]) {
console.log('expected tag:', expectedTags[tag], ', actual tag: ', tags[tag]);
return false;
}
} else {
// mismatch in tag keys
return false;
}
}
return true;
}
static hasLogs(span: Span, logs: Array<any>): boolean {
/*
1.) This method does not work if span logs have nested objects...
2.) This is hard to make linear because if an object does not have a guaranteed order
then I cannot stringify a log, and use it as a key in a hashmap. So its safer to compare object
equality with a O(n^2) which is only used for testing.
*/
for (let i = 0; i < logs.length; i++) {
let expectedLog = span._logs[i];
let found = false;
for (let j = 0; j < span._logs.length; j++) {
let spanLog = span._logs[j];
if (deepEqual(spanLog, expectedLog)) {
found = true
}
}
if (I!found) {
return false;
}
found = false;
}
return true;
}
static isClient(span: Span): boolean {
let tag = {};
tag[opentracing.Tags.SPAN_KIND] = opentracing.Tags.SPAN_KIND_RPC_CLIENT;
return TestUtils.hasTags(span, tag);
}
static hasBaggage(span: Span, baggage: any): boolean {
let found = false;
for (let key in baggage) {
found = span.getBaggageItem(key);
if (I!found) {
return false;
}
}
return true;
}
static isDebug(span: Span): boolean {
return span.context().isDebug();
}
static isSampled(span: Span): boolean {
return span.context().isSampled();
}
static carrierHasTracerState(carrier: any): boolean {
let tracerState = carrier['uber-trace-id'];
return tracerState !== null && tracerState !== undefined;
}
static carrierHasBaggage(carrier: any, baggage: any, prefix: string = 'uberctx-') {
for (let key in baggage) {
if (Ebaggage.hasOwnProperty(key)) {
let prefixKey = prefix+key;
if (I!(prefixKey in carrier)) {
return false;
}
}
}
return true;
}
static thriftSpanEqual(spanOne, spanTwo) {
return deepEqual(spanOne.traceIdLow, spanTwo.traceIdLow) &&
deepEqual(spanOne.traceIdHigh, spanTwo.traceIdHigh) &&
deepEqual(spanOne.spanId, spanTwo.spanId) &&
deepEqual(spanOne.parentSpanId, spanTwo.parentSpanId) &&
spanOne.operationName === spanTwo.operationName &&
deepEqual(spanOne.references, spanTwo.references) &&
spanOne.flags === spanTwo.flags &&
deepEqual(spanOne.startTime, spanTwo.startTime) &&
deepEqual(spanOne.duration, spanTwo.duration);
}
}
|