Skip to content

Commit 951502d

Browse files
Ashish AggarwalAshish Aggarwal
authored andcommitted
adding mocha/chai and few unit tests
1 parent 60f611c commit 951502d

6 files changed

Lines changed: 135 additions & 15 deletions

File tree

Makefile

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,14 @@ check-node-version:
1212
@$(MIN_NODE_VER_FOUND) || echo Build requires minimum Node $(MIN_NODE_VER).x
1313

1414
.PHONY: build
15-
build: check-node-version npm_install idl_codegen tslint
15+
build: check-node-version npm_install idl_codegen tslint compile test
16+
17+
.PHONY: test
18+
test:
19+
./node_modules/mocha/bin/mocha -r ./node_modules/ts-node/register src/tests/**/*.ts
20+
21+
.PHONY: compile
22+
compile:
1623
rm -rf ./dist/
1724
tsc -p tsconfig.json
1825
cp package.json dist/

package.json

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,18 @@
2222
"dependencies": {
2323
"google-protobuf": "^3.0.0",
2424
"grpc": "^1.9.0",
25-
"uuid": "^3.2.1",
26-
"opentracing": "^0.13.0"
25+
"opentracing": "^0.13.0",
26+
"uuid": "^3.2.1"
2727
},
2828
"devDependencies": {
29-
"@types/node": "^10.3.4",
30-
"typescript": "2.9.2",
29+
"@types/node": "7.0.12",
30+
"@types/chai": "3.4.35",
31+
"@types/mocha": "2.2.40",
32+
"chai": "^3.5.0",
3133
"grpc-tools": "1.6.6",
32-
"tslint": "^5.0.0"
34+
"mocha": "^2.5.3",
35+
"ts-node": "^7.0.0",
36+
"tslint": "^5.0.0",
37+
"typescript": "2.9.2"
3338
}
3439
}

src/dispatchers/in_memory.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,8 @@ export default class InMemoryDispatcher implements Dispatcher {
4040
callback();
4141
}
4242
}
43+
44+
spans(): Span[] {
45+
return this._spans;
46+
}
4347
}

src/start_span_fields.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* Copyright 2018 Expedia, Inc.
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+
// startSpanFields is used for type-checking the Trace.startSpan().
18+
import SpanContext from './span_context';
19+
import * as opentracing from 'opentracing';
20+
21+
export default class StartSpanFields {
22+
childOf?: SpanContext;
23+
references?: opentracing.Reference[];
24+
tags?: any;
25+
startTime?: number;
26+
callerSpanContext?: SpanContext;
27+
}

src/tests/tracer.spec.ts

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/*
2+
* Copyright 2018 Expedia, Inc.
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 { expect } from 'chai';
18+
import Tracer from '../tracer';
19+
import InMemoryDispatcher from '../dispatchers/in_memory';
20+
import StartSpanFields from '../start_span_fields';
21+
import {isUndefined} from 'util';
22+
23+
describe('Tracer tests', () => {
24+
const dummyServiceName = 'my-service';
25+
const dummyOperation = 'my-service-operation';
26+
const downstreamOperation = 'downstream';
27+
const commonTags = {
28+
version: '1.1'
29+
};
30+
31+
describe('Tracer#startSpan', () => {
32+
it('should start a span and dispatch when finished', () => {
33+
const inMemSpanStore = new InMemoryDispatcher();
34+
const tracer = new Tracer(dummyServiceName, inMemSpanStore, commonTags);
35+
expect(tracer.serviceName()).equal(dummyServiceName);
36+
const span = tracer.startSpan(dummyOperation);
37+
expect(span.isFinished()).eq(false);
38+
expect(inMemSpanStore.spans().length).equal(0);
39+
span.finish();
40+
expect(inMemSpanStore.spans().length).equal(1);
41+
const receivedSpan = inMemSpanStore.spans()[0];
42+
expect(receivedSpan.isFinished()).eq(true);
43+
expect(receivedSpan.serviceName()).eq(dummyServiceName);
44+
expect(receivedSpan.operationName()).eq(dummyOperation);
45+
expect(isUndefined(receivedSpan.context().traceId())).eq(false);
46+
expect(isUndefined(receivedSpan.context().spanId())).eq(false);
47+
expect(isUndefined(receivedSpan.context().parentSpanId())).eq(true);
48+
const receivedSpanTag = receivedSpan.tags()[0];
49+
expect(receivedSpanTag.key).eq('version');
50+
expect(receivedSpanTag.value).eq('1.1');
51+
});
52+
53+
it('should start and dispatch server and client spans', () => {
54+
const inMemSpanStore = new InMemoryDispatcher();
55+
const tracer = new Tracer(dummyServiceName, inMemSpanStore, commonTags);
56+
expect(tracer.serviceName()).equal(dummyServiceName);
57+
const serverSpan = tracer.startSpan(dummyOperation);
58+
const startSpanFields = new StartSpanFields();
59+
startSpanFields.childOf = serverSpan.context();
60+
startSpanFields.tags = { 'span.kind': 'client' };
61+
const clientSpan = tracer.startSpan(downstreamOperation, startSpanFields);
62+
63+
expect(serverSpan.isFinished()).eq(false);
64+
expect(inMemSpanStore.spans().length).equal(0);
65+
serverSpan.finish();
66+
clientSpan.finish();
67+
68+
expect(inMemSpanStore.spans().length).equal(2);
69+
inMemSpanStore.spans().forEach(receivedSpan => {
70+
expect(receivedSpan.isFinished()).eq(true);
71+
const receivedSpanTag = receivedSpan.tags()[0];
72+
expect(receivedSpanTag.key).eq('version');
73+
expect(receivedSpanTag.value).eq('1.1');
74+
expect(receivedSpan.serviceName()).eq(dummyServiceName);
75+
});
76+
expect(inMemSpanStore.spans().map(span => span.operationName())).includes(downstreamOperation);
77+
expect(inMemSpanStore.spans().map(span => span.operationName())).includes(dummyOperation);
78+
const receivedClientSpan = inMemSpanStore.spans().filter(span => span.tags().filter(tag => tag.key === 'span.kind' && tag.value === 'client').length > 0)[0];
79+
const receivedServerSpan = inMemSpanStore.spans().filter(span => span.tags().filter(tag => tag.key === 'span.kind').length <= 0)[0];
80+
expect(receivedClientSpan.context().parentSpanId()).eq(receivedServerSpan.context().spanId());
81+
expect(isUndefined(receivedServerSpan.context().parentSpanId())).eq(true);
82+
expect(receivedServerSpan.context().traceId()).eq(receivedClientSpan.context().traceId());
83+
});
84+
});
85+
});

src/tracer.ts

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,7 @@ import Utils from './utils';
2626
import PropagationRegistry from './propagators/propagation_registry';
2727
import TextMapPropagator from './propagators/textmap_propagator';
2828
import URLCodex from './propagators/url_codex';
29-
30-
// startSpanFields is used for type-checking the Trace.startSpan().
31-
declare interface StartSpanFields {
32-
childOf?: SpanContext;
33-
references?: opentracing.Reference[];
34-
tags?: any;
35-
startTime?: number;
36-
callerSpanContext?: SpanContext;
37-
}
29+
import StartSpanFields from './start_span_fields';
3830

3931
export default class Tracer {
4032
_serviceName: string;

0 commit comments

Comments
 (0)