Skip to content
This repository was archived by the owner on Oct 3, 2023. It is now read-only.

Commit 4adaa33

Browse files
djonathascardososilva-fabio
authored andcommitted
fix: fixed span tests
1 parent b5a0635 commit 4adaa33

3 files changed

Lines changed: 65 additions & 46 deletions

File tree

packages/opencensus-core/package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"repository": "census-instrumentation/opencensus-node",
88
"scripts": {
99
"build": "node_modules/.bin/tsc --declaration",
10-
"test": "node_modules/.bin/mocha 'build/test/**/*.js' --reporter spec",
10+
"test": "nyc --reporter=html --reporter=text mocha 'build/test/**/*.js'",
1111
"clean": "rimraf build/*"
1212
},
1313
"keywords": [
@@ -44,7 +44,8 @@
4444
"mocha": "^5.0.4",
4545
"ncp": "^2.0.0",
4646
"ts-node": "^4.0.0",
47-
"typescript": "^2.7.2"
47+
"typescript": "^2.7.2",
48+
"nyc": "11.6.0"
4849
},
4950
"dependencies": {
5051
"async_hooks": "^1.0.0",

packages/opencensus-core/src/trace/model/span.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,12 @@ export class SpanImpl extends SpanBaseModel implements Span {
3535
this.root = root;
3636
}
3737

38-
/** Gets trace id of rootspan. */
38+
/** Gets trace id of span. */
3939
get traceId(): string {
4040
return this.root.traceId;
4141
}
4242

43-
/** Gets trace context of rootspan. */
43+
/** Gets trace context of span. */
4444
get traceContext(): TraceContext {
4545
return {
4646
traceId: this.traceId.toString(),
@@ -64,6 +64,11 @@ export class SpanImpl extends SpanBaseModel implements Span {
6464

6565
/** Ends the span. */
6666
end(): void {
67+
if (!this.started) {
68+
debug("calling end() on un-started span");
69+
return;
70+
}
71+
6772
super.end();
6873
this.notifyEnd();
6974
debug('ending span %o', {

packages/opencensus-core/test/test-span.ts

Lines changed: 55 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -26,75 +26,88 @@ let tracer = new TracerImpl();
2626

2727
describe('Span', function() {
2828
/**
29-
* Should create, start and end a rootspan
29+
* Should create a span
3030
*/
31-
describe('startSpan()', function() {
32-
it('should create an span', function() {
31+
describe('new Span()', function() {
32+
it('should create a Span instance', function() {
3333
const rootSpan = new RootSpanImpl(tracer);
34-
assert.ok(rootSpan instanceof SpanImpl);
34+
const span = new SpanImpl(rootSpan);
35+
assert.ok(span instanceof SpanImpl);
36+
});
37+
});
3538

39+
/**
40+
* Should return the Trace ID
41+
*/
42+
describe('get traceId()', function() {
43+
it('should return the trace id', function() {
44+
const rootSpan = new RootSpanImpl(tracer);
3645
rootSpan.start();
37-
const span = rootSpan.startSpan('spanName', 'typeSpan');
38-
assert.ok(span instanceof SpanImpl);
39-
assert.ok(span.id);
46+
const span = new SpanImpl(rootSpan);
47+
assert.equal(span.traceId, rootSpan.traceId);
4048
});
49+
});
4150

42-
it('should start a span', function() {
51+
/**
52+
* Should the trace context of span
53+
*/
54+
describe('get traceContext()', function() {
55+
it('should the trace context of span', function() {
4356
const rootSpan = new RootSpanImpl(tracer);
4457
rootSpan.start();
45-
const span = rootSpan.startSpan('spanName', 'typeSpan');
46-
span.start();
47-
assert.ok(span.started);
58+
59+
const span = new SpanImpl(rootSpan);
60+
const context = span.traceContext;
61+
62+
assert.equal(context.traceId, rootSpan.traceId);
63+
assert.equal(context.spanId, span.id);
64+
assert.equal(context.options, 1);
4865
});
66+
});
4967

50-
it('should end a span', function() {
68+
/**
69+
* Should start a span instance
70+
*/
71+
describe('start()', function() {
72+
it('should start a span instance', function() {
5173
const rootSpan = new RootSpanImpl(tracer);
5274
rootSpan.start();
53-
const span = rootSpan.startSpan('spanName', 'typeSpan');
75+
76+
const span = new SpanImpl(rootSpan);
5477
span.start();
55-
span.end();
56-
assert.ok(span.ended);
78+
79+
assert.ok(span.started);
5780
});
5881
});
5982

6083
/**
61-
* Should not start span after it ended
84+
* Should end a span instance
6285
*/
63-
describe('Span checking after creation', function() {
64-
it('should not start span after it ended', function() {
65-
const root = new RootSpanImpl(tracer);
66-
root.start();
67-
const span = root.startSpan('spanName', 'typeSpan');
86+
describe('end()', function() {
87+
it('should end a span instance', function() {
88+
const rootSpan = new RootSpanImpl(tracer);
89+
rootSpan.start();
90+
91+
const span = new SpanImpl(rootSpan);
6892
span.start();
6993
span.end();
7094

71-
span.start();
72-
assert.equal(span.ended, true);
95+
assert.ok(span.ended);
7396
});
7497
});
7598

7699
/**
77-
* Should an unique ID for spans
100+
* Should not end a span instance
78101
*/
79-
describe('Span data', function() {
80-
it('should create an unique numeric span ID strings', function() {
81-
const root = new RootSpanImpl(tracer);
82-
root.start();
83-
84-
var numberOfSpansToCheck = 5;
85-
for (var i = 0; i < numberOfSpansToCheck; i++) {
86-
var span = root.startSpan('spanName' + i, 'typeSpan' + i);
87-
var spanId = span.id;
88-
assert.ok(typeof spanId === 'string');
89-
assert.ok(spanId.match(/\d+/));
90-
assert.ok(Number(spanId) > 0);
91-
assert.strictEqual(Number(spanId).toString(), spanId);
92-
}
93-
});
102+
describe('end() before start the span', function() {
103+
it('should not end a span instance', function() {
104+
const rootSpan = new RootSpanImpl(tracer);
105+
rootSpan.start();
106+
107+
const span = new SpanImpl(rootSpan);
108+
span.end();
94109

95-
// TODO
96-
it('should truncate namespace', function() {
97-
this.skip();
110+
assert.ok(!span.ended);
98111
});
99112
});
100113
});

0 commit comments

Comments
 (0)